Redis: What It Is and When to Use It
Fast recall
Quick revision
Core answer
Whether you understand Redis beyond the generic 'it's a cache' answer—specific use cases demonstrate practical thinking about performance bottlenecks.
Treating Redis as a primary data store for critical business data without understanding its in-memory nature and potential data loss on crashes.
Mention Spring's @Cacheable annotation with Redis as a cache provider, showing familiarity with the Spring Boot integration pattern.
Interview-ready guide
Interview answer
Redis is an in-memory data structure store whose keys can hold strings, hashes, lists, sets, sorted sets, streams, and other supported structures. Java applications often use it for caches, shared sessions, counters, rate limits, leaderboards, and messaging. For example, a product cache can load from the database on the first miss and store the result with a TTL; later requests may use the cached value. The design still needs invalidation after updates, stale-data rules, serialization, and fallback when Redis is unavailable.
Redis latency is often low, but I would benchmark the real command, value size, network, persistence, and replication setup instead of promising a fixed time. Individual commands are atomic; a multi-command read-modify-write workflow can still race and may need a conditional command, transaction, or Lua script. RDB and AOF offer different persistence trade-offs. Redis can be a rebuildable cache or a primary store, but a primary-store design must prove its durability, replication, backup, failover, and recovery requirements.
Redis: What It Is and When to Use It
Direct Answer
Redis is an in-memory data store that supports strings and richer structures such as hashes, sets, sorted sets, streams, and geospatial indexes. Java backends commonly use it for caches, sessions, rate limits, counters, and messaging. Its latency and durability depend on the command, data size, network, persistence, replication, and deployment settings, so benchmark the real workload.
Code Example
// Add dependency: spring-boot-starter-data-redis
@Service
public class ProductService {
private final ProductRepository repository;
@Cacheable(value = "products", key = "#id")
public Optional<Product> findById(Long id) {
// Expensive database query - Redis caches the result
return repository.findById(id);
}
@CacheEvict(value = "products", key = "#product.id")
public Product update(Product product) {
return repository.save(product);
}
}
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
return RedisCacheManager.builder(factory)
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10))) // Cache expires after 10 minutes
.build();
}
}
Common Mistakes & Pitfalls
- Assuming Redis is always only a cache: it can be a primary database in a design that explicitly meets its persistence and recovery needs.
- Assuming one command makes a workflow atomic: read-then-write sequences can race even though each command is atomic.
- Caching without invalidation: define how writes update or evict cached values and how much staleness is acceptable.
- Confusing Pub/Sub with a durable queue: disconnected subscribers miss Pub/Sub messages; use Streams or another broker when retention and acknowledgement matter.
- Ignoring memory and eviction: set memory policy deliberately and monitor value growth, hot keys, latency, persistence, and failover.
Check your understanding
Answer these without looking back at the article.
- How does Spring Boot integrate with Redis for caching?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.