Design Patterns
Circuit Breaker, Saga, CQRS, Cache-Aside and other distributed patterns
24 lessons
[PATTERN] Cache-Aside (Lazy Loading)
Caching strategy: cache hit/miss, write invalidation, thundering herd (cache stampede) problem
[PATTERN] Ambassador
Ambassador pattern: outgoing-only sidecar proxy. App emits plain HTTP/TCP to localhost; ambassador (Envoy/PgBouncer) adds mTLS, retries with jitter, circuit breaker, connection pooling, canary routing, and read/write splitting. Five scenarios: outbound retry, circuit breaker trip, mTLS upgrade with canary, Postgres pool multiplexing, and ambassador-vs-sidecar comparison. ADRs cover ambassador vs library vs full service mesh, and connection pool ownership.
[PATTERN] Strangler Fig Migration
Incremental monolith decomposition: proxy routing, CDC data sync, shadow traffic, phased migration
[PATTERN] Circuit Breaker
Prevent cascading failures: CLOSED -> OPEN -> HALF-OPEN state transitions with fallback responses
[PATTERN] Branch by Abstraction
Branch by Abstraction — pattern для refactoring без long-lived branches. Trunk-based dev через interface + feature flag.
[PATTERN] Refresh-Ahead
proactive cache refresh, single-flight mutex, probabilistic XFetch
[PATTERN] Load Balancing
L4 vs L7 load balancers, algorithms (round-robin, weighted, least-connections, P2C/Power of Two Choices, consistent hash, random), active+passive health checks with outlier detection, connection draining, GSLB via Anycast IP+BGP and GeoDNS. 5 scenarios: round-robin-naive, p2c-choice, health-check-evict, l4-vs-l7-routing, global-server-lb-anycast.
[PATTERN] Sidecar
Sidecar pattern: helper container in same pod (Envoy mTLS, Fluentd log shipping, Vault Agent secret injection, PID1 zombie anti-pattern)
[PATTERN] CQRS
CQRS pattern — separate write and read sides connected via event bus
[PATTERN] Priority Queue
Priority Queue pattern: SLA-driven scheduling. FIFO unfair, separate priority queues, starvation problem with weighted/aging fixes, Redis ZSET implementation.
[PATTERN] Dead-Letter Queue
Dead Letter Queue pattern: poison message handling, replay flow, alerting on DLQ. Multi-scenario animated explainer showing what happens without DLQ (consumer blocked), with DLQ (failed messages quarantined, processing continues), replay after fix, and silent-swallow antipattern.
[PATTERN] Sticky Sessions
Session affinity на load-balancer level: cookie-based routing, IP-hash NAT bug, hot-spot проблема при backend death, и stateless альтернатива через externalize state в Redis. 4 сценария показывают когда sticky нужны (WebSocket, in-memory session) и почему вредны для horizontal scale.
[PATTERN] Back-pressure
Back-pressure pattern: explicit flow control between producer and consumer. Demonstrates 4 scenarios — no back-pressure leading to OOM, Reactive Streams request(N) flow, TCP receive window back-pressure, and bounded queue with drop policy for metrics ingest.
[PATTERN] Timeout & Deadline
Каскадные падения при отсутствии timeouts и решение через hierarchical timeout/deadline budget propagation. Показано четыре сценария: cascade без timeouts, наивный per-hop timeout без иерархии, правильные hierarchical timeouts с убывающим бюджетом и deadline propagation через gRPC metadata с context cancellation.
[PATTERN] Write-Through and Write-Behind
Cache write strategies pattern — compare write-through, write-behind, write-around, and cache-aside race conditions side-by-side. Topology: client outside, group with cache (Redis) and database (Postgres). Four scenarios demonstrate different trade-offs between durability, latency, and consistency.
[PATTERN] Load Shedding
Load Shedding pattern: adaptive admission control, priority-based rejection, deadline-aware shedding. Compares unbounded queue meltdown vs graceful degradation under 10x traffic spike.
[PATTERN] API Gateway
Single entry point: rate limiting, JWT auth, routing, API composition (BFF pattern)
[PATTERN] Bulkhead
Isolate resources: separate thread pools per service prevent one slow service from taking down others
[PATTERN] Retry + Exponential Backoff
Handle transient failures: retry with 1s -> 2s -> 4s backoff, jitter, dead letter queue on max retries
[PATTERN] Transactional Outbox
Solve dual-write problem: DB + outbox in one transaction, relay publishes to Kafka, at-least-once delivery
[PATTERN] Two-Phase Commit (2PC)
Classic distributed transaction: Coordinator + 3 Participants, PREPARE/VOTE/COMMIT/ROLLBACK phases
[PATTERN] Saga Choreography
Event-driven saga without orchestrator: services communicate via Kafka event bus with compensating events
[PATTERN] Saga Orchestration
Central orchestrator coordinates distributed transaction: Order, Payment, Inventory, Shipping with compensating rollbacks
Event Sourcing / CQRS
CQRS pattern: Command side, Event Store, Kafka event bus, Read-side projections, cache, event replay