๐Ÿช TechCookies
ConceptsPracticeSD challengesPricingNextJS Q&A
Log inStart free
๐Ÿช TechCookies
ConceptsPracticeSD challengesPricingPrivacyTerms
ยฉ 2026 TechCookies
Concepts
โ—ˆA/B Testing SystemsPro
โ—ˆAdaptive Bitrate Streaming (HLS/DASH)Pro
โ—ˆAnalytics PipelinesPro
โ—‰Append-Only Audit Tables
โ—ˆAsync Job Processing FundamentalsPro
โ—ˆAsync Processing PipelinesPro
โ—ˆAudio Streaming ArchitecturePro
โ—ˆAuthentication FundamentalsPro
โ—ˆAuthorisation and RBACPro
โ—‰Caching
โ—ˆCanvas and WebGL RenderingPro
โ—‰Back-of-Envelope Estimation
โ—‰CDN Caching
โ—‰Circuit Breakers
โ—‰Client-Side Caching
โ—ˆConsistent hashingPro
โ—ˆConflict-Free Replicated Data Types (CRDTs)Pro
โ—ˆCross-Tab SynchronisationPro
โ—‰Database Constraints as Data Guards
โ—‰Database Indexes: Making Queries Fast
โ—‰Relationships and Foreign Keys
โ—ˆDatabases & storagePro
โ—ˆDead-Letter QueuesPro
โ—ˆAt-Least-Once vs Exactly-Once DeliveryPro
โ—ˆDelta SyncPro
โ—ˆDistributed Payment ArchitecturePro
โ—ˆDrag-and-Drop ArchitecturePro
โ—ˆEnd-to-End Encryption in the BrowserPro
โ—ˆEdge Computing and CDN ArchitecturePro
โ—ˆMicrocontroller and Embedded ConstraintsPro
โ—‰Event-Driven Architecture
โ—‰Exponential Backoff and Retry
โ—‰Failure Modes in Distributed Systems
โ—ˆFile API and File System Access APIPro
โ—ˆFile Security and DeduplicationPro
โ—‰Frontend Architecture Patterns
โ—ˆGeolocation and Permissions APIPro
โ—ˆGeospatial QueriesPro
โ—‰Graceful Degradation and Fallbacks
โ—ˆGrid Layout and Widget SystemsPro
โ—‰Idempotency
โ—ˆIndexedDB for Browser-Side StoragePro
โ—‰Infinite Scroll and Pagination
โ—ˆIntersection Observer APIPro
โ—ˆIP-Based LocalisationPro
โ—ˆKafka Deep DivePro
โ—ˆLast-Write-Wins Conflict ResolutionPro
โ—‰Lazy Loading Images and Media
โ—‰Virtualisation and Large Lists
โ—‰Load balancing
โ—ˆMessage Queue FundamentalsPro
โ—ˆMessage queuesPro
โ—ˆObject Storage (S3-Compatible)Pro
โ—ˆLogging and ObservabilityPro
โ—ˆOffline-First ArchitecturePro
โ—ˆOffline-Capable Hardware SystemsPro
โ—ˆOperational Transform (OT)Pro
โ—ˆPassword SecurityPro
โ—ˆPayment Integration FundamentalsPro
โ—ˆPresence and AwarenessPro
โ—ˆProvider Adapter PatternPro
โ—ˆQueue-per-Channel PatternPro
โ—ˆRate Limiting AlgorithmsPro
โ—ˆRate Limiting in PracticePro
โ—‰Server State with React Query
โ—‰Read Replicas and Write Scaling
โ—ˆReal-Time Location TrackingPro
โ—ˆReal-Time State ManagementPro
โ—‰Redis Deep Dive
โ—‰REST API Design Principles
โ—ˆScheduled Jobs and CronPro
โ—‰Schema Design Fundamentals
โ—ˆServer-Sent Events (SSE)Pro
โ—ˆService WorkersPro
โ—ˆSQS and Managed QueuesPro
โ—ˆState Machines on the FrontendPro
โ—‰Stateless Service Design
โ—‰Synchronous vs Asynchronous Communication
โ—ˆThird-Party API ResiliencePro
โ—ˆThree-Way MergePro
โ—ˆTime-Series and Columnar DatabasesPro
โ—‰Vertical vs Horizontal Scaling
โ—ˆVideo Processing PipelinesPro
โ—ˆWeb Audio APIPro
โ—ˆWeb Crypto APIPro
โ—ˆWeb Workers for Heavy ComputationPro
โ—ˆWebhooks: Receiving External EventsPro
โ—ˆWebRTCPro
โ—ˆWebSocket ArchitecturePro
โ—‰WebSockets vs SSE vs Polling
โ—ˆWhen to Use NoSQLPro
โ—ˆWorker Pool PatternPro
โ—ˆYjs in PracticePro
โ—‰State Management with Zustand
SD challenges
โ†—children's drawing animation app โ€” minimize server farm costProโ†—competitive game scoring system under high loadProโ†—global cultural events platform with IP-based localizationProโ†—frontend data-fetching layer (Fetch API)Proโ†—infinite canvas for a design tool (Figma / Miro)Proโ†—location-aware store locator (Geolocation API)View all โ†’
System design โ€บ Concepts โ€บ Caching
โ—‰CachingFree~15 min ยท avg 64%
PerformanceRedisCDNDistributed systems
On this page:What is itKey conceptsAdvanced patternsPracticeSD challenges
What is Caching?

