Lettuce — An Advanced Java Client for Redis implementation

Sourav Mukherjee
Globant
Published in
3 min readDec 2, 2020

Ritesh was worried about his application’s performance. His application was interacting with DB for static DB information each time when request was landing to his app. Ritesh was searching a solution to reduce the DB call from his application. While searching. he came to know about Redis.

Ritesh started reading below information from Internet.

Basic concept of Redis:

Redis is an open source and NoSQL database which follows the principle of key-value in-memory data store. It is used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams.

  • NoSQL Database
  • Key Value Store
  • In Memory Database
  • Data Structure Server
  • Open Source

When we use Redis:

  • Cache
  • Leaderboard
  • Queue
  • Cookie Storage
  • Messaging
  • Data Replication using Master/Slave server etc.

System Architecture of Caching with Redis

Java Clients of Redis:

In order to use Redis with Java you will need a Java Redis client. lettuce and Jedis are two most common Java clients of Redis.

Overview of Lettuce:

Lettuce provides synchronous, asynchronous and reactive APIs to interact with Redis.

Lettuce offers a natural interface for making asynchronous requests from the Redis database server and for creating streams.

Why Lettuce Over Jedis:

Jedis can work with Clusters synchronously.
Whereas, Lettuce is capable of synchronously, asynchronously, reactive interaction with clusters.

Master/Slave:

Redis servers replicate themselves in a master/slave configuration. The master server sends the slave a stream of commands that replicate the master cache to the slave. Redis doesn’t support bi-directional replication, so slaves are read-only.

Sentinel

Redis Sentinel monitors master and slave instances and orchestrates failovers to slaves in the event of a master failover. Lettuce can connect to the Sentinel, use it to discover the address of the current master, and then return a connection to it.

Implementation:

After knowing about Redis Cache Ritesh determined to implement Redis in his java Application. He chose Lettuce java client for his application because it has Master/Slave concept and is capable of synchronously, asynchronously, reactive interaction with clusters. After adding Lettuce dependencies in his application, application started giving errors. He was confused.

He started again exploring how to implement Redis cache with Spring boot above 2.0.x.

Implementation of Redis cache with Spring boot:

Add dependencies for Redis and lettuce.
Add properties in application.properties.
Write a configuration for Redis Connection.
Write an Util class for Redis Operations like putValues, getValues, setExpire etc.
For storing or getting caches, call RedisUtil methods with Redis key and value.
Pass the cacheable list to CacheManager.
Check the Redis Cache in Redis server.

Git Link:

https://github.corp.globant.com/scalableplatform/lettuce-A-Java-Redis-Client

Now Ritesh is happy. His application is too fast. He got appreciation from his client for his work. Now, if you are also facing similar issue of performance in your application, you can go ahead to implement Redis with Lettuce client.

It really works.

--

--