Inspect Redis artistry!
By: Keerthana Thiagaraj
Tags: NoSQL, Spring Boot, Ds In The Real World, Data Journalism, Redis

This article will probe into the magic of Redis for cache management, to reduce client workload and speed up your application.

Over a period of time, I kept wondering how a large scale application would respond impulsively when the number of users increase rapidly, which means large audience interaction, large database requests, large response time, and obviously large costs. 🙄 🤔 It was really strange for me to understand the whole situation because of my very less experience in the IT industry to know how could we actually save the god damn database requests? As far as I knew saving data could be done via database only.

When I was cogitating about the solution, there came a sudden notion about Caching technique!🙇

What is Caching?

Caching is the process of storing some data in Cache. It is a temporary storage component area where the data is stored so that in future. Data can be served faster.

The distributed cache is a widely-adopted design pattern for building robust and scalable applications. By serving data from the cache, and instead of fetching it from slower devices or re-computing it, the application’s overall performance is improved.

The cache also relieves some of the load on external data sources as well as improves the availability of the application during outages.

Redis is the most popular distributed caching engine today.

What is Redis ?

Redis is an open source, NoSQL in-memory Data Structure Store, used as a database, a caching layer or a message broker. It supports data structures such as string, hashes, lists, sets, sorted sets

Why it is used?

It is blazingly fast!

It is developer friendly. Redis is being supported in most of the languages. Languages like JavaScript, Java, Go, C, C++, C#, Python, Objective-C, PHP and almost every famous language out there has support for this.

is being supported in most of the languages. Languages like JavaScript, Java, Go, C, C++, C#, Python, Objective-C, PHP and almost every famous language out there has support for this. Easy to install, and easy to use

In memory with high availability

Install and Start Redis:

Primarily, install redis in your system. Refer the official website for the installation. Navigate to the folder where Redis server resides and start the server:

Redis server

In other tab, execute the below command to open Redis command line interface which is a simple program that allows you to send commands to Redis and read the replies sent by the server directly from the terminal.

There are plenty of commands available in Redis. We are going to make use of monitor which helps in understanding what is happening to the redis database.

Implementation of Redis in Spring Boot application

Create a Spring Boot project with the dependencies:Spring Data Redis,spring-boot-starter-web.

Project structure

Open pom.xml and add jedis dependency. Jedis is the client library in java for Redis. It is light-weight and easy to use. But, there are other popular client libraries(i.e. lettuce and Redisson) are also available to connect with Redis which offers some unique features like thread safety, transparent reconnection handling and async API calls.

Now, let us try to quickly drill down the configuration of each class.

First of all, include the Redis template in the config class which is considered to be obligatory.

@Configuration

public class redisConfig {



@Bean

public JedisConnectionFactory jedisConnectionFactory() {

RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);



return new JedisConnectionFactory(redisStandaloneConfiguration);

}



@Bean

public RedisTemplate redisTemplate() {

RedisTemplate<String, Object> template = new RedisTemplate<>();

template.setConnectionFactory(jedisConnectionFactory());

template.setKeySerializer(new StringRedisSerializer());

return template;

}

Incorporate the model class and dao class.

Now comes the vital part of the application. In EmployeeRepoImpl class, we are going to Autowire the RedisTemplate defined in config class, in-order to access the Redis database. Also, we are going to use opsForValue property of RedisTemplate to add the employee information to the database. In this article, I have shown the usage of String and List data types of Redis. You can also explore on Set and Hash usages. Refer here to embody the complete details.

Next, let us design the service classes. Refer here for service impl classes.

Lastly, it marks the end of the application with the design of controller class where the endpoints are defined.

Shakedown our application:

POST Call- /employee/add

Employee info persisted in Redis database with TTL 10 Seconds

Get call-employee/{id}

Note: GET call will render results only within TTL period.(i.e) In this case, the results are available only for 10 seconds.

Redis cache limits:

It has no limit on storing data on a 64-bit system but, on the 32-bit system, it can only store 3 GB. So, once cache reaches its memory limit, we should remove the old data to make some space for the new one.

Complete code repository can be found here.

Happy learning.🎊 Any suggestions are appreciated! Give a clap✌️ and extend your love by gandering at my other articles.