'Only two hard things: cache invalidation and naming things.' TTL is one dial. There are others. Here they are.

TTLexpire on timeWrite-through invalidatedelete on writeVersioned keysuser:v42:profilePub/subapp notifies cachesSimple + easyStrong consistencyAtomic swapsReal-timeStale until expiryFails at scaleMemory bloatBus complexity
Four invalidation strategies, four tradeoffs
Advertisement

TTL

Set expiry. Simple. Stale data until it expires. Right for slow-changing data.

TTL

Set expiry. Simple. Stale data until it expires. Right for slow-changing data.

Advertisement

Write-through invalidate

When you write to DB, delete the cache key. Next read repopulates. Works until you have 100 caches to invalidate.

Versioned keys

Key includes version: user:v42:profile. Update user? Bump version. Old key naturally expires. No delete needed.

Pub/sub invalidation

App publishes 'user 123 updated'. All caches subscribed, invalidate. Real-time + distributed.

Multi-level invalidation

L1 (local) + L2 (Redis). Invalidate both. Or use pub/sub to invalidate L1 when L2 updated.

TTL + write-through + versioning + pub/sub. Pick by consistency need vs cost.