Interview Databases & Caching

What is a TTL, and how do you choose one for cached data?

Databases & Caching · Advanced level

Answer

TTL is the expiration time for a key. I choose it from freshness needs, update frequency, miss cost, database capacity, and user impact of staleness, and I add jitter to avoid synchronized expiry.

Technical explanation

LRU approximates least recently used; LFU approximates least frequently used; volatile policies protect non-TTL keys but can fail writes if no TTL keys are eligible.

TTL jitter prevents synchronized expiry and thundering herds.

Memory alerts must fire before maxmemory to avoid eviction storms or write failures.

Hands-on example

Policy example for pure cache:

maxmemory-policy allkeys-lfu

SETEX product:v2:123 642 <json>

642 seconds = 600 base + random jitter.

Incident checks:

INFO memory

INFO stats

CONFIG GET maxmemory-policy

Watch evicted_keys, keyspace_hits, keyspace_misses, app cache errors, and RDS fallback load.

Preparing for an interview?

Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.

More Databases & Caching interview questions

← All Databases & Caching questions