Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Application Cache
HLD

Application Cache

Redis/Memcached in your service path — the workhorse cache tier, its data shapes, and operational realities.

The Workhorse Tier

 application cache = explicit key-value store in your service path:
   Redis / Memcached / managed equivalents

 [app] ──GET user:912──► [ Redis ] ──hit: 0.5ms──► return
            │ miss

         [ Postgres ] ──45ms──► SET user:912, TTL 300s ──► return

 vs browser/CDN layers: this one YOU program — keys, values,
 TTLs, invalidation are all your code's decisions.

What Lives There

PatternExampleNotes
Entity cacheuser:912 → JSONThe bread-and-butter
Query resulttrips:city:nyc:activeCareful: invalidation pain
Computed objectfare:o:d:h:t → priceExpensive derivation saved
Session storesess:abc → user dataStatelessness enabler
Rate countersrl:user:912 (INCR)Atomic ops shine
Lockslock:trip:77 (SET NX)Distributed coordination
Hot liststrending (ZSET)Sorted-set superpowers

Redis vs Memcached

 Memcached: pure multi-threaded KV. dead simple. strings only.
 Redis    : single-threaded core (mostly), RICH structures:
            hashes, sorted sets, streams, pub/sub, Lua scripting,
            persistence options, replication/cluster

 default choice today: Redis — the data structures eliminate
 whole classes of "cache + side table" complexity.
 Memcached persists in legacy fleets and pure-string farms.

Data Structure Superpowers

 modeling IN the cache beats JSON blobs for hot paths:

 leaderboard:  ZADD board score user     / ZREVRANGE board 0 9
 sessions:     HSET sess:a field val     (partial updates)
 uniqueness:   SADD seen:day ids         (membership tests)
 rate limit:   INCR rl:u:912 + EXPIRE    (atomic counter+TTL)

 each replaces fetch-modify-write races with single atomic ops.

Operational Realities

 memory management:
 - set maxmemory + eviction policy (allkeys-lru usually)
 - TTL EVERYTHING unless deliberately persistent
 - watch fragmentation; big values hurt more than many small

 failure modes to plan:
 - cache restart = cold = origin stampede (warming lesson)
 - network blip to redis: fail OPEN (serve from source) or CLOSED?
   reads: open. auth-critical decisions: careful.
 - never let a cache outage take the site down;
   caches are accelerators, not dependencies... except sessions,
   which ARE dependencies — replicate those.

Sizing With Numbers

 hot-set estimate method:
   top N entities × avg object size × safety factor

 RideShare drivers online: 500k × 2KB ≈ 1GB working set
 active trips:             200k × 4KB ≈ 800MB
 sessions:                 2M   × 1KB ≈ 2GB
 ─────────────────────────────────────────────
 ~4GB hot set → one 8GB node with room; cluster later.

 sizing caches is THIS arithmetic, not vibes.

Interview Framing

“Add caching” scores when specific: engine chosen with reason (“Redis — need ZSETs for leaderboards”), key scheme shown, TTLs per entity class, size estimate computed, fail-open stance stated. Naming what you would NOT put there (source-of-truth financial state) completes the seniority picture.

My Private Notes

Notes are auto-saved locally to this device.