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

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.

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.

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.