Implementing Frequency-Based Cache Eviction in Azure Managed Redis
Short answer
B Use the allkeys-lru eviction policy to automatically retain frequently accessed embeddings in memory while removing least recently used keys when Redis reaches its maxmemory limit.
Question
Key Takeaways
The allkeys-lru eviction policy is the correct strategy for keeping frequently accessed embeddings in Azure Managed Redis memory. This policy evicts the least recently used keys first, ensuring hot data persists while cold data is removed when memory limits are reached.
Expert Analysis
Redis eviction policies decide which keys to remove when the configured maxmemory limit is reached.
allkeys-lruapplies Least Recently Used eviction across the entire key space, so keys that are accessed frequently stay in memory while the least recently used ones are evicted first — exactly the "keep only frequently accessed embeddings in memory" requirement.
The core challenge in this scenario is managing a bounded memory resource (Redis) for an unbounded or semi-bounded set of data items (embeddings) where access patterns vary significantly. The requirement is explicit: keep frequently accessed embeddings in memory and allow rarely used ones to be evicted. This is a classic cache eviction problem, not a data expiration problem. To solve it correctly, one must understand how Redis handles memory pressure via its maxmemory-policy configuration. When the maxmemory limit is hit, Redis does not simply crash or reject writes; it triggers an eviction policy based on the selected algorithm.
Option B, allkeys-lru, is the correct choice because it implements the Least Recently Used algorithm across all keys in the database. In the context of your embeddings formatted as doc:(id), every key is subject to eviction consideration. If a specific embedding key has not been accessed for some time, its "recency" score decreases. When new embeddings need to be stored and memory is full, Redis identifies the key with the lowest recency (the least recently used) and evicts it. This naturally preserves hot embeddings (frequently accessed) and discards cold embeddings (rarely used), perfectly aligning with the business logic required.
Option A, volatile-ttl, is incorrect because it only considers keys that have a Time-To-Live (TTL) set. It evicts the keys with the shortest remaining TTL among those that are expiring. This policy ignores access frequency entirely. An embedding could be accessed frequently but have a short TTL, leading to premature eviction, while another embedding might be rarely accessed but have a long TTL, staying in memory unnecessarily. Furthermore, if no keys have a TTL set, volatile-ttl cannot evict anything, potentially causing write errors when memory is full. Since the requirement is based on frequency, not time-to-live, this policy fails the functional test.
Option C, the EXPIRE command, is a fundamental misunderstanding of scope. EXPIRE is a command used to set a TTL on a single key. It is a mechanism for manual expiration, not an automated eviction policy driven by system constraints like memory limits. Using EXPIRE would require application-level logic to determine which keys to expire, which contradicts the goal of implementing a system-level caching strategy that automatically manages memory based on usage patterns. It does not provide the automatic, frequency-based pruning described in the requirement.
Option D, time-window expiration, refers similarly to setting fixed durations after which data becomes invalid. Like EXPIRE, this is a static temporal constraint. It assumes that data older than a certain window is useless, regardless of whether it was accessed yesterday or last week. This approach cannot distinguish between a frequently accessed embedding that happens to be slightly older versus a rarely accessed one. It lacks the dynamic adaptability provided by LRU (Least Recently Used) algorithms.
A critical technical nuance here is the distinction between volatile-lru and allkeys-lru. While both use LRU, volatile-lru only evicts keys with a TTL. Since embeddings often don't have strict expiration times (they are semantic representations, not transient session data), relying on volatile-lru would effectively disable eviction unless you manually set TTLs on everything, which adds overhead and complexity. allkeys-lru operates on the entire dataset, making it the robust default for caches where any item can be dropped based on recency. Additionally, while the reference note mentions vector indexes (HNSW) for search functionality, this is orthogonal to the memory management layer. The caching strategy ensures that the working set of vectors fits in RAM for fast retrieval, independent of how the similarity search is indexed. Therefore, configuring maxmemory-policy allkeys-lru is the definitive solution.
Related Analysis
Practice All AI-200 Questions
Access 69 questions with complete answers and detailed explanations.
View Full AI-200 Practice Test →