4.3 Dynamic Scalability Through Caching
Key Takeaways
- A multi-tier caching architecture deploys caching at the edge (CloudFront), in application memory, and at the database layer (ElastiCache) to offload traffic and minimize read latency.
- ElastiCache for Valkey/Redis supports advanced data structures, persistence, replication, and horizontal write sharding with Cluster Mode Enabled, whereas Memcached provides a simple, multithreaded in-memory key-value store without replication.
- Cache-Aside (Lazy Loading) populates cache on demand with TTL expiration, Write-Through updates cache and database synchronously, and Write-Around avoids cache churn by writing directly to the database.
- Cache stampedes (thundering herd) occur when expired keys cause concurrent database surges, mitigated by distributed mutex locking (Redis SETNX) and probabilistic early expiration.
- EngineCPUUtilization measures the single-threaded Redis execution core and detects engine saturation that host-level CPUUtilization obscures, while SwapUsage must remain near zero to prevent severe latency spikes.
4.3 Dynamic Scalability Through Caching
Scaling compute nodes and relational databases horizontally or vertically introduces steep cost curves and architectural bottlenecks. Relational databases face finite connection pools and disk I/O ceilings when hit with intense read spikes. Distributed caching absorbs high-frequency queries, flattens database loads, and delivers sub-millisecond response times. For the CloudOps exam, engineers must master multi-tier caching architectures, Amazon ElastiCache engine internals (Valkey/Redis vs. Memcached), caching patterns, eviction strategies, and CloudWatch performance metrics.
Multi-Tier Caching Architecture
Enterprise cloud architectures deploy caching across three distinct infrastructure tiers:
- Edge Tier (Amazon CloudFront): Content Delivery Network (CDN) with hundreds of Points of Presence (PoPs) globally. CloudFront caches static assets (images, stylesheets, media) and cacheable REST/GraphQL responses using HTTP
Cache-Controlheaders, terminating requests closest to users. - Application Tier (Local In-Memory Cache): Ephemeral process memory (e.g., local heap maps). Delivers nanosecond lookups but remains isolated per instance or container, creating cache synchronization challenges across auto-scaled fleets.
- Database Tier (Amazon ElastiCache): Centralized, distributed in-memory data store shared across all application instances. Delivers sub-millisecond read latency, shields primary relational databases from query spikes, and provides centralized session management.
Amazon ElastiCache Engine Comparison: Valkey / Redis OSS vs. Memcached
Amazon ElastiCache offers managed engines based on Valkey / Redis OSS and Memcached:
| Architectural Feature | ElastiCache for Valkey / Redis OSS | ElastiCache for Memcached |
|---|---|---|
| Data Structures | Complex: Strings, Hashes, Lists, Sets, Sorted Sets (ZSET), Streams | Simple key-value store (strings and serialized objects) |
| Threading Model | Single-threaded core execution loop (I/O offloaded to helper threads) | Fully multithreaded (scales vertically on multi-core CPUs) |
| High Availability | Primary-replica replication with Multi-AZ automated failover | No native replication; independent standalone nodes |
| Persistence & Backup | Disk persistence via RDB point-in-time snapshots and AOF logs | Pure in-memory; no persistence, backup, or restore |
| Horizontal Scaling | Partitioned sharding via Cluster Mode Enabled (up to 500 shards) | Client-side consistent hashing across nodes |
| Advanced Features | Pub/Sub messaging, Geospatial indexes, and Lua scripting | Simple key-value caching only |
Redis Cluster Modes
- Cluster Mode Disabled: Consists of 1 primary node and up to 5 read replicas in a single shard. All writes route strictly to the primary node. While read capacity scales horizontally across replicas, write throughput is strictly bounded by the single primary, and dataset size cannot exceed a single node's memory.
- Cluster Mode Enabled: Partitions data across 1 to 500 shards using 16,384 internal hash slots. Each shard contains 1 primary and up to 5 read replicas, enabling horizontal scaling of both read and write capacity across multi-terabyte datasets.
Caching Patterns & Invalidation Strategies
Applications interact with distributed caches using three primary patterns:
1. Cache-Aside (Lazy Loading)
The application directly orchestrates cache lookups:
- Application queries the cache. On a cache hit, it returns data immediately.
- On a cache miss, the application queries the database, writes the result to the cache, and returns it to the client.
- Trade-offs: Resilient against cache outages (falls back to database); caches only requested keys. However, misses incur three network round-trips, and data can become stale if updated directly in the database.
- Mitigation: Configure an explicit Time to Live (TTL) on all keys to force periodic re-fetching.
2. Write-Through
The application writes data to the cache and the persistent database simultaneously:
- Trade-offs: Cache data is always fresh, eliminating read-time database misses. However, write latency increases because writes must succeed across two systems, and memory can be flooded with data that is rarely read. Usually combined with eviction policies or TTLs.
3. Write-Around
Data is written directly to the database without populating the cache. Data enters the cache only upon a subsequent cache miss via Cache-Aside. This prevents cache pollution from high-volume write operations that are seldom read (e.g., historical audit logs).
Eviction Policies & Thundering Herd Mitigation
When memory reaches maxmemory, ElastiCache invokes an eviction policy to remove existing keys:
volatile-lru/allkeys-lru: Evicts Least Recently Used keys (either with explicit TTL or across all keys).volatile-lfu/allkeys-lfu: Evicts Least Frequently Used keys.noeviction: Returns an out-of-memory error on write operations when memory is full.
Mitigating Cache Stampede (Thundering Herd)
A cache stampede occurs when a heavily queried key expires, causing thousands of concurrent threads to hit the backend database simultaneously on cache miss. CloudOps engineers mitigate stampedes using:
- Distributed Mutex Locking: Threads attempt to acquire a distributed lock in Redis (using
SET key value NX PX 10000). Only the single thread obtaining the lock queries the database and refreshes the cache; all other threads wait or consume stale cached values. - Probabilistic Early Expiration (XFetch): The application calculates randomized early expiration windows, refreshing keys in the background before the hard TTL expires.
Monitoring ElastiCache with CloudWatch
CloudOps engineers must track critical ElastiCache CloudWatch metrics:
EngineCPUUtilization: Critical for Redis / Valkey. Because Redis executes commands in a single-threaded loop, a 4-vCPU instance shows 25% hostCPUUtilizationwhen its Redis engine core is 100% saturated. Always monitorEngineCPUUtilizationto detect Redis bottlenecks. For Memcached, hostCPUUtilizationaccurately reflects multi-threaded CPU usage.SwapUsage: Indicates OS memory paging. In an in-memory database, swapping causes severe latency spikes (from microseconds to milliseconds).SwapUsagemust not exceed 50 MB.Evictions: Spikes indicate memory exhaustion where valid keys are being dropped, requiring cluster scale-up or shard addition.CurrConnections: Spikes indicate connection pooling failures or application connection leaks.
ElastiCache supports Application Auto Scaling to automatically add or remove read replicas or scale shards based on EngineCPUUtilization and memory metrics.
A CloudOps engineer notices that an Amazon ElastiCache for Redis (Cluster Mode Disabled) primary node exhibits severe latency spikes during peak hours. The overall host CPUUtilization metric reported in CloudWatch shows only 26%, but application requests to the cache are timing out. Further inspection reveals that the host instance type has 4 vCPUs. What is the root cause of this performance degradation, and how should it be monitored?
An e-commerce platform experiences sudden database connection crashes during flash sales when popular product catalog cache keys expire. Tens of thousands of concurrent client requests encounter cache misses at the exact same millisecond and concurrently query the Amazon RDS MySQL primary database, exhausting its connection pool. Which architectural pattern directly mitigates this thundering herd phenomenon?
An operations team needs to choose between Amazon ElastiCache for Valkey/Redis and Amazon ElastiCache for Memcached for an analytics dashboard session store. The requirements mandate horizontal write scalability across multiple partitions, automatic failover with zero manual intervention if a node fails, and in-memory sorted sets for real-time leaderboards. Which configuration meets all requirements?