06. Architecture: cheatsheets

4 lessons, one page each when printed.

System Design · 06. Architecture · 10 min · Intermediate

Monolith vs Microservices

One big program or many small ones: what each choice costs and when to split.

In short

A monolith is simple and fast to start: 0.006 ms for a checkout, one thing to run. Microservices give each team its own 5-minute release, and cost 12 ms per checkout, 99.6% uptime and harder debugging.

Draw boundaries along the business, give each service its own data, trace every request, and split only when the pain is real.

Key ideas

  • Good boundaries follow the business, so most changes touch only one service. (step 3)
  • Rule: only the service that owns the data reads or writes it. Everyone else uses its API or its events. (step 6)
  • Start with a monolith, keep modules clean, and extract a service when the pain is real and named. (step 9)

Good to know

  • A monolith is not a mistake. For a small team it is usually the fastest and cheapest way to build. (step 1)
  • Independent deploys are the real prize of microservices. If your services must be released together, you have a distributed monolith: all the cost, none of the gain. (step 4)
  • Many teams get most of the benefit of microservices (clear ownership) with none of the network cost. (step 8)

Watch out

  • The pain is mostly about people: waiting for each other, stepping on each other, and a big blast radius for every mistake. (step 2)
  • The more services a request must pass through, the slower and less reliable it is. Keep call chains short. (step 5)
  • Microservices need logs, metrics and traces from day one. Without them you are guessing. (step 7)
UsersAPI gatewayOrdersPaymentsOrders DBCatalogPayments DBCatalog DB

Key numbers

Release time
5 min
Checkout calls
12 ms
Checkout uptime
99.6%

Terms

Monolith
An application built, deployed and run as a single program.
Microservices
Many small programs, each owning one part of the business, built and released independently.
Domain boundary
A line drawn around one part of the business so a service owns a whole job.
Independent deployment
Releasing one service without waiting for or redeploying the others.
Distributed monolith
Services that must be released together: the costs of microservices without the benefits.
Network call cost
A call between services crosses the network, so it is slower and can fail, unlike a function call.
Database per service
Each service owns its data and others reach it only through its API.
Distributed tracing
Passing one trace id through every service so a whole request can be followed.
Modular monolith
One deployable program built from modules with strict boundaries.

System Design · 06. Architecture · 8 min · Intermediate

Service Discovery

How one service finds the live addresses of another when machines come and go.

In short

Addresses are temporary, so callers look services up by name. Instances register themselves and send heartbeats; a registry keeps the live list.

Client-side discovery has no extra hop but needs library code; server-side and DNS keep callers simple; a mesh moves it all into proxies. Each one is a different answer to how quickly a change is noticed: 15 to 45 s here, and retries cover the gap.

Key ideas

  • In the cloud, addresses are temporary. Callers must look them up, not remember them. (step 2)
  • The service name stays fixed; the addresses behind it change freely. (step 3)

Good to know

  • Fast and simple on the network, but the discovery logic is copied into every service. (step 5)
  • This is what Kubernetes Services and cloud load balancers do for you. (step 6)

Watch out

  • A hard-coded address is a promise that the machine at that address will exist forever. (step 1)
  • A shorter heartbeat finds failures faster but adds traffic and can drop a healthy but busy instance. Callers should also retry on another instance. (step 4)
  • A low TTL (5 to 30 s) helps, but cannot be trusted on every client. DNS also gives no health or load information. (step 7)
  • A mesh is powerful but heavy: another layer to run, learn and debug. Use it when many services need the same rules. (step 8)
OrdersPayments 10.0.1.5Payments 10.0.1.6RegistryPayments 10.0.1.9

Key numbers

Change noticed in
15–45 s
Instances up
3

Terms

Service discovery
Finding the current addresses of a service by name instead of keeping them in a file.
Service registry
A small, highly available database of service names and the addresses of their live instances.
Self-registration
An instance adds itself to the registry when it starts.
Heartbeat
A regular signal from an instance saying it is alive; missed ones get it removed from the registry.
Client-side discovery
The caller asks the registry for the list and chooses an instance itself.
Server-side discovery
The caller uses one stable address; a load balancer finds the live instances.
DNS-based discovery
A service name resolves through DNS to the addresses of live instances, cached for the TTL.
Service mesh
A proxy beside each service handles lookup, retries, encryption and metrics, driven by a control plane.

System Design · 06. Architecture · 12 min · Advanced

Sagas & Distributed Transactions

How to keep one business action correct when it spans several services and databases.

