Improve API Performance using Redis, MYSQL in Spring Boot App
Making faster responses and decreasing dependency on expensive DB query.

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

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

Redis Implementation
Our build.gradle file is given below:




