Markdown Editor

Caching Strategy Template (Cache-Aside, Invalidation, Stampede Control)

Per-data-class cache decision: placement, read/write pattern, hit ratio and memory sizing, TTL, invalidation, stampede control, cold start

सिस्टम डिज़ाइनमानकScaling Memcache at Facebook (NSDI 2013); RFC 9111; RFC 5861cachingstrategy
एडिटर में खोलें.md डाउनलोड करें

उपयोग कैसे करें: Use it when adding or changing a cache in front of a database or service. Fill the source capacity and minimum hit ratio first, then the invalidation path. The main pitfall is planning for a warm cache only, so a flush or cold region sends the full read load to the source.

प्रीव्यू

Caching Strategy: [Service or data domain]

Building-block decision record for adding or changing a cache in front of a database or service. It covers where the cache sits, the read and write pattern (cache-aside, read-through, write-through, write-behind), keys and time to live, invalidation, stampede control, cold start, failure modes and the metrics that prove the cache works. Concepts draw on "Scaling Memcache at Facebook" (Nishtala et al., NSDI 2013), on the probabilistic early recomputation analysis of Vattani et al. (VLDB 2015) and, for HTTP caches, on RFC 9111 HTTP Caching (June 2022) and RFC 5861. Replace every [bracketed] value and every row marked Example. Worked examples use Quillmere, an invented book-review and reading-list platform shared by the other building-block templates in this category.

Document control

FieldValue
Document ID[CACHE-REC-001]
Data class cached[Example: Book detail (title, authors, rating summary, cover URL)]
Source of truth[Example: catalog PostgreSQL primary, read replicas]
Cache technology and tier[Example: Shared in-memory key-value cluster, in-process L1 per pod]
Version[0.1]
Status[Draft / In review / Approved / Implemented / Superseded]
Owner (data)[@catalog-lead]
Owner (cache tier)[@platform-lead]
Reviewers[@sre], [@security]
Last updatedYYYY-MM-DD
Classification[Internal]
RelatedDistributed Cache System Design (system-design/design-distributed-cache), CDN System Design (system-design/design-cdn), Resilience and Load Shedding Design (system-design/resilience-load-shedding-design)

Approvals

RoleNameDecisionDate
Data owner[@name][Approved / Changes requested]YYYY-MM-DD
Cache tier owner[@name][Decision]YYYY-MM-DD
SRE[@name][Decision]YYYY-MM-DD

Revision history

VersionDateAuthorChange summary
0.1YYYY-MM-DD[@name]First draft
0.2YYYY-MM-DD[@name]Example: Switched from update-on-write to delete-on-write after a stale rating incident

1. Problem and goal

[Problem statement. Example: Book detail reads peak at 40,000 requests per second during evening hours. The catalog database can serve about 3,000 reads per second at an acceptable p99 latency. Without a cache the page cannot meet its 150 ms p99 target and the database saturates.]

GoalTargetHow measured
Example: Protect the sourceDatabase reads at or below 3,000 per second at peakDatabase query rate dashboard
Example: LatencyBook detail p99 at or below 150 msService latency histogram
Example: FreshnessRating summary at most 10 minutes stale, title and authors at most 60 seconds after an editStaleness probe (section 9)
[Goal][Target][Measure]

2. Placement

LayerHoldsTTLInvalidated byNotes
Example: Browser and CDN (HTTP cache)Rendered public book pagemax-age=60, stale-while-revalidate=30Expiry onlyPublic data only; never personalised fields
Example: In-process L1 per podBook detail object5 sExpiry onlyAbsorbs hot keys; short TTL bounds staleness
Example: Shared cache cluster (L2)Book detail object600 s with plus or minus 10% jitterDelete on write (section 5)Main protection for the database
[Layer][Data][TTL][Mechanism][Notes]

For HTTP caches, the freshness model, max-age, s-maxage, no-store, private and serving stale responses are defined in RFC 9111 (sections 4.2 and 5.2); stale-while-revalidate and stale-if-error are defined in RFC 5861 (sections 3 and 4). Check that every proxy and CDN in the path honours the directives you rely on.

3. Read and write pattern

PatternHow it worksGood forRisks
Cache-aside (look-aside)Application reads cache, on miss reads source and fills cache; on write updates source then deletes the keyMost read-heavy dataRace between a slow fill and a delete can leave stale data; mitigate with leases or versioned sets
Read-throughCache library loads from source on missUniform loaders, fewer code pathsHides source latency from callers; loader must be idempotent
Write-throughWrite goes to cache and source in the same request pathData read right after writeWrite latency is the sum of both; partial failure leaves the two out of step
Write-behind (write-back)Write goes to cache, flushed to source laterCounters, high write rates where loss is acceptableAcknowledged writes lost if the cache node fails before flush
Refresh-aheadCache refreshes popular keys before they expirePredictable hot keysWasted work on keys that are no longer read

Decision for this data class: [Pattern and reason. Example: Cache-aside with delete-on-write for book detail. Writes are rare (about 200 per second) and correctness after an author edit matters more than the cost of one extra miss.]

4. Capacity and hit ratio

InputExample valueSource
Peak reads40,000 per secondTraffic dashboard, last 30 days
Safe source reads3,000 per secondLoad test of catalog primary and replicas
Minimum hit ratio1 - 3,000 / 40,000 = 92.5%Derived
Target hit ratio95% (source sees 2,000 reads per second)Chosen with 1,000 reads per second margin
Hot working set2,000,000 keysDistinct books read in a 24-hour window
Average entry size2,048 B value plus about 100 B key and overheadSampled serialised objects
Memory for working set2,000,000 x 2,148 B = 4.30 GB (4.0 GiB)Derived
Memory with 30% headroom5.2 GiBDerived

Source load at other hit ratios, Example:

Hit ratioSource reads per secondWithin 3,000?
90%4,000No
92.5%3,000At the limit
95%2,000Yes
98%800Yes

A cold cache sends all 40,000 reads per second to the source, 13.3 times its safe capacity. Section 7 covers how to avoid that.

5. Keys, TTL and invalidation

Key format: [Example: book:v3:{book_id}. The v3 segment is the schema version; bumping it on a format change makes old entries unreachable without a mass delete.]

TTL: [Example: 600 s with plus or minus 10% random jitter, so entries written together expire between 540 s and 660 s instead of in one second.]

Invalidation path:

RuleExample decision
Who deletesA change stream reader on the database commit log, not the writing service, so every write path (admin tools, migrations, batch jobs) is covered
Delete retriesRetried with backoff until acknowledged; failures alert after 5 minutes
Race with a slow fillFills carry a lease token issued on the miss; a delete cancels outstanding leases, so a fill started before the delete is rejected
Bulk changesSchema change bumps the key version; no mass delete
RegionsEach region's change reader deletes in its own region's cache

The lease idea (a token on miss that a later delete invalidates) and commit-log driven deletes are described in the NSDI 2013 memcache paper. The commit-log approach is covered in more depth by the CDC Pipeline System Design template (system-design/design-cdc-pipeline).

6. Stampede and hot-key control

Example: the most popular book receives 5,000 reads per second and a fill takes 200 ms. Without protection about 5,000 x 0.2 = 1,000 requests reach the database for one key during each refill.

ControlHow it worksExample setting
Request coalescing (single flight)One request per key per process performs the fill; others wait for itOn in L1 and in the loader
Lease on missThe cache gives one client a lease to fill; others get "retry shortly" or a stale valueLease wait 10 ms, maximum 3 retries
Serve stale while refreshingKeep the expired value for a short grace period and refresh in the background30 s grace
Probabilistic early refreshEach read refreshes early with a probability that grows as expiry approaches, so one reader usually refreshes before expiry (Vattani et al., VLDB 2015)Beta 1.0, recompute time stored with the value
TTL jitterSpread expiry of entries written togetherPlus or minus 10%
Hot-key replicasCopy a very hot key under several suffixes or in L1L1 5 s for top 1,000 keys

A common form of early refresh, written as code:

on read(key):
  value, delta, expiry = cache.get_with_meta(key)
  # delta = seconds the last recompute took, beta >= 1.0 refreshes earlier
  if value is missing or now() - delta * beta * ln(random()) >= expiry:
      value = recompute_and_set(key)
  return value

ln(random()) is negative, so the subtraction moves the check point earlier by a random amount scaled by the recompute time.

7. Cold start and cache failure

