A SIMPLE REDIS IMPLEMENTATION IN JAVA

Engin Demircioğlu
8 min readNov 8, 2021

--

WHAT IS REDIS?

Redis (REmote DIctionary Server) is an in-memory database tool. It can be used as an in-memory data store, caching tool and a message broker. Redis stores the records as key-value pairs and provides different types of data structures like strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes and streams. Redis is mostly used for purposes like session cache, object store, message queues (pub/sub) and leaderboards (counting).

You can check the download options below.

https://redis.io/download

Its popularity is increasing day by day. You can check the ranking of popularity below.

https://db-engines.com/en/ranking/key-value+store

We can connect to Redis by client libraries in many programming languages. You can the check client libraries page below to see supported programming languages.

SPRING DATA REDIS

In this article, we will develop a simple application using Redis. We will use Spring Data Redis framework because it will make it easy to write applications by eliminating the redundant tasks and boilerplate code required for interacting with the store through Spring’s infrastructure support. Spring provides a high-level abstraction for us.

There are lots of libraries that we can use to connect Redis in Java language. “Lettuce” and “Jedis” are two successful implementations of these libraries. Spring Data Redis has options to use both “Lettuce” and “Jedis” libraries. It also provides us to use cache capabilities of Spring Framework via configurative abstraction for Redis.

A SIMPLE JAVA APPLICATION

We will develop a simple Java application to connect to Redis and we will show how to use cache capabilities of Spring Framework on Redis without any ekstra configurative development for Redis.

Note: The screenshots were taken from IntelliJ IDEA version 2021.2.1 and TablePlus version 4.5.0.

Firstly, we will run Redis server on our local machine. You can check the instructions above (Part: What Is Redis) to setup Redis.

You can check if Redis Server is running with several ways. We will use a GUI tool for Redis called TablePlus. We added a new Redis connection and tested the connection on localhost with port number 6379 which are the default values of server configuration for Redis.

You can also check it on redis-cli interface by running command “redis-cli ping” like below. It will return a message “PONG” if the server is running successfully.

Now we can create a project by using Spring Boot.

We will add Spring Data Redis dependency to our project.

The “pom.xml” file will look like below.

We can use a property file to change behaviour of Redis for our needs. We can define the server configuration values or other Redis parameters via this configuration file. In order to do this, we will edit “application.yml” file in “resources” directory and we will add two java files (RedisConfig.java, RedisConfigurationProperties.java) under a new package (com.example.redisdemo.config) to arrange the configurations.

We will use Lombok library to transfer some implementation needs like getter-setter methods automatically with one annotation (@Data).

Now, we will create a bean called “RedisTemplate” to connect to Redis and use it’s functions. There are different connectors that we can use to connect to Redis and for this project we will use Lettuce connector. Below, you can see the bean implementation of RedisTemplate class in “RedisConfig.java” file. RedisConfig class is annotated as a configuration file so we will start using beans in this file after the application is started.

We are ready to connect to Redis and use it’s functions. In order to test our implementation we will insert a key-value pair when the application is started. We edited the project application class (RedisDemoApplication.java) to reach to the “run” method which is being called when the application is started. We will override this method to get the RedisTemplate bean and insert a key-value pair to our Redis server. You can see the implementation below.

After the application is started, we will see that our key (test_key_redisTemplate) will be stored in Redis Server like seen below.

As you can see, there are undesired characters at the beginning of the value “test_value”. The reason is that we did not define the serializer information for the values in the RedisTemplate bean configuration. We will define the value serializer like the same as key values.

After this change, we will start the application and check if the value is correct (test_value).

That’s how we can connect to Redis and start using it’s functions. We used ‘opsForValue’ operation class (ValueOperations) to insert string keys but there are lots of different operation types that we can use in our various needs.

CACHE CAPABILITIES

In order to test the cache capabilities, we will add a service class to our project. You can check the implementations of service class (RedisService) below.

In order to enable cache capability, we will add “@EnableCaching” annotation to our application class “RedisDemoApplication.java”. Then we can inject our new service class with “Autowired” annotation into our application class like seen below.

In order to test the cache capability, we will modify our run method and add a new method (getNumberWithCacheable) to insert some keys to Redis Server.

There is one more modification in order to set the serializers of our keys and values. We will add default RedisCacheConfiguration bean to our “RedisConfig.java” file like seen below.

If we do not add this “cacheConfiguration” bean method, the default cache configurations will damage our values with undesired characters. We also have to add a dependency to get rid of errors while using this Jackson configuration.

Now we are ready to run the application and see if the cache system is working or not.

As we can see from server logs, we called the method 10 times to insert random numbers between “0” and “4”. At first call, number “4” is inserted to Redis and the method “getNumberWithCacheable” content is processed. That means the value is not reached from cache. After inserting the values “0“ and “3” like the same way, we tried to call the method with parameter “0” again and you can see that the method “getNumberWithCacheable” content is not processed. That means the value is reached from the cache now. And after all possible values are inserted to Redis, all method calls have been replied from the cache. We can also check the values in Redis server like seen below.

CUSTOM CACHE MANAGER

In order to change cache configurations we can use customized cache manager implementations of Spring. We will add a cacheManager bean to our RedisConfig file like seen below.

We can use RedisConfigurationProperties parameters to change the configuration values.

In order to test the cacheManager, we added a method called “getNumberWithCacheManager” to our service class.

Then we can use this method by editing the run method of our application (RedisDemoApplication.java) like seen below.

We deleted all the keys on RedisServer and started the application to test the cacheManager implementation.

As we can see from server logs, we called the method 10 times to insert random numbers between “0” and “4”. At first call, number “2” is inserted to Redis and the method “getNumberWithCacheManager” content is processed (Number is cached with cacheManager). That means the value is not reached from cache. After inserting the value “3” like the same way, we tried to call the method with parameter “2” again and you can see that the method “getNumberWithCacheManager” content is not processed like the first one. That means the value is reached from the cache. And after all possible values are inserted to Redis, all method calls have been replied from the cache. We can also check the values in Redis server like seen below.

You can download the application from the github address below.

https://github.com/dengin/redisdemo.git

As a result, Spring Framework makes it easy for us to use Redis in various ways. We recommend you to try Spring Data Redis in your projects.

--

--