STACK
Ch 11 · Caching & Messaging
Chapter 11 · The async backbone

Caching & Messaging — the layer that turns "fast" into "scales"

11.1 · The cache tiers

TierExamplesTypical latency
ClientHTTP cache, SWR, IndexedDB0 ms
CDN / edgeCloudflare, Fastly, CloudFront10–50 ms
API gatewayKong, Apigee, Envoy filter5–20 ms
In-processLRU, Caffeine, lru-cache< 1 ms
SharedRedis, Memcached, Valkey~1 ms LAN

11.2 · Redis — beyond GET/SET

  • Types: strings, hashes, lists, sets, sorted sets, streams, bitmaps, geo, JSON, search.
  • Patterns: session store, leaderboard (ZSET), rate limit (INCR+EXPIRE), distributed lock (SETNX + Redlock with caveats), pub/sub, streams.
  • Persistence: RDB snapshots vs AOF; hybrid for safety.
  • HA: Sentinel + Cluster. Forks/alternatives: Valkey (Linux Foundation, post-Redis-license), Dragonfly, KeyDB.

11.3 · Cache pitfalls (and the fixes)

ProblemFix
Thundering herd on expiryJittered TTL; lock + refresh; stale-while-revalidate.
Cache penetrationNegative cache + bloom filter.
Cache stampede (cold)Pre-warm; single-flight.
InconsistencyInvalidate on write; CDC; short TTL.
Cache-only failureAlways fall back to source.

11.4 · Queue vs stream

QueueLog / stream
ConsumedOnce, then deletedMany consumers, independent offsets, replayable
ExamplesSQS, RabbitMQ, Redis BLPOPKafka, Redpanda, Kinesis, NATS JetStream

11.5 · Kafka / Redpanda

  • Topics → partitions → replicas. Ordering per partition.
  • Consumer groups with offset tracking; replay by resetting offsets.
  • Connect (CDC / sinks), Streams / kSQL for in-flight processing.
  • Redpanda: Kafka-API compatible, single binary, no ZK; lower op cost.

11.6 · NATS / JetStream

Tiny binary, sub-ms pub/sub; JetStream adds persistence + streams + KV + Object store. Sweet spot: microservice glue, IoT, edge.

11.7 · RabbitMQ

Classic AMQP broker. Exchanges (direct, fanout, topic, headers) + queues + DLX. Rich routing. RabbitMQ Streams adds Kafka-like log.

11.8 · Cloud-managed

ServiceWhat
AWS SQS / SNSQueue + pub/sub; FIFO variants.
AWS EventBridgeEvent bus with rules + schema registry.
GCP Pub/SubGlobal pub/sub with ordering keys.
Azure Service Bus / Event HubsAMQP + Kafka-compatible.
Cloudflare QueuesEdge queues bound to Workers.
Upstash / MomentoServerless Redis / Kafka.

11.9 · Durable execution

ToolNotes
TemporalThe flagship — write workflows in Go/Java/TS/Python/.NET; auto-resume after crashes.
InngestServerless durable execution; great DX for TS/JS.
Trigger.devBackground jobs + durable workflows.
RestateOSS durable runtime with virtual objects.
CadenceUber's predecessor of Temporal.

When to reach for it: any multi-step process that must complete eventually (onboarding, payments, sagas) but can take minutes / hours / days.

11.10 · Delivery patterns & semantics

PatternDefinition
At-most-onceSend and forget. Lossy.
At-least-onceDefault; duplicates possible → consumers must be idempotent.
Exactly-once (effective)Idempotent producers + transactions + consumer dedup. No magic.
OutboxDB write + outbox row in one tx; relay publishes.
Inbox / dedup tableConsumer records processed IDs.
SagaLong-running distributed transaction with compensations.
DLQDead-letter queue for poison messages.
BackpressureBounded queues + signal upstream.

11.11 · Self-check

Q1 · You need many services to consume the same events, each at their own pace, with the ability to replay history. Pick…
SQS
Kafka / log-style stream
Redis pub/sub
Direct HTTP fan-out