Monday, 15 December 2014

Thread safe Jedis client

Jedis is neat client library for Redis. The redis java client (Jedis) object provided by the library is not thread safe. Hence, if an application was muli-threaded, it should ideally be using a JedisPool object (which is thread-safe) or it would be facing a lot of unpredictable behavior and weird exceptions. JedisPool is a Jedis clients pool implementation using the Apache commons pool 2 API.

However, managing a pool and its resources might bring in some complexity and length to the app's implementation. So, I thought abstracting this resource management might help out the developer a lot. I've hosted the implementation on github. A new ThreadSafeJedis class has been introduced which will internally manage pooled jedis objects, and provide an interface similar to that of a Jedis class, but will be awesomely thread safe!

Background: The Jedis class currently uses a single Client object in all it's methods, which reasons the "thread unsafe" nature. Hence, the class cannot be simply extended. All the commands (which are methods) use the client object to launch commands. To introduce thread safe nature, each command must be launched using a client which has been freshly fetched from a pool, which means that all the  methods need to re-implemented in a fashion that they would:
  • borrow a client, 
  • launch the operation, 
  • record response,
  • return the client back to the pool 
  • and return the response back to the callee. 
And that's it!


  1. Hi Nachi,

    This sounds very useful - please lmk (@itamarhaber or itamar at redislabs dot com) once you've finalized the implementation so I can promote it in my newsletter :)


  2. Hi Itamar,

    Sounds great! Will definitely do. :)

  3. Hi Nachi
    Great news for me. Thank you so much

  4. Hi Nachi,

    I have cluster ( 2 nodes) of application servers. Each application is sharing redis cache also each node independently updates the cache. How do I ensure synchronization across node to make sure no cache gets overriden. I ma using Jedis client

  5. Metallic Thread: This kind of thread has a center wrapped with a metal thwart which might be covered with silver amalgam, and shading is added to this thwart with a polyester film which may likewise be connected as an undercoating. Metallic thread is a standout amongst the most tough accessible place to buy machine embroidery thread