Implementing Cache-Aside with Redis TTL and Key Deletion
Short answer
AB Delete related cache keys when the source data changes and configure a 10-minute Time to Live on each key.
Question
Key Takeaways
The correct solution involves deleting related cache keys when source data changes and configuring a 10-minute Time to Live on each key to ensure data consistency and adherence to expiration requirements in Azure Managed Redis.
Expert Analysis
Delete keys on mutation (implement an event-driven or database-hook system that purges related Redis keys the moment the source data updates) and use absolute expiration with a strict 10-minute TTL on each key. Sliding expiration would extend lifetime based on access and can preserve stale data; keyspace notifications do not replace application logic that invalidates entries when the authoritative source changes.
To ensure data consistency in a cache-aside pattern where cached semantic search results must strictly reflect the underlying source of truth, two distinct mechanisms are required: proactive invalidation upon data mutation and reactive expiration to handle cases where no update occurs. The correct strategy combines deleting related cache keys when the source data changes with configuring a 10-minute Time to Live (TTL) on each key.
Option A is critical for immediate consistency. In a cache-aside model, the cache acts as a shadow of the primary data store. When the underlying data changes, any existing cached entry becomes stale. By deleting the related cache keys at the moment of mutation, you force the next read request to bypass the cache, fetch fresh data from the source, and repopulate the cache with the current state. This ensures that users never see outdated information resulting from recent updates. Without this step, the cache would continue to serve old data indefinitely until it expired naturally, violating the requirement that "cached entries must NOT be returned" if the underlying data changes.
Option B addresses the time-bound constraint. The requirement specifies that search results must remain cached for 10 minutes. Configuring an absolute TTL of 10 minutes ensures that even if the source data does not change, the cache entry will expire after that duration. This prevents indefinite caching and guarantees that the system periodically refreshes its data, which is essential for maintaining eventual consistency over time. Absolute expiration is deterministic and aligns precisely with the business requirement of a fixed 10-minute window.
Option C, implementing sliding expiration, is incorrect because it extends the lifetime of a key based on access frequency. If a popular search result is accessed frequently, its TTL would reset with every hit, potentially keeping stale data in the cache for hours or days if the source data remains unchanged during that period but was updated earlier. This contradicts the requirement that cached entries should not persist indefinitely and undermines the goal of ensuring relatively fresh data within the 10-minute window. Sliding expiration prioritizes hot data retention over data freshness, which is the opposite of what is needed here.
Option D, configuring a cache notification for key space events, is insufficient and misleading. While Redis supports key space notifications, these are typically used for monitoring or triggering secondary actions, not for enforcing primary data consistency in the application logic. Relying solely on notifications does not guarantee that the cache is cleared before the next read; it merely signals that a change occurred. Furthermore, notifications are asynchronous and may suffer from delays or loss, making them unreliable for strict consistency requirements. The application must actively manage cache invalidation through explicit delete operations (as in Option A) rather than passively reacting to events.
The combination of proactive deletion (A) and absolute expiration (B) creates a robust cache-aside implementation. Deletion handles immediate consistency upon updates, while TTL handles eventual consistency for non-updated data. This dual approach ensures that cached semantic search results are always either fresh (due to deletion on mutation) or within the acceptable 10-minute staleness window (due to TTL).
Exam Strategy
When dealing with cache consistency questions, always look for a combination of proactive invalidation (deleting keys on write/update) and reactive expiration (setting a TTL). Avoid options like sliding expiration or event notifications unless explicitly asked for monitoring or performance optimization, as they do not guarantee strict data consistency against the source of truth.
Related Analysis
Practice All AI-200 Questions
Access 69 questions with complete answers and detailed explanations.
View Full AI-200 Practice Test →