Cloud Design Patterns (System Design #16)
By now you have met the building blocks. But experienced architects do not rediscover solutions under pressure; they recognize problems and reach for known patterns. “The legacy system must stay up while we replace it” has a name — Strangler Fig. “Reads and writes have wildly different shapes” has a name — CQRS. Naming the pattern turns a vague design debate into a known problem with known trade-offs.
This chapter is a catalog. You do not need to master every pattern — the roadmap itself says getting an overview is enough — but you should know what each solves and when it applies. They are grouped by the problem they address.
Design and implementation patterns
These shape how services are structured and deployed.
- Sidecar. Attach a helper process alongside each service instance to handle cross-cutting concerns (logging, proxying, TLS, config) without changing the service. The basis of service meshes.
- Ambassador. A sidecar that acts as a proxy for outbound calls, handling retries, circuit breaking, and routing transparently.
- Anti-Corruption Layer (ACL). An adapter that translates between your clean domain model and a legacy or foreign system, so the mess does not leak inward. Essential in microservice and migration work.
- Strangler Fig. Replace a legacy system incrementally by routing more and more traffic to new services until the old one is dead. The safe migration pattern.
- Backends for Frontends (BFF). A dedicated backend per client type (web, mobile) that tailors responses, instead of one general API serving everyone poorly.
- Gateway Routing / Aggregation / Offloading. Route requests to the right backend; aggregate several backends into one response; offload cross-cutting concerns (auth, TLS, caching) from services to the gateway.
- External Configuration Store. Keep configuration outside the deployable so it can change without redeploying.
- Compute Resource Consolidation. Pack multiple tasks/tenants onto shared compute for cost efficiency (with isolation to avoid the noisy-neighbor problem).
- Leader Election. Choose one instance to coordinate a task (a singleton job, a scheduler) so duplicated instances do not conflict.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=1.55cm, minimum height=0.7cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=gray!10] (lb) at (-3.6,0) {router};
\node[b, fill=blue!10] (old) at (-0.9,0) {legacy};
\node[b, fill=green!12] (new) at (1.9,0) {new service};
\draw[a] (lb) -- node[above, font=\scriptsize] {shrinking} (old);
\draw[a] (lb) -- node[below, font=\scriptsize] {growing} (new);
\node[font=\rmfamily\small, text=black!60, anchor=north] at (-0.9,-0.7) {Strangler Fig: strangle the legacy gradually};
\end{tikzpicture}
Messaging patterns
These govern how services exchange work and events (Chapter 13).
- Competing Consumers. Multiple consumers pull from one queue to parallelize a backlog.
- Publisher/Subscriber. One event, many independent subscribers; fan-out.
- Queue-Based Load Leveling. Put a queue between a producer and a consumer to smooth spikes and protect a fragile backend.
- Priority Queue. Consumers take high-priority messages first, so urgent work is not stuck behind bulk work.
- Pipes and Filters. Decompose processing into a pipeline of independent steps, each a filter; compose and reuse.
- Async Request-Reply. Decouple a request from its response: return a token, deliver the result later (Chapter 6’s job-id pattern).
- Choreography. Services react to events without a central orchestrator. Flexible and decoupled, but harder to trace than orchestration.
- Claim Check. Pass a reference (a claim check) to a large payload stored elsewhere, instead of stuffing the message with data.
- Sequential Convoy. Preserve ordering for related messages while processing unrelated streams concurrently (partition by key).
- Scheduler Agent Supervisor. Distribute and monitor tasks across workers, with a supervisor handling failures and reassignment.
Data management patterns
These address how data is stored, read, and kept consistent (Chapters 10–12).
- Cache-Aside. Load on demand, invalidate on write (Chapter 12).
- CQRS (Command Query Responsibility Segregation). Separate the write model from the read model. Writes go through a normalized transactional store; reads use a denormalized, query-optimized store, kept in sync via events. Powerful when read and write shapes diverge; adds eventual consistency and sync machinery.
- Event Sourcing. Store the sequence of events as the source of truth, and derive current state by replaying them. Gives a complete audit log and time travel; costs event versioning and eventual consistency.
- Materialized View. Precompute an expensive query into a table, refreshed periodically, for fast reads.
- Index Table. Maintain a table of pointers for queries that the primary store cannot serve efficiently.
- Sharding. Partition data by key (Chapter 11).
- Static Content Hosting. Serve static assets from object storage/CDN, not the app (Chapter 7).
- Valet Key. Give a client a limited, time-boxed credential to access a resource (e.g. upload directly to blob storage) without proxying through your service.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=1.8cm, minimum height=0.7cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=green!12] (cmd) at (-3.2,0.9) {command (write)};
\node[b, fill=blue!10] (wm) at (-0.8,0.9) {write model};
\node[b, fill=orange!16] (q) at (1.8,0.9) {read model};
\node[b, fill=cyan!15] (query) at (1.8,-0.9) {query (read)};
\draw[a] (cmd) -- (wm); \draw[a] (wm) -- node[above, font=\scriptsize] {events} (q);
\draw[a] (q) -- (query);
\node[font=\rmfamily\small, text=black!60, anchor=north] at (-0.4,-1.4) {CQRS: separate shapes, sync by events};
\end{tikzpicture}
Choosing patterns without over-applying
Patterns are tools, not a badge of sophistication. The failure mode is pattern soup: applying Event Sourcing + CQRS + a service mesh + a saga to a CRUD app with a thousand users. Every pattern adds operational cost, failure modes, and cognitive load.
The three questions that keep you honest:
- What measured problem does this solve? If you cannot name one, do not add it.
- What is the simplest thing that could work? A monolith with a well-indexed database beats a distributed event-sourced system for most products.
- Can we operate it? If nobody on the team has run CQRS or a service mesh in production, adopting it is a liability.
Use the catalog to recognize problems and communicate solutions, then adopt the smallest pattern that fixes the actual bottleneck.
Pitfalls
- Pattern soup. Layering patterns for their own sake multiplies complexity without solving a real problem.
- Event Sourcing without versioning. Events are permanent; changing their schema later breaks replay. Version events from the start.
- CQRS without a sync plan. The read and write models drift; you need events, reconciliation, and a consistency stance (Chapter 4).
- Sidecars per service without a control plane. Dozens of independent sidecars are unmanageable without a mesh to configure them.
- Strangler without a router. You must be able to shift traffic by route or percentage, or the migration stalls.
- Ignoring the operational bill. Every pattern is also an on-call responsibility. Budget attention, not just code.
What problem does CQRS solve, and what does it cost? When is Strangler Fig the right migration approach?
ASolution
CQRS separates read and write models so each can be optimized (normalized transactional writes, denormalized fast reads); it costs eventual consistency and syncing machinery, often via events. Strangler Fig is right when replacing a live legacy system: route traffic gradually to new services until the old one can be retired, avoiding a risky big-bang cutover.
Next: Reliability, Resiliency and Security (System Design #17) — keeping it up under failure, and keeping the wrong people out.