Redis Sorted Sets with Spring Boot and RedisTemplate

This article details about the usage of Redis Sorted Sets with Spring Boot and Spring Data’s RedisTemplate. I had already written about the same use case but used Jedis client instead of RedisTemplate. You can read about it here. The details such as Data, Design, Code etc. are same as the other article. But for the ease of reading this article without the need to go back and forth, i’m providing the same details here also.

RedisTemplate vs Jedis

When you compare the code examples for both these usages (RedisTemplate and Jedis), you’ll see that it’s very simple and easy to use RedisTemplate. Spring Boot’s auto-configuration makes it very easy for the developers as we don’t have to write any code for connection or resource handling. But this comes with a trade-off. The code written using Jedis can be easily deployed anywhere but when you are dependent on Spring Boot’s auto-configuration to get the connection details, the code doesn’t run “as-it-is” in local. I have to write extra code to ensure that the code runs in local as well as in PCF.

The code given in the Github repository covers only the Cloud scenario. This code won’t run as it is in local.

Now, the use case and details…

Use Case — Retrieve one value from a range

Bank A is issuing Car Loans in different states. The rates get changed once in a quarter but the date of initiation of application is used to calculate the interest rate. The rates for each quarter in 5 states are given in the below table

The table explained with an example below,

If the application was initiated by a resident of MD on any dates from 1/1/2017 to 3/31/2017, the interest rate will be 3.00%. If the application was initiated by a resident of PA on any dates from 4/1/2017 to 6/30/2017, the interest rate will be 3.50% and so on.

Design

Since the interest rate doesn’t change frequently, it is a good idea to cache this data. But since a loan application can be initiated on any date, the interest rate valid for that date should be applied.

It is not a good design to store the interest rate corresponding to for every state and every date in between, say, 1/1/2017 and 3/1/2017.

This is where the Sorted Set comes into picture. The data combination can be stored in the Sorted Set such that a query for a state with any date retrieves the Interest Rate effective on that day.

Code

The final product is available my GitHub page.

The typical data structure in Redis is a key-value pair. Sorted Set provides an additional feature called score. This score can be used for retrieving value(s) from a range even if the exact matching record is not present in Redis.

Insertion

For the implementation of this Use Case, I’m giving a logical name for the table as “RatesByState”. This is to easily identify the data for the given use case as Redis doesn’t have individual tables like a traditional RDBMS database.

Key

The key used for the data insertion will be a combination of table name as the state. They are separated by ‘|’. For ex: RatesByState|MD will be a key. Usage of | symbol here is a purely personal choice.

Score

In order to effectively get the rate for any given date and to avoid the storage of every day as a row in the DB, I’ll be using Date as the score. This will just be a Double value of date in yyyyMMdd format. For ex: new Double(“20170101”)

Value

The interest rate will be stored as the value. I’ll be using the String representation, though Redis allows other formats also.

Spring Data Redis API for Insertion of Sorted Sets

I have used StringRedisTemplate class since I’m dealing only with String values here.

In order to insert a Sorted Set, RedisTemplate provides a method to get all Sorted Sets operations — opsForZSet() — which returns ZSetOperations object.

ZSetOperations has “add” method which accepts a key, value and score, in this order. Please note that this order is a little different from the usage in Jedis.

The above code will add a row and return the status code as a boolean.

Retrieval

Key

For data retrieval, I need to give the same key that was used for insertion. For ex: RateByState|MD

Score

For retrieving a value from a Sorted Set, the score is a mandatory field. As mentioned above, the Double value of date is given as score. An important factor to note here is that the date for retrieval can be any date and not necessarily the dates that was used for insertion.

For ex: if the user wants to know the interest rate for MD on 4/3/2017, the query logic will use 20170403 as the score.

Please note that the order of parameters in the reverseRangeByScore method is different than the same method in Jedis API.

The above line will return the interest rate applicable for the give date.

Conclusion

As shown in this page, using Sorted Sets in Redis provides the option to store limited rows but retrieve data for a large variation of input data.

For the whole project, please refer to the GitHub page.

Java and Cloud Architect