Connect to Redis

Learn via video courses
Topics Covered

Overview

Caching is a common technique to improve the performance of any application. Frequently accessed data can be cached, so avoid repeated, expensive database/third-party service calls. Varieties of caching solutions are available in the market, and Redis is one of them and a popular one.

What Is Redis?

The official definition of Redis from its website

Redis is an open-source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.

REDIS

Let's understand each capability.

  • In-memory data store - By default, Redis keeps data in memory through key-value pairs.
  • Database - Redis can be used as a persistence database. By default, Redis keep information in memory; however, it can also be configured to store the information in the physical disk.
  • Caching - This capability of Redis is used widely. Its primary intent was to use it as a highly available distributed cache.
  • Streaming engine - The stream data type enables high-rate data ingestion, messaging, event sourcing, and notifications.
  • Message broker - Redis can be used as a message broker to implement the pub-sub architecture.

Redis has many capabilities but it is traditionally used as cache.

REDIS MYSQL

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Spring Boot Redis Project Setup

Spring boot offers simple integration with Redis using starters and application.properties configuration. It also offers a RedisRepository just like JpaRepository to perform operations on the Redis store.

Dependencies

We need two dependencies to work with Redis.

  • Spring Redis abstraction
  • Java client for Redis

Alternatively, you can use starter dependency will bring jedis along with it.

The above starter dependency comes with a default configuration which can be overridden using the application.properties configuration.

Configuring Redis

By default, Redis runs on your system on host localhost and port 6379. The starter-data-redis comes with the same default configuration; therefore, no need for extra configuration.

If you use a configuration other than the default, it can be provided using properties spring.redis.*. For example, if your Redis instance runs on port 6000, you need to specify a port in the application configuration.

Defining the Model

Let's use an Intern entity and perform operations on it.

Note - The annotation on top is @RedisHash, which marks Objects as aggregate roots to be stored in a Redis.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Defining the Repository

To interact with Redis and the Intern entity, we have a repository not different from regular CrudRepository. Behind the scene, spring takes care of where to fetch information from.

CrudRepository comes with a basic CRUD method, as the name suggests. Now we can perform different operations on the Redis.

Creating a new Redis cache entry

InternRepository#save method will create a new entry in the Redis store.

Updating the existing Cache entry

No special methods are available to update the cache entry. InternRepository#save will update the entry if it already exists in the Redis store.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Clearing Cache

To delete entries from the Redis store, use the method InternReposotory#deleteById.

Conclusion

  • Redis is a key-value database used widely across the industry.
  • Redis can be used as a cache, database, pub-sub, and streaming engine.
  • Spring boot offers spring-boot-starter-data-redis dependency to simplify integration.
  • Redis model uses @RedisHash annotation to mark as a Redis entity.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more