Video in progress

Strong Middle ⚡ Redis and caching

What is the difference between the Lettuce and Jedis clients, and does Lettuce need a connection pool?

Jedis is blocking: one connection serves one thread at a time, so a pool is a must. Lettuce is built on Netty and is thread-safe, so many threads share one connection. Spring Boot uses Lettuce by default with a single shared connection, and a pool is usually not needed.

📖 Answer

Lettuce multiplexes commands over a single TCP connection: threads send commands, and replies come back in order. For plain GET/SET this is very efficient and the number of connections stays low: 50 pods × 1 connection, instead of 50 × 64 with Jedis.

When Lettuce needs a pool too:

  • blocking commands (BLPOP, XREAD BLOCK): they tie up the shared connection and make everything else wait;
  • MULTI/EXEC transactions: they need a dedicated connection;
  • very large replies (for example, a huge HGETALL) that hold up other requests.
spring:
  data:
    redis:
      lettuce:
        pool:
          enabled: true
          max-active: 16

The pool needs the commons-pool2 dependency.

Jedis's strengths are simplicity and predictable behavior. If the pool size is wrong, you quickly get a Could not get a resource from the pool error.

A problem that's common on prod with Lettuce: when Redis slows down, commands pile up in the single connection's queue and all of them fail with commandTimeout. Set the timeout explicitly (spring.data.redis.timeout), and if you use Cluster, enable topology refresh.