avatarMd Sajedul Karim

Summary

This article discusses improving API performance using Redis and MySQL in a Spring Boot application, focusing on caching data to reduce expensive database queries.

Abstract

The article begins by explaining the concept of caching and its benefits, such as faster data retrieval and reduced database querying. It then introduces Redis, an open-source in-memory data store, and its features like data persistence, replication, and a wide variety of data structures. The article proceeds to demonstrate how to configure Redis in a docker container and connect it to an application. It also provides a detailed implementation of Redis, MySQL, and JPA with a Spring Boot application, including code snippets for the build.gradle, application.yml, and application class files. The article concludes by discussing the use cases of Redis and sharing the codebase for reference.

Opinions

  • Caching is a powerful tool for improving API performance and reducing expensive database queries.
  • Redis is a suitable choice for caching due to its in-memory data storage, persistence, and replication features.
  • Redis supports a wide variety of data structures, making it versatile for different use cases.
  • Configuring Redis in a docker container is straightforward and can be done using the provided commands.
  • The article provides a detailed implementation of Redis, MySQL, and JPA with a Spring Boot application, which can serve as a reference for developers.
  • The use cases of Redis are diverse, and it can be used in various scenarios to improve application performance.
  • The codebase shared in the article can be a valuable resource for developers looking to implement Redis in their applications.

Improve API Performance using Redis, MYSQL in Spring Boot App

Making faster responses and decreasing dependency on expensive DB query.

Image Credit — Wikipedia

More often during API design, each API may server respond after querying data from a database or from external API calling. Sometimes there are some tables those data are not frequently changed, which means those tables are not transactional.

Database query or external API calls are very much expensive. In this case, we may cache those data into faster storage and server our client from that storage instead of fetch from there.

To achieve this goal, Redis has come. It not only caches data but also persistent with the database periodically and also it works in the distributed system.

Redis stands for Remote Dictionary Server, is a Keystore-based, fast, open-source in-memory-based data store. Its main usages are used as a database, cache, message broker, and queue.

According to the website Redis.io:

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

Redis support data types like string, lists, hashes, bitmaps, geospatial indexes, hyperloglogs, sets, and streams. It also supports asynchronous replication, with very fast non-blocking first synchronization, auto-reconnection with partial resynchronization on netsplit.

In this article, we will learn.

  • Use cases of Redis
  • Configure Redis in a docker container and connect applications
  • Details level implementation of Redis, MySQL, JPA with spring boot application
  • Data analysis of how caching is working in multi-instance deployment
  • Share the codebase

To implement distributed locking using Redis lock, you may check this article.

Our application architecture is given below

Image Credit — Author

What are we doing here?

  • We have a MySQL database running 3406 port in a docker container
  • A Redis server running in port 6379 on a docker container. The Redis server is using as second-level caching.
  • Application instance named country_service. It has two instances, and both are connected to the same MySQL and Redis server.
  • In application, it is straightforward—some CRUD operations of the country.

Why and When should use cache

Caching is the mechanism of storing non-transactional data into an intermediate high-speed data storage layer (cache). In most cases, the cache-store data in a relatively faster hardware system so that its store and retrieval process may more efficient and faster.

  • Redis stores everything in primary memory; for this reason, its storing and retrieval is highly faster than a database retrieval query.
  • It is also persistent so that it will sync data with the database periodically. This persistence level may be configurable; by turning of persistence, you may create a temporary cache.
  • According to redis.io

Redis also supports trivial-to-setup master-slave asynchronous replication, with very fast non-blocking first synchronization, auto-reconnection with partial resynchronization on the net split

  • Unlike simplistic other key-value data stores, Redis has a wide variety of data structures. Redis data types include Strings, List, Sets, Sorted Sets, Hashes, Bitmaps, HyperLogLogs.

Redis configuration

You may download the Redis server version into your machine. In this article, we will use the Redis server from a docker container.

Pull docker image

docker pull redis

Run docker container from the downloaded image

sudo docker run — name my-redis-container -p 6379:6379 -d redis

Now your Redis server is running on the default port 6379

Image Credit — Author

Redis Implementation

Our build.gradle file is given below:

Here, we added MySQL and c3p0 dependency for connection pooling with the database. spring-boot-starter-data-jpa for the Redis dependency

Our application.yml file structure is given below:

Here we configured MySQL and JPA using proper credentials. For cache, we used Redis and added Redis IP and port for connections.

The application class code is given below:

Here, we added @EnableCaching for enabling the Redis cache.

The entity class and table structure are given below:

Here, id is the primary key and auto-increment. other’s fields are name, code

The entity class is given below.

Our repository class is given below.

Here, it is a basic CrudRepositoeryAnd added some JPA interface methods like find an entry by entry id, find all entries and delete an entry by id.

The service method is given below:

Here, we added some method like save, findById and delete. Its description is given below:

Save Data (CachePut)

During saving data, we saved data into table country with JPA repository.

we used CachePut annotation in this method. This annotation is used to update the cache. Here, the key will be country id, and value will be the country object in Redis key store.

  • During the save method, it will save data to the database. After successfully save, it will save a copy into the Redis cache.
  • During cache, the key will be country id, and value will be the country object.
  • Once the cache did, all instances/nodes try to fetch data using country id will directly get from the cache. So it will not further query on the database.

Fetch Data (Cacheable)

findById method has annotations Cacheable And this annotation indicates that the application will cache the result of calling this method.

  • During API calls, first, it will search from the cache using country id. If found, then it will return as a response. If not found, then it will fetch from the database.
  • After cache done from any node, then all connected nodes will automatically found that cached value.
  • The Cache has time-to-live value. After that time, Redis will clear that data from the cache.

Delete data (CacheEvict)

delete method has annotations CacheEvict And This annotation indicates that calling a method can be evicted from the cache by country id.

  • During API call, it will delete the country entity from the database and delete that entry from the cache also.
  • After deleting, no instance will not found this value from the search API.

The controller method

The code for controller/endpoints is given below

By calling this method’s you can check its output

Codebase

You may check the codebase from this GitHub repository

Conclusion

Caching is a very important and powerful tool for developers and distributed systems. Based on your data type and nature of business, you may introduce the Redis cache into your system for better performance.

Thanks for reading this article. I hope you enjoy this.

If you want to learn Microservice implementation using spring cloud with docker, this article can help.

Resources

1. https://redis.io/ 2. https://aws.amazon.com/redis/ 3. https://www.atlantic.net/hipaa-data-centers/ashburn-virginia-hosting/why-redis-has-become-so-popular-fast-open-source/ 4. https://www.baeldung.com/spring-data-redis-tutorial 5. https://pavankjadda.medium.com/implement-hibernate-2nd-level-cache-with-redis-spring-boot-and-spring-data-jpa-7cdbf5632883 6. https://www.netsurfingzone.com/spring-boot/spring-boot-redis-cache-example/

Redis
Spring
Jpa
Java
MySQL
Recommended from ReadMedium