ScenarioEffect without controlControlExample decision
New or flushed clusterSource sees full read loadWarm from a peer cluster or from logs of recent keys before taking trafficWarm the top 500,000 keys, then shift traffic in 10% steps
One cache node lostIts share of keys misses at onceSmall standby pool with short TTL, as in the NSDI 2013 gutter idea, or replicasStandby pool of 2 nodes, 10 s TTL
Whole cache downUp to 40,000 reads per second at sourceLoad shedding and a circuit breaker in front of the sourceShed non-logged-in traffic first; see system-design/resilience-load-shedding-design
Cache slow, not downTimeouts pile up threadsShort cache timeout, treat as miss with a rate cap20 ms timeout, fall back for at most 2,000 reads per second

8. Security and privacy

  • Shared HTTP caches receive only public responses; personalised responses carry Cache-Control: private or no-store (RFC 9111 section 5.2.2).
  • Cache keys for user data include the user or tenant ID, so one user cannot read another's entry.
  • Network access to the cache cluster is restricted to the services that need it, with authentication and transport encryption where supported.
  • Deleting a user's data (erasure request) deletes or expires their cached copies within a stated time: [Example: 10 minutes].
  • Secrets and tokens are not cached unless the cache is approved for that classification.

9. Metrics and alerts

MetricExample alert thresholdWhy
Hit ratio per data classBelow 93% for 10 minutesApproaches the 92.5% minimum from section 4
Source reads per secondAbove 2,700 for 5 minutes90% of safe capacity
Fill latency p99Above 300 msHerd size grows with fill time
Invalidation lag (commit to delete)Above 5 s p99Freshness target
Staleness probeProbe value older than targetWrites a known record and reads it back through the cache
Evictions per secondSudden rise of 3 times baselineWorking set grew or memory shrank

10. Decisions and open items

IDDecision or open itemReasonOwnerStatus
CD-01Example: Cache-aside with delete-on-writeSimple, fails safe, writes are rare@catalog-leadDecided
CD-02Example: Change stream drives deletesCovers every write path@platform-leadDecided
CD-03Example: Rating summary uses TTL only, no delete10-minute staleness accepted by product@productDecided
CD-04Example: Evaluate warm-up tooling for region failoverCold start is 13.3 times source capacity@sreOpen
[CD-NN][Item][Reason][@owner][Status]

Glossary and references

TermMeaning
Hit ratioShare of reads answered by the cache without reaching the source
TTLTime to live: how long an entry may be served before it expires
Stampede (thundering herd)Many concurrent misses on one key that overload the source
LeaseToken given to one client on a miss, allowing it to fill the key; invalidated by a later delete
Cold startPeriod when the cache is empty and the source sees most reads

References:

  • Nishtala R. et al., "Scaling Memcache at Facebook", 10th USENIX Symposium on Networked Systems Design and Implementation (NSDI '13), 2013, pages 385 to 398, https://www.usenix.org/conference/nsdi13/technical-sessions/presentation/nishtala (leases, commit-log invalidation, gutter pool, cold cluster warm-up).
  • Vattani A., Chierichetti F., Lowenstein K., "Optimal probabilistic cache stampede prevention", Proceedings of the VLDB Endowment 8(8), 2015, pages 886 to 897, https://doi.org/10.14778/2757807.2757813.
  • RFC 9111, HTTP Caching (STD 98, June 2022), https://www.rfc-editor.org/info/rfc9111.
  • RFC 5861, HTTP Cache-Control Extensions for Stale Content (Informational, May 2010), https://www.rfc-editor.org/info/rfc5861.
  • Related templates: Distributed Cache System Design (system-design/design-distributed-cache), CDN System Design (system-design/design-cdn), Partitioning and Sharding Guide (system-design/partitioning-sharding-guide), Replication and Consistency Models (system-design/replication-consistency-models), Back-of-Envelope Estimation (system-design/back-of-envelope-estimation).

Standard and licence note

Based on the publicly described techniques in the NSDI 2013 memcache paper and the VLDB 2015 stampede paper, and on RFC 9111 and RFC 5861 for HTTP caching semantics; no standard governs application cache design. The text, tables, numbers and examples are original to this collection, and Quillmere is an invented organisation. Check the current RFC status and the documentation of the cache software you adopt. Template licence: MIT.

संबंधित टेम्पलेट