HELM
Ch 09 · Caching & Messaging
Chapter 09 · The async backbone

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

When the answer is "it's slow", the next question is "what can we cache?" When the answer is "it's coupled", the next question is "what can we make async?" This chapter is both.

9.1 · The five cache tiers

TierExamplesLatency
ClientHTTP cache, IndexedDB, SWR0 ms (already there)
CDN / edgeCloudflare, Fastly, CloudFront10–50 ms
API gatewayKong, Apigee5–20 ms
Application memoryLRU in-process, Caffeine, lru-cache< 1 ms (no network)
Shared cacheRedis, 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

ProblemFix
Thundering herd on expiryJittered TTL; lock + refresh; stale-while-revalidate.
Cache penetration (missing keys hammer DB)Negative cache + bloom filter.
Cache stampede on cold startPre-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 modesAlways fall back to source; never trust cache as source of truth.

9.4 · Queue vs stream — the core distinction

Queue message consumed once, then gone msg → worker → ack → delete SQS, RabbitMQ, Redis BLPOP Log / Stream append-only; many consumers replay independently offset-based; retain N days Kafka, NATS JetStream, Kinesis, Redpanda

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

ServiceWhat it is
AWS SQS / SNSQueue + pub/sub fan-out; FIFO variants for ordering.
AWS EventBridgeEvent bus with rules + schema registry; AWS-native pipelines.
GCP Pub/SubGlobal pub/sub; exactly-once with ordering keys.
Azure Service Bus / Event HubsAMQP + Kafka-compatible event ingestion.
Cloudflare QueuesQueues 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

PatternDefinition
At-most-onceSend and forget. Cheap, may lose messages.
At-least-onceDefault for most brokers. Duplicates possible → consumers must be idempotent.
Exactly-once (effective)Idempotent producers + transactions / dedup keys + idempotent consumers. No magic.
OutboxWrite to DB + outbox table in one tx; a relay publishes from outbox. Guarantees DB↔broker consistency.
Inbox / dedup tableConsumer records processed message IDs to drop duplicates.
SagaLong-running distributed transaction via choreography or orchestration. (Ch 11.)
Dead-letter queue (DLQ)Park unprocessable messages for inspection.
BackpressureSlow producer when consumer can't keep up; bounded queues.

9.11 · Self-check

Q1 · You need many independent services to consume the same events and be able to replay history. Best fit?
SQS queue
Kafka / log-style stream
Redis pub/sub
Direct HTTP webhooks