Redis (Remote Dictionary Server) is an open-source, in-memory key-value data store used as:
| Without Redis | With Redis |
|---|---|
| Every request hits DB | Frequently used data comes from cache |
| Slow response time | Fast response time |
| High DB load | Reduced DB load |
| More CPU usage | Better scalability |
User Requests Product
|
v
Redis Cache
|
Cache Hit ?
|
Yes --> Return Product
No
|
v
Database
|
v
Store Result in Redis
|
v
Return Response
A product viewed 100,000 times should not trigger 100,000 database queries.
sudo apt install redis redis-server
docker run --name redis-cache \ -p 6379:6379 \ -d redis
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-pool2
</artifactId>
</dependency>
spring.cache.type=redis spring.redis.host=localhost spring.redis.port=6379 spring.redis.timeout=60000 spring.redis.password=password
@SpringBootApplication
@EnableCaching
public class MyApplication {
public static void main(
String[] args) {
SpringApplication.run(
MyApplication.class,
args
);
}
}
@Service
public class ProductService {
@Cacheable(
value="productCache",
key="#id"
)
public Product getProductById(
String id
) {
return repository
.findById(id)
.orElse(null);
}
}
1st Call
---------
Redis Miss
|
v
Database
|
v
Store in Redis
2nd Call
---------
Redis Hit
|
v
Return Cached Data
@CachePut(
value="productCache",
key="#product.id"
)
public Product updateProduct(
Product product
){
return repository.save(product);
}
Updates database and refreshes cache.
@CacheEvict(
value="productCache",
key="#id"
)
public void deleteProduct(
String id
){
repository.deleteById(id);
}
Removes stale data from cache.
Request
|
v
Cache Check
|
+-- HIT
| |
| v
| Return Data
|
+-- MISS
|
v
Database
|
v
Save To Cache
|
v
Return Data
@Configuration
public class RedisConfig {
@Bean
public RedisCacheConfiguration
cacheConfiguration() {
return RedisCacheConfiguration
.defaultCacheConfig()
.entryTtl(
Duration.ofMinutes(10)
)
.disableCachingNullValues();
}
}
After 10 minutes cache expires automatically.
Search Hyderabad ? Delhi Thousands of users search same route. Store result in Redis for 5 minutes. Benefits: ? Faster Search ? Less DB Load ? Better User Experience
User Login
|
v
Load User Profile
Redis Cache
|
+--> Hit (1 ms)
Database
|
+--> 50-100 ms
Gateway
|
+--> User Service
|
+--> Product Service
|
+--> Auth Service
JWT Tokens
Cached in Redis
Result:
? Faster Authentication
? Reduced DB Calls
| Pattern | Description |
|---|---|
| Cache Aside | Application manages cache manually |
| Read Through | Cache loads data automatically |
| Write Through | Writes DB and Cache together |
| Write Back | Write cache first, DB later |
| Type | Use Case |
|---|---|
| String | Caching Objects |
| Hash | User Profiles |
| List | Recent Orders |
| Set | Unique Visitors |
| Sorted Set | Leaderboards |
redis-cliList Keys:
KEYS *Get Cache Value:
GET productCache::101TTL:
TTL productCache::101
| Feature | Redis | Hazelcast | EhCache |
|---|---|---|---|
| Distributed | Yes | Yes | No |
| Persistent | Yes | Optional | No |
| Cloud Ready | Excellent | Good | Limited |
| Performance | Very High | High | Medium |
| Question | Answer |
|---|---|
| Redis | In-memory key-value data store. |
| Caching | Temporary storage of frequently accessed data. |
| TTL | Time after which cache expires. |
| @Cacheable | Reads from cache before executing method. |
| @CachePut | Updates cache after method execution. |
| @CacheEvict | Removes entries from cache. |
| Cache Hit | Data found in cache. |
| Cache Miss | Data not found in cache. |
| Redis Port | 6379 |
| Best Use Case | Frequently accessed read-heavy data. |
| Redis Advantage | Sub-millisecond response time. |
| Cache Aside Pattern | Most common caching strategy. |
| Redis Monitoring | RedisInsight, Prometheus, Grafana. |
| Redis Persistence | RDB and AOF mechanisms. |
Redis is an in-memory key-value datastore commonly used as a caching layer in Spring Boot microservices. It improves performance by storing frequently accessed data in memory, reducing database load and response time. Spring Boot integrates Redis using spring-boot-starter-data-redis and caching annotations like @Cacheable, @CachePut, and @CacheEvict. Data can be configured with TTL values to automatically expire stale entries. In production systems such as e-commerce platforms, user profile services, authentication systems, and API gateways, Redis significantly improves scalability, throughput, and user experience by serving cached data in milliseconds instead of querying the database repeatedly.