Why set an expiry on cached values?
Why B is the answer
So that stale data is removed in time
TTLs limit how long stale data can be served.
In detail
- An expiry removes stale data in time.
- Choose the expiry from how fresh the data must be.
Use case
An illustrative scenario, not a real incident.
A price list changes each day, so cached prices expire within hours, and users do not see stale values for long.
See it in different ways
flowchart LR
A["Request"] --> B{"In Redis?"}
B -->|Yes| C["Return cached value"]
B -->|No| D["Read the database"]
D --> E["Store in Redis"]
E --> CsequenceDiagram
participant A as App
participant R as Redis
participant S as Source
A->>R: Get the value
alt Hit
R-->>A: Value
else Miss
A->>S: Fetch or compute
A->>R: Store, with an expiry
endstateDiagram-v2 state "Cached" as s0 state "Served" as s1 state "Expired" as s2 [*] --> s0 s0 --> s1: hit s1 --> s0 s0 --> s2: time passes s2 --> [*]
Key point: Expiry removes stale data.
Why the other options are wrong
- A. So that each key name becomes shorter
- It does not change key names.
- C. So that Redis can store more than its memory allows
- Expiry does not add capacity.
- D. So that the database runs on fewer disks
- It has no effect on the database's disks.

