Database caching, invalidation, and source-of-truth boundaries
Integrate database caching only after naming the source of truth, repeated access pattern, cache key, staleness tolerance, invalidation event, miss behavior, and failure safety.
LESSON GOALS
By the end, you can
- Select cache location, key, TTL, invalidation, and consistency behavior from the read/write pattern.
- Distinguish cache acceleration from database durability, write scaling, and connection management.
01
Define correctness before hit ratio
Identify the authoritative store and divide reads by staleness tolerance. Product descriptions may tolerate minutes; stock reservations and revocations may require current reads. Define cache key, tenant and authorization dimensions, TTL, expected update rate, and the exact invalidation trigger. If these facts are unknown, a cache can make a fast wrong answer. Measure baseline query latency, repeated-read ratio, and database load so the cache has a testable purpose.
Cache-aside loads the database on a miss and writes the result to cache. It is simple but permits stale values and stampedes unless writes invalidate and misses are coalesced or rate-limited. Write-through updates cache on the write path but adds latency and failure coordination. TTL limits age; it does not guarantee freshness immediately after a write. Versioned keys or event-driven invalidation can narrow stale windows, but failure handling must be specified.
02
Choose cache location and integration
CloudFront or API caching belongs near HTTP when whole responses are safely cacheable. ElastiCache is a general in-memory cache or data store for application objects, sessions, counters, and non-DynamoDB sources. DAX is DynamoDB-compatible acceleration for repeated eventually consistent reads where its integration and latency fit. DAX is not for strongly consistent reads and does not repair write-hot keys. RDS Proxy is not a cache: it pools relational connections. Read replicas execute database queries on replicated data; they are not key-value object caches.
Choose key granularity to avoid cross-tenant leaks and hot keys. Size memory for working set plus overhead, choose eviction behavior, and monitor hit ratio, latency, evictions, connection count, and source load. Multi-node deployment can improve cache availability, but asynchronous cache copies and eviction reinforce why durable truth remains elsewhere.
03
Plan miss, invalidation, and outage paths
On cache outage, bounded fallback may query the source, but unlimited fallback can collapse the database. Use request coalescing, concurrency limits, and degraded responses where business rules permit. On source outage, serve stale content only for explicitly safe classes. Writes should either invalidate after authoritative commit or use a design that explains ordering; invalidating before a failed database write can cause needless misses, while failing to invalidate after success serves stale data.
Security includes encryption, network controls, authentication where supported, and keys that include authorization context. Cost includes nodes or serverless cache capacity and engineering complexity against saved database work. A low hit ratio or high write rate can make caching negative value. Validate with a write/read race, node restart, cold cache, and source slowdown. Caching is a performance integration, not durability, write scaling, connection management, or a universal rescue for poor queries.
Treat cache correctness as a state machine with four observable events: authoritative write starts, commit succeeds, invalidation is published, and the next reader misses or receives the new version. Test reordered and duplicated invalidations, not just the happy path. A version number in the cache key or value lets the application reject an older object after a race, while request coalescing prevents thousands of misses from becoming thousands of database queries. Security requires that a cache key include tenant and authorization-relevant dimensions whenever representation differs; encryption cannot repair a shared key that leaks another tenant's object. Compare memory capacity, replicas, network transfer, and invalidation engineering with the database CPU actually removed. If campaign reads are mostly unique, the low hit ratio makes ElastiCache a loser and query or index work becomes the likely next step.
DECISION TABLE
Compare the architecture constraints
| requirement | choose | because | reject |
|---|---|---|---|
| Repeated general application objects | ElastiCache cache-aside | General keys, TTL, and invalidation are controlled | DAX is DynamoDB-specific; Proxy pools connections |
| Repeated eventually consistent DynamoDB reads needing very low latency | DAX | DynamoDB-compatible read acceleration fits | ElastiCache requires custom integration |
| HTTP object or response shared globally | CloudFront/API cache layer | Caching occurs near request front door | Database cache alone does not edge-distribute |
| Connection churn with low query repetition | RDS Proxy | Connections, not result reads, are constrained | ElastiCache targets data caching |
GUIDED WORKED SCENARIO
Reason from the stated constraints
This is a guided example, not a question to memorise. The knowledge check uses new situations to test the same reasoning.
A catalog is 95% reads at 8,000 requests/s. Descriptions may be stale for five minutes, but inventory must reflect reservations immediately. Database CPU reaches 80% during campaigns.
Requirements
- Reduce repeated description queries
- Never serve stale reservation availability
- Prevent cold-cache stampede
- Keep the relational database authoritative
Reasoning path
- Define separate keys and policies for descriptions and inventory.
- Use cache-aside for descriptions with five-minute TTL and invalidation on edits.
- Read inventory from authoritative transaction path or a strictly correct design.
- Coalesce misses and cap fallback concurrency.
Why it wins
- It removes the repeatable safe reads causing load.
- It does not trade inventory correctness for hit ratio.
Why the alternatives lose
It serves stale inventory after reservations.
Becomes correct when: Inventory is informational and explicitly tolerates that staleness.Connections were not identified as the bottleneck and Proxy does not cache results.
Becomes correct when: Connection churn is the measured constraint.The source is relational and DAX is DynamoDB-specific.
Becomes correct when: The source becomes DynamoDB and repeated eventually consistent reads need DAX integration.ARCHITECTURE VISUAL · TEXT MODE
Inspect the model
A cache timeline orders commit, invalidation, miss, refill, node loss, and source fallback for descriptions and inventory separately.
saa-viz-service-choice-labKNOWLEDGE CHECK
Answer one question at a time
Pass every question to complete this lesson. Each answer includes reasoning before you continue.
What must every cache key include for personalized data?
Reveal reasoning
All dimensions affecting response, including tenant and authorization context.
What is the safe cold-cache response to 10,000 simultaneous misses?
Reveal reasoning
Coalesce or cap fallback; do not let all requests hit the source.
Change one decisive constraint: Inventory is informational and the product owner explicitly accepts values up to five minutes stale. Which architecture now wins, and what observable result proves the reversal?
Reveal reasoning
The cache winner reverses to five-minute caching for the complete catalog response. The proof is: A reservation followed by a cache hit can show old inventory for no more than five minutes without violating policy.
Before reveal in saa-viz-service-choice-lab, predict the first failed gate for the original database caching, invalidation, and source-of-truth boundaries case, then trace the observed branch and minimal repair.
Reveal reasoning
Commits to an outcome first, follows the visual gate order, separates routing or control state from business response, and repairs the earliest violated requirement.
PRIMARY SOURCES
Verify the claims
- primaryWhat is Amazon ElastiCache?aws-db-elasticache · retrieved 2026-07-29
- primaryIn-memory acceleration with DynamoDB Accelerator (DAX)aws-dynamodb-dax · retrieved 2026-07-30
- primaryWhat is Amazon DynamoDB?aws-db-dynamodb · retrieved 2026-07-29
- primaryAmazon RDS Proxyaws-rds-proxy · retrieved 2026-07-30