Assume your cache is stale due to frequent updates in the underlying data source. How would you determine when to refresh the cache, and what strategies would you use to ensure minimal disruption to users?

Keeping a cache fresh against frequently changing source data requires a deliberate invalidation strategy, typically combining event-driven refresh with a time-to-live fallback so stale data never lingers indefinitely.

Key Points

Example

An inventory service publishes a "stock updated" event whenever a database write occurs; a listener evicts the corresponding cache key immediately, while a five-minute TTL on all entries guards against any event that's dropped.

@Service
public class StockService {

    @Cacheable(value = "stock", key = "#sku")
    public int available(String sku) { return repository.findStock(sku); }

    // Evict immediately when the data changes through this service
    @CacheEvict(value = "stock", key = "#sku")
    public void update(String sku, int qty) { repository.updateStock(sku, qty); }
}

// Changes made by OTHER services arrive as events
@KafkaListener(topics = "stock-updated", groupId = "catalog-cache")
public void onStockUpdated(StockUpdated e) {
    cacheManager.getCache("stock").evict(e.sku());
}

// Safety net: every entry expires after 5 minutes even if an event is missed
@Bean
RedisCacheConfiguration cacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5));
}

Interview Tip

A concise interview answer is

I'd combine event-driven cache invalidation, where a data update publishes an event that evicts the stale entry, with a TTL as a safety net in case an event is missed. That balances freshness with performance and avoids serving badly stale data to users.

Last updated: 1 November 2026

Previous question: Do you use caching? In which scenarios, and when do you remove or refresh cached data?

Next question: How do you verify your changes after they are deployed to production?

More Spring Boot - Level V (Expert) interview questions

All Spring Boot - Level V (Expert) questions