In short

Across services there is no single transaction. Two-phase commit gives all-or-nothing but blocks (locks for 5 min if the coordinator dies). A saga commits each local step at once, in 20 ms, and undoes failures with compensations.

Choreography uses events, orchestration uses a central runner. The outbox makes the event reliable, and idempotent steps make repeats harmless.

Key ideas

  • Phase 1: prepare and vote. Phase 2: commit or abort. Nobody commits until everybody has said yes. (step 2)
  • A saga gives up isolation to keep availability. The business rule must be fine with the in-between states. (step 4)
  • One database transaction can hold both the data and the event. The relay turns the event into a message. (step 8)
  • At-least-once delivery plus idempotent steps behaves like exactly once. (step 9)

Good to know

  • Choreography suits short sagas of 2 to 4 steps with clear events. (step 6)
  • Orchestration is easier to follow as sagas grow; workflow engines (Temporal, AWS Step Functions) provide it ready-made. (step 7)

Watch out

  • This is the price of a database per service: you give up the single transaction. (step 1)
  • 2PC trades availability for strict consistency. It is used inside some databases, rarely across microservices. (step 3)
  • A compensation is a business action, such as a refund, not a database rollback. Design it for every step. (step 5)
OrchestratorInventoryPaymentsShipping

Key numbers

Locks held
20 ms each
Steps done
3 of 3
Order
Confirmed

Terms

Distributed transaction
One business action that must change data in several databases or services.
Two-phase commit (2PC)
A coordinator asks every service to prepare and vote, then tells all to commit or abort.
2PC blocking
If the coordinator dies after the votes, services keep their locks and wait.
Saga
A chain of local transactions, each committed at once, with undo steps for failures.
Compensation
A new action that cancels the effect of an already committed step, such as a refund.
Choreography
Services trigger each other by publishing and reacting to events, with no central controller.
Orchestration
A central orchestrator runs the saga's steps and compensations and saves its state.
Outbox pattern
Write the data and the event in one local transaction, and let a relay publish the event.
Idempotent step
A step that can be repeated safely, because a unique key lets it recognise duplicates.

System Design · 06. Architecture · 10 min · Intermediate

Batch & Stream Processing

Crunch a huge pile of data once a day, or count events as they arrive: when to choose each.

In short

Batch jobs process a big, fixed pile with many machines (100 s instead of 33.3 min) and are complete but old. Streams update with every event, fresh in about 2 s, but must handle windows, late events and restarts.

Choose by how fresh the answer must be, and by how much correctness you need on the first try.

Key ideas

  • Two ways to answer: process the pile in big runs (batch), or keep counting as events arrive (stream). (step 1)
  • Windows turn an endless stream into a series of small, finished answers. (step 6)
  • Checkpoint the position and the state together, and write results in a way a replay cannot double. (step 8)

Good to know

  • Move the code to the data, split by key, and the work spreads over as many machines as you can add. (step 3)
  • Use a stream when a delay costs money: fraud checks, live dashboards, alerts. (step 5)
  • Many teams now start with a stream and replay, and keep a batch job only where they truly need it. (step 9)

Watch out

  • Batch gives complete, correct answers, but the result is only as new as the last run: up to a day old. (step 2)
  • Alert when a job has not finished by a deadline. A silent failed job means yesterday's numbers on today's dashboard. (step 4)
  • A longer wait is more accurate and slower. Choose the grace period from how late your data really is. (step 7)
Apps and usersEvent streamStream processorCounts per minute

Key numbers

Result age
≈ 2 s
Events counted
99.5%

Terms

Batch processing
Reading a large, fixed set of data in one job, computing the answer and writing it out.
Data lake
Cheap, large storage that keeps raw data, such as every event, for later processing.
MapReduce
Split data over machines, map each piece into key-value pairs, shuffle by key, and reduce each key.
Job scheduler
A tool that starts the stages of a data pipeline at set times and in the right order.
Stream processing
Handling each event as it arrives, so results are fresh within seconds.
Tumbling window
A fixed, non-overlapping slice of time, such as one minute, used to group events in a stream.
Watermark
How long a stream waits for late events before it closes a window.
Checkpoint
A saved position and state, from which a stream job restarts after a crash.
Exactly-once (effectively)
At-least-once delivery plus idempotent writes, so each event affects the result once.
Lambda architecture
A fast stream layer and an accurate batch layer over the same events, merged at query time.
Kappa architecture
Only a stream, with the event log replayed to recompute results.