Domain 2 of 4 · Chapter 5 of 12

Implement Azure Managed Redis caching, expiration, and invalidation

Unlock the complete study guide + 1,040 practice questions across 16 full exams.

Bundled into the existing Developing AI Cloud Solutions on Azure premium course — no separate purchase.

14-day money-back guarantee — no questions asked.

Included in this chapter:

  • Two ways a key leaves the cache
  • The tier sets capacity, the policy sets your client
  • Setting a time to live without losing it
  • How Redis actually removes an expired key
  • Eviction: maxmemory-policy and the volatile-lru default
  • Cache-aside: read on miss, invalidate on write
  • When a hot key expires: the cache stampede
  • Keyspace notifications and their delivery guarantee
  • Exam-pattern recognition

Eviction policies, and what each one does when the instance is full

maxmemory-policyWhich keys are candidatesHow the victim is chosenReach for it when
volatile-lru (the Azure Managed Redis default)Only keys that have a TTL setLeast recently used among those candidatesEvery cached entry carries an expiry and you want keys without one treated as pinned. If no key has a TTL, nothing is eligible and the instance stops accepting writes instead.
allkeys-lruEvery key in the instanceLeast recently used across the whole keyspaceThe instance is purely a cache and you would rather lose the coldest entry than fail a write. Microsoft names this as the alternative when you want any key to be evictable under pressure.
volatile-ttlOnly keys that have a TTL setThe one closest to expiringEntries carry meaningfully different lifetimes and shedding the soonest-to-die first is closest to what would have happened anyway.
volatile-randomOnly keys that have a TTL setPicked at random among those candidatesRecency carries no signal for your workload and you want the cheapest possible choice.
noevictionNoneNothing is removed; writes fail insteadThe instance holds data you must not silently lose, and you would rather see an out-of-memory error on the write path than discover a key vanished.

Decision tree

Choosing how a cached entry stays freshRead-after-write freshness?the caller must see its own writeyesWrite-throughupdate the store and the cachein the same operationnoDoes your code perform the write?the change goes through a path you controlyesInvalidate on writecommit to the store first,then DEL or UNLINK the keynoMust other processes hear about it?in-process caches on other nodesyesKeyspace notificationspublish key changes, and keepthe TTL as the real backstopnoTime to live onlybound staleness with an expiry on every writeEvery path still needs an eviction policy: the default volatile-lru only evicts keys that have a TTL.

Cheat sheet

  • The redis-py client connects to Azure Managed Redis over TLS, then issues SET and GET
  • SET can atomically apply an expiry and conditional flags (EX/PX, NX/XX) in one round trip
  • MSET/MGET and pipelining batch multiple operations to cut round trips
  • EXPIRE sets a key's time-to-live in seconds; Redis deletes the key automatically when it lapses
  • TTL returns the remaining life: a positive value, -1 for no expiry, and -2 if the key does not exist
  • PERSIST removes a key's expiry, and a plain SET on an existing key clears its TTL
  • Redis mixes lazy and active expiration, so an expired key is never served
  • Cache-aside checks Redis first and loads from the backing store only on a miss
  • A TTL on each cached entry bounds staleness and lets the cache self-heal after source changes
  • Cache-aside risks a stampede when a hot key expires; mitigate with a lock or early refresh
  • Invalidate a cached item on write by deleting or updating its key (DEL/UNLINK)
  • maxmemory-policy selects which keys are evicted when the cache reaches its memory limit
  • noeviction returns errors on writes once memory is full instead of dropping keys
  • Keyspace notifications can publish key-change and expiry events to drive external invalidation

Unlock with Premium — includes all practice exams and the complete study guide.

References

  1. Caching guidance Well-Architected
  2. What is Azure Managed Redis?
  3. Azure Managed Redis architecture
  4. Use Microsoft Entra for cache authentication with Azure Managed Redis
  5. Quickstart: create a Python app with Azure Managed Redis
  6. Connect redis-py to Azure Managed Redis
  7. Best practices using client libraries with Azure Managed Redis
  8. Multi-key operations
  9. SET command reference
  10. EXPIRE command reference
  11. TTL command reference
  12. Redis keyspace notifications
  13. Best practices for memory management for Azure Managed Redis
  14. Cache-Aside pattern Well-Architected
  15. UNLINK command reference
  16. Redis cache-aside with redis-py
  17. Enable Redis keyspace notifications in Azure Managed Redis (preview)