Caching stores frequently accessed data in fast storage so future requests skip the slow source โ€” cutting latency and reducing DB load. A cache hit returns data directly; a cache miss fetches from the source, then populates the cache for subsequent requests.

Cache Aside Pattern

Cache-aside (also called lazy loading) is the most common caching pattern. The application code owns the cache interaction โ€” the cache is never written to directly on a write, only on a read miss.

Read flow:

  1. Application checks cache for the key.
  2. Hit โ†’ return cached value immediately.
  3. Miss โ†’ fetch from DB, write the result into cache with a TTL, then return it.

Why it works: Only data that is actually requested gets cached, so the cache stays lean. Cold-start is the only downside โ€” the first request always hits the DB.

When to use it: Most read-heavy workloads: user profiles, product pages, feed items. It pairs well with Redis as the cache layer and any relational or document DB as the source of truth.

Trade-offs: Stale data is possible between the DB write and the next cache miss. Explicit invalidation (delete the key on write) or short TTLs mitigate this.

Write Strategies

How you handle writes determines whether the cache stays consistent with the database.

Write-through โ€” every write updates cache and DB synchronously before returning to the caller. Cache is always consistent. Writes are slower because both stores must acknowledge. Best when read-after-write consistency is critical (e.g., user settings, financial balances).

Write-back (write-behind) โ€” writes go to cache first; a background process flushes dirty entries to the DB asynchronously. Writes are fast, but a crash before the flush loses data. Best for high-write workloads where occasional loss is acceptable (metrics, counters, analytics).

Write-around โ€” writes go directly to the DB, bypassing the cache entirely. Cache is populated only on the next read miss. Prevents cache pollution for data that is written frequently but rarely re-read (bulk imports, audit logs). Increases read latency for the first access.

Rule of thumb: Write-through for correctness, write-back for throughput, write-around for write-heavy but read-cold data.

Cache Eviction Policies

When the cache is full, something must be evicted. The policy determines which entry goes.

LRU (Least Recently Used) โ€” evicts the entry that was accessed longest ago. Optimal for workloads where recent access predicts future access (sessions, user feeds, API responses). Redis implements LRU natively.

LFU (Least Frequently Used) โ€” evicts the entry accessed the fewest times over its lifetime. Better than LRU when a burst of one-time reads would otherwise pollute the cache. Heavier bookkeeping overhead.

FIFO (First In, First Out) โ€” evicts the oldest-inserted entry regardless of access pattern. Simple but often wrong โ€” a frequently-accessed entry inserted early gets evicted before a cold entry inserted recently.

TTL (Time To Live) โ€” entries expire after a fixed duration regardless of access. Not strictly an eviction policy but works alongside LRU/LFU to bound staleness. Always set a TTL; an unbounded cache grows until it exhausts memory.

Sizing tip: Aim for a cache hit rate above 90% for read-heavy systems. If hit rate is low, the cache is too small or the key space is too diverse โ€” consider a consistent hashing ring across multiple cache nodes.

CDN Caching

A Content Delivery Network (CDN) places cache nodes at the network edge, geographically close to users, so that static and cacheable dynamic content is served without a round-trip to the origin.

How it works:

  1. User requests https://cdn.example.com/app.js.
  2. Nearest edge node checks its local cache.
  3. Hit โ†’ file served from edge (< 10 ms).
  4. Miss โ†’ edge fetches from origin, caches the response, returns it to user.

Cache-Control headers tell CDNs (and browsers) how long to cache a response:

  • Cache-Control: public, max-age=31536000, immutable โ€” cache for one year, never revalidate. Use for content-hashed assets (app.a3f2bc.js).
  • Cache-Control: no-cache โ€” must revalidate with origin before serving (uses ETag/Last-Modified). Good for HTML pages.
  • Cache-Control: private โ€” browser may cache but CDN must not. Use for authenticated responses.

Cache busting: Append a content hash to the filename (app.a3f2bc.js) so a new deploy changes the URL, forcing the CDN to fetch the new file. Do not change TTLs โ€” keep them long; change URLs instead.

Invalidation / purge: CDNs support API-driven cache purges for emergency rollouts. This is expensive at scale โ€” prefer hash-based URLs over purging.

Cache Stampede Prevention

A cache stampede (thundering herd) occurs when a hot key's TTL expires and thousands of concurrent requests all miss the cache simultaneously, flooding the DB.

Why it happens: Every request that finds the expired key independently decides to fetch from DB and repopulate. Under high concurrency this sends N parallel DB queries for the same data.

Mutex lock (probabilistic lock): When a cache miss occurs, the first request acquires a lock (e.g., SET key:lock NX EX 2). All other requests either wait or serve the slightly stale value while the lock holder fetches and repopulates. Prevents the thundering herd at the cost of a brief lock-wait.

Probabilistic early refresh: Before the key actually expires, a small fraction of requests voluntarily refresh it early. As the TTL approaches zero the probability of triggering a refresh rises. No lock needed; works well for non-critical content.

Request coalescing: The cache layer (or a proxy like Nginx/Varnish) collapses concurrent identical requests into a single upstream fetch and fans the response out to all waiting clients. Transparent to the application.

Never set all keys to the same TTL โ€” stagger TTLs with a small random jitter (e.g., base_ttl + rand(0, 60)) so keys don't expire in synchronised waves.

โŠ› Cache-aside read flow
ClientrequestCacheRedisHIT โœ“MISSDatabasesource of truthpopulate cacheEvictionTTL / LRU
Write strategies
โ‡„ Write-through ยท Write-back ยท Write-around
Write-throughCacheDBConsistent, slower writesalways in syncWrite-backCacheDBFast writes, async flushrisk of data lossWrite-aroundCacheDBBypasses cache on writegood for infrequent data
Key concepts
TTL (time to live)โ†‘ view
Cache entries expire after a set duration, forcing a fresh DB fetch to prevent stale data.
LRU evictionโ†‘ view
Evict the least recently used entry when the cache is full โ€” keeps hot data in memory.
Cache stampedeโ†‘ view
Many requests hit DB simultaneously when a hot key expires. Fix: mutex lock or probabilistic early refresh.
CDN cachingโ†‘ view
Edge nodes cache content geographically close to users, reducing origin load and latency worldwide.
Advanced patterns
Consistent hashing
Distributes cache across nodes with minimal rehashing when nodes are added or removed.
Hot key problemโ†‘ view
One key overwhelms a single cache node. Fix: local in-process caches or replicate the key across nodes.
Write-throughโ†‘ view
Every write updates cache and DB synchronously โ€” consistent but slower on writes.
Write-backโ†‘ view
Writes go to cache first, DB updated asynchronously โ€” fast writes but risk data loss on crash.
๐Ÿ”’
Pro content
Advanced patterns and interview tips are on the Pro plan.
Unlock Pro โ†’
โŠ—
Ready to apply this?
Design a children's drawing animation app โ€” minimize server farm cost โ€” a challenge that uses caching heavily.
โ–ถ Attempt SD challenge
๐Ÿ“ NotesCaching
Sign in to save notes across sessions.