9.1 · The five cache tiers
| Tier | Examples | Latency |
|---|---|---|
| Client | HTTP cache, IndexedDB, SWR | 0 ms (already there) |
| CDN / edge | Cloudflare, Fastly, CloudFront | 10–50 ms |
| API gateway | Kong, Apigee | 5–20 ms |
| Application memory | LRU in-process, Caffeine, lru-cache | < 1 ms (no network) |
| Shared cache | Redis, Memcached, Valkey | ~1 ms LAN |
Always check the cheaper tier first; promote what's hot.
9.2 · Redis — beyond GET/SET
- Strings, hashes, lists, sets, sorted sets, streams, bitmaps, geo, JSON, search — Redis is a Swiss Army DB.
- Patterns: session store, leaderboard (ZSET), rate limiter (INCR+EXPIRE), distributed lock (SET NX + Redlock — with caveats), pub/sub.
- Persistence: RDB snapshots vs AOF. Hybrid for safety.
- Cluster + sentinel for HA. Valkey / Dragonfly / KeyDB as forks/alternatives with different licenses or perf characteristics.
9.3 · Cache pitfalls — and the fixes
| Problem | Fix |
|---|---|
| Thundering herd on expiry | Jittered TTL; lock + refresh; stale-while-revalidate. |
| Cache penetration (missing keys hammer DB) | Negative cache + bloom filter. |
| Cache stampede on cold start | Pre-warm; single-flight (one fetch per key, fan-out). |
| Inconsistency (DB updated, cache stale) | Invalidate on write; event-driven (CDC); short TTL. |
| Cache-only failure modes | Always fall back to source; never trust cache as source of truth. |
9.4 · Queue vs stream — the core distinction
9.5 · Kafka — the de-facto log
- Topics → partitions → replicas. Ordering is per partition, not topic.
- Consumers track offsets; replay by resetting offset.
- Throughput in millions of messages/sec; durable for days/weeks.
- Connect (CDC, sinks) + Streams / kSQL for in-flight processing.
- Redpanda — Kafka-API compatible, single-binary, no Zookeeper.
9.6 · NATS / JetStream
Tiny binary; sub-ms pub/sub; JetStream adds persistence + streams. Sweet spot: edge messaging, microservice glue, IoT.
9.7 · RabbitMQ
Classic AMQP broker. Exchanges (direct, fanout, topic, headers) + queues + DLX. Excellent for complex routing and per-message delivery semantics. RabbitMQ Streams adds Kafka-like log.
9.8 · Cloud-managed queues & events
| Service | What it is |
|---|---|
| AWS SQS / SNS | Queue + pub/sub fan-out; FIFO variants for ordering. |
| AWS EventBridge | Event bus with rules + schema registry; AWS-native pipelines. |
| GCP Pub/Sub | Global pub/sub; exactly-once with ordering keys. |
| Azure Service Bus / Event Hubs | AMQP + Kafka-compatible event ingestion. |
| Cloudflare Queues | Queues at the edge; integrates with Workers. |
9.9 · Durable execution — Temporal, Inngest, Trigger.dev
For long-running, retryable, stateful workflows (multi-day onboarding, multi-step payments, sagas), durable-execution platforms let you write workflow code that automatically resumes after crashes, replays deterministically, and retries with backoff. The replacement for hand-rolled state machines.
9.10 · Delivery patterns & semantics
| Pattern | Definition |
|---|---|
| At-most-once | Send and forget. Cheap, may lose messages. |
| At-least-once | Default for most brokers. Duplicates possible → consumers must be idempotent. |
| Exactly-once (effective) | Idempotent producers + transactions / dedup keys + idempotent consumers. No magic. |
| Outbox | Write to DB + outbox table in one tx; a relay publishes from outbox. Guarantees DB↔broker consistency. |
| Inbox / dedup table | Consumer records processed message IDs to drop duplicates. |
| Saga | Long-running distributed transaction via choreography or orchestration. (Ch 11.) |
| Dead-letter queue (DLQ) | Park unprocessable messages for inspection. |
| Backpressure | Slow producer when consumer can't keep up; bounded queues. |