![]() ![]() Private static ConnectionMultiplexer Leak圜onnĬonn = ConnectionMultiplexer.Connect(configOptions.Value) įinally, we write some code that runs some Redis stuff in parallel: Private static ConnectionMultiplexer conn Then, we provide a property with the faulty lazy initialization: Var configOptions = new ConfigurationOptions() Ĭ("localhost:6379") ĬonfigOptions.ClientName = "LeakyRedisConnection" First, let’s set up the ConfigurationOptions with a client name so that we can identify connections coming from our program: It is not hard to prove that this code is leaky in multithreaded environments. RedisCacheDb = RedisConnection.GetDatabase() Īs I pointed out in my question, this is a bad form of lazy initialization because it lacks thread safety: multiple threads may get through the checks and initialize multiple connections, resulting in connection leaks. RedisConnection = ConnectionMultiplexer.Connect() If (RedisConnection = null || !RedisConnection.IsConnected) If (ConfigurationManager.ConnectionStrings != null) Protected void Session_Start(object sender, EventArgs e) Public static ConnectionMultiplexer RedisConnection For instance, check out the implementation in this answer: Unfortunately, it can be a little bit tricky to use, and the existing documentation is far from comprehensive.Īfter installing StackExchange.Redis via NuGet, a Redis connection can be obtained via a special ConnectionMultiplexer object. Working with this is already tricky in itself, and many get this wrong. Read on for a more detailed article on the topic. Update 25th October 2016: Just looking to quickly set up a Redis connection? Check out the followup article. Once you’ve got hold of your Redis database, you can perform your regular Redis operations on it. “The object returned from GetDatabase is a cheap pass-thru object, and does not need to be stored.”ĭespite this, I see no point in having an unnecessary extra level of indirection in my code, so I like to store this and work directly with it. That’s fine, because the Basic Usage documentation states that: I’ve seen a lot of code in the past that just calls GetDatabase() all the time, for each operation. Once you have the means to create a connection (as above), just get the lazy value, and from it get a handle on one of the 16 Redis databases (by default it’s database 0 if not specified): However, if you get lots of timeouts, you should review your code and look for bottlenecks or blocking operations. In such cases, you don’t want an operation to fail because of a one-off spike, so just give it a little extra (3 seconds should be reasonable). But we don’t work in an ideal world, and since Redis is single-threaded, expensive application commands against Redis or even internal Redis operations can cause commands to at times exceed this threshold and result in a timeout. 1000, because the value in milliseconds), and operations against Redis should typically be a lot less than that. The default for syncTimeout is 1 second (i.e. This is more concise but also makes configuration a lot easier.Īt the very least, you should have one or more endpoints that you’ll connect to (6379 is the default port in case you leave it out), abortConnect=false to automatically reconnect in case a disconnection occurs (see my original article for details on this), and a reasonable syncTimeout in case some of your Redis operations take long. You build up a connection string using a comma-separated sequence of configuration parameters (as an alternative to ConfigurationOptions in code, which the original article used). My original article goes into detail on why this lazy construct is necessary, but it is mainly because it guarantees thread safety, so the ConnectionMultiplexer will be created only once when it is needed (which is how it’s intended to be used). "localhost:6379,abortConnect=false,syncTimeout=3000")) Then, figure out what connection you need, and build a lazy factory for it (ideally the connection string should come from a configuration file): If you need to just try this out quickly, you can grab a copy of Redis for Windows (just remember that this is not supported for production environments).įirst, install the NuGet package for the Redis client library: ![]() So here’s how you set up a Redis connection using StackExchange.Redis, really quickly. Also, there is a connection string format that’s a lot more concise. A lot of people found this useful, but at the same time the article went into a lot of detail in order to explain the dangers of doing this wrong. Last year I wrote an article about the right way to set up a Redis connection using StackExchange’s Redis client library.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |