A developer uses lazy loading with Amazon ElastiCache for a catalogue service. Because items are only refreshed on a cache miss, an item that changes in the database can remain stale in the cache indefinitely if it keeps being read. The developer wants a simple way to bound how long any cached item can be out of date, without changing the write path. Which change achieves this?
- ASet a time to live on each cached item so it expires after a fixed interval and the next read reloads the current value from the database. Correct
- BIncrease the time to live on each cached item to a much larger value so the cache retains items longer and serves more reads from memory.
- CRemove any time to live so cached items never expire and the application avoids the cost of repeatedly reloading items from the database.
- DSwitch reads to use a conditional get that compares a stored version attribute and reloads only when the database version is newer.
Why A is correct: A time to live forces each cached item to expire after a fixed interval, so the next read misses and reloads fresh data, capping how long a stale item can be served without touching the write path.
Why B is wrong: A larger time to live keeps items in the cache longer and so increases, rather than bounds, the maximum staleness, which is the opposite of the stated requirement.
Why C is wrong: Removing expiry means a stale item under lazy loading can live in the cache forever while it keeps being read, which is exactly the staleness the developer wants to bound.
Why D is wrong: ElastiCache get operations do not perform a server-side version comparison against the database, so this adds a read to the database on every request and defeats the cache.