How to implement Redis Cache Using Spring boot?
Published: 2021-11-03 09:21:57


Hello 👋,Its Rohan Kadam 😊

Hello All 👋, You all may have in mind many questions such as **What is Redis Cache? Why use Redis Cache? How to use Redis Cache?** In this blog, we are going to answer all the questions asked above but predominately **How Redis Cache implemented Using Spring Boot?**.

![How to implement Redis Cache Using Spring Boot](https://cdn-images-1.medium.com/max/3840/1*Eb_Nvyac9_OmenCNx0JF9w.png)

### What is Redis Cache?

Redis, which stands for Remote Dictionary Server, is a fast, open-source, in-memory, key-value data store. The project started when Salvatore Sanfilippo, the original developer of Redis, wanted to improve the scalability of his Italian startup. From there, he developed Redis, which is now used as a database, cache, message broker, and queue. Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

### Why use Redis Cache?

Redis is a great choice for implementing a highly available in-memory cache to decrease data access latency, increase throughput, and ease the load off your relational or NoSQL database and application. Redis can serve frequently requested items at sub-millisecond response times and enables you to easily scale for higher loads without growing the costlier backend. Database query results caching, persistent session caching, web page caching and caching of frequently used objects such as images, files, and metadata are all popular examples of caching with Redis. Redis can be used with streaming solutions such as Apache Kafka and Amazon Kinesis as an in-memory data store to ingest, process, and analyze real-time data with sub-millisecond latency. Redis is an ideal choice for [real-time analytics](https://aws.amazon.com/elasticache/redis/#Real-time_Analytics) use cases such as social media analytics, ad targeting, personalization, and IoT.

### **How to use Redis Cache?**

The use of Redis Cache is are many its can be used as a database or cache but in these parts, we going to cover how we can use **Redis as a cache.**

### Step 1: How to generate Spring Boot Project?
[**Spring Initializr**](https://start.spring.io/)

### Step 2: Which dependencies should I include?

![Redis Cache Dependencies](https://cdn-images-1.medium.com/max/2648/1*ONPlr0RBVFGU9SLFNT_mzw.png)

### Step 3: What are the properties we need to add to the application. properties?

![application.properties](https://cdn-images-1.medium.com/max/3432/1*b5MTN-9qfBMGYdumxb29YQ.png)

### Step 4: What will be the Configuration for Redis Cache?

Note: We are using building a spring boot application so we are going to Jedis Configuration in configuration.

![Redis Configuration For Application](https://cdn-images-1.medium.com/max/3340/1*abKE10bZjDAW3UlKUuxxLw.png)

### Step 5: What are the Controller Endpoints or API Endpoints?

![User Controller For Redis Application](https://cdn-images-1.medium.com/max/3340/1*rEe6VPC5nlDllr7dcOEBOg.png)

### Step 5: What is GenericResponse?AddUserDto?& AuthUserDto?

![Generic Response](https://cdn-images-1.medium.com/max/2000/1*B3WrWnapiSRFStPPkRz4ag.png)

![AddUserDTO](https://cdn-images-1.medium.com/max/2000/1*yjGol62dTUB7if9AeQZQVg.png)

![AuthUserDTO](https://cdn-images-1.medium.com/max/2168/1*r_w1pl9wKsNEVga8LPy1Hw.png)

### Step 6: Where is business logic or so-called UserService?

![User Service For Login and Registration](https://cdn-images-1.medium.com/max/3476/1*qWzvMOhKJe33DRkGliVNFQ.png)

### Step 7: Where is my entity class?

![RedisUser](https://cdn-images-1.medium.com/max/2744/1*RTBv25zQpgxrEGOgPz5nHw.png)

### Step 8: How to perform CRUD operations for Redis cache?

![Redis Utility for Redis Service](https://cdn-images-1.medium.com/max/3012/1*lZXAtAGu1ejx4IjfMvlvFQ.png)

### Step 9: How to test It are project?

Testing of API endpoint can be done using many different ways if you asked me its will using **[Postman](https://www.postman.com/)**

**API Endpoints are:-**

**User Registration Endpoint**

[http://localhost:8080/redis/demo/add](http://localhost:8080/redis/demo/add)

**User Login Endpoint**

[http://localhost:8080/redis/demo](http://localhost:8080/redis/demo)

### Conclusion:-

In the article, we tried to answers many questions related to Redis Cache and how to implement the same using Spring boot. Redis Cache powerfully features such as caching and fast querying are used by many fortune 500 companies to provide service to others.

Please do share and like 💕if you find the article useful. Follow me on medium [Rohan Ravindra Kadam](undefined) and on Twitter at [**rohankadam25](https://twitter.com/rohankadam25)**

![Thank You Viewers](https://cdn-images-1.medium.com/max/2800/1*mRwSuSvhuRcZKS3uElP6bg.png)