Redis Caching with Spring Boot - Complete Interview Guide

1. What is Redis?

Redis (Remote Dictionary Server) is an open-source, in-memory key-value data store used as:

Redis stores data in RAM, making it extremely fast (compared to database disk reads).

2. Why Use Redis?

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

3. Real-Time Example

E-Commerce Product Catalog

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.

4. Install Redis

Ubuntu

sudo apt install redis
redis-server

Docker

docker run --name redis-cache \
-p 6379:6379 \
-d redis

5. Add Dependencies

<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>

6. Configure Redis

spring.cache.type=redis

spring.redis.host=localhost

spring.redis.port=6379

spring.redis.timeout=60000

spring.redis.password=password

7. Enable Caching

@SpringBootApplication
@EnableCaching
public class MyApplication {

   public static void main(
       String[] args) {

      SpringApplication.run(
          MyApplication.class,
          args
      );
   }
}

8. @Cacheable Example

@Service
public class ProductService {

   @Cacheable(
      value="productCache",
      key="#id"
   )
   public Product getProductById(
      String id
   ) {

      return repository
             .findById(id)
             .orElse(null);
   }
}

Execution Flow

1st Call
---------
Redis Miss
      |
      v
Database
      |
      v
Store in Redis

2nd Call
---------
Redis Hit
      |
      v
Return Cached Data

9. @CachePut Example

@CachePut(
 value="productCache",
 key="#product.id"
)
public Product updateProduct(
 Product product
){
   return repository.save(product);
}

Updates database and refreshes cache.

10. @CacheEvict Example

@CacheEvict(
 value="productCache",
 key="#id"
)
public void deleteProduct(
 String id
){
   repository.deleteById(id);
}

Removes stale data from cache.

11. Redis Cache Lifecycle

Request
   |
   v
Cache Check
   |
   +-- HIT
   |      |
   |      v
   |   Return Data
   |
   +-- MISS
          |
          v
      Database
          |
          v
     Save To Cache
          |
          v
      Return Data

12. Configure TTL (Time To Live)

@Configuration
public class RedisConfig {

   @Bean
   public RedisCacheConfiguration
   cacheConfiguration() {

      return RedisCacheConfiguration
           .defaultCacheConfig()
           .entryTtl(
              Duration.ofMinutes(10)
           )
           .disableCachingNullValues();
   }
}

After 10 minutes cache expires automatically.

13. Real Production Scenario #1

Flight Booking Application

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

14. Real Production Scenario #2

User Profile Service

User Login
     |
     v
Load User Profile

Redis Cache
     |
     +--> Hit (1 ms)

Database
     |
     +--> 50-100 ms

15. Real Production Scenario #3

Microservices API Gateway

Gateway
    |
    +--> User Service
    |
    +--> Product Service
    |
    +--> Auth Service

JWT Tokens
Cached in Redis

Result:
? Faster Authentication
? Reduced DB Calls

16. Cache Patterns

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

17. Redis Data Structures

Type Use Case
String Caching Objects
Hash User Profiles
List Recent Orders
Set Unique Visitors
Sorted Set Leaderboards

18. Verify Redis Cache

redis-cli
List Keys:
KEYS *
Get Cache Value:
GET productCache::101
TTL:
TTL productCache::101

19. Monitoring Redis

20. Common Interview Questions

21. Redis vs Hazelcast vs EhCache

Feature Redis Hazelcast EhCache
Distributed Yes Yes No
Persistent Yes Optional No
Cloud Ready Excellent Good Limited
Performance Very High High Medium

22. Quick Interview One-Liners

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.

23. 5-Minute Interview Answer

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.