Architecture · Chapter 17

Monolith vs microservices

When one codebase stops scaling with your team, how to split it into services, how they talk (sync or async), who owns the data, and why "monolith first" is usually right.

IntermediateInterview priority: Medium30 min
On this page
  1. The problem: one app, fifty engineers
  2. The idea: small services that own their data
  3. Drawing the boundaries
  4. How services talk: sync or async
  5. Data per service, and the consistency it costs
  6. The plumbing: gateway, discovery, mesh
  7. The options side by side
  8. When not to use microservices
  9. In the interview
  10. Quiz

The problem: one app, fifty engineers

A shop starts as a single program: users, catalogue, cart, payments and search in one codebase, one database, one deploy. For a team of five this is ideal. Then the company grows.

A monolith is simple until the team and traffic grow

  1. Most apps start as a monolith: one codebase, deployed as one program. Users, catalogue, cart and payments all live together. It’s simple to build, test and run.
  2. As it grows, pains appear. Fifty engineers editing one codebase step on each other’s toes. Every small change means redeploying everything. And to handle more search traffic, you must scale the entire app.
  • Teams collide. Fifty engineers merge into one repository; a broken test in payments blocks a search release.
  • Deploys get scary. Every change ships everything, so releases become weekly events with long checklists.
  • Scaling is all or nothing. Search needs ten times the CPU on sale day, but you can only scale the whole app.
  • One bug can take everything down. A memory leak in image resizing crashes the process that also takes payments.

A monolith is one deployable unit. That is not a flaw in itself: most of these pains are about people and deploys, not about the code being in one place.

The idea: small services that own their data

Each service owns one job, its data, and its deploys

  1. Microservices split the app into small services, each owning one job and its own data. Each can be deployed by its own team, on its own schedule.
  2. Each service can scale on its own. On sale day, search gets ten copies while payments stays at two. And a bug in one service has a smaller blast radius: search breaking doesn’t stop checkout.

Microservices split the system into independently deployable services, each owning one business capability (orders, payments, search) and its own data. Other services may only reach that data through the owning service's API or the events it publishes. Each service has a team that can change, deploy and scale it without asking anyone else.

The benefits are real: independent deploys, scaling only what is busy, and a smaller blast radius (how much breaks when one part fails). But you pay for them in network calls, operational load and harder consistency.

Drawing the boundaries

Bad boundaries are the most expensive microservices mistake, because moving them later means migrating data between databases.

  • Split by business capability, not by technical layer. "Orders" and "Payments" are good services. "Database service" and "Validation service" are not: every feature would touch all of them.
  • Use the language of the domain. In domain-driven design, a bounded context is an area where a word has one meaning. "Product" means a listing with photos in the catalogue, and a SKU with a shelf location in the warehouse. Those are two contexts, so probably two services.
  • Look at what changes together. If two parts are always edited in the same pull request, they belong in the same service.
  • Follow team shape. Conway's law says systems end up mirroring the communication structure of the organisation that builds them. One team per service (or a few services) works; one service shared by four teams does not.

How services talk: sync or async

Synchronous calls behind an API gateway, and how slowness spreads

A synchronous call (REST or gRPC) means the caller waits for the answer. It is simple and gives an immediate result, but it couples the caller's availability and latency to the callee's.

Worked example: the cost of a call chain

Inside one process, a function call takes nanoseconds. Between services in one datacentre, a call costs roughly 0.5–2 ms at the median and often 10–50 ms at p99, once you add serialisation, the network and the callee's own work.

If checkout makes four calls in sequence, each 2 ms median and 30 ms p99, the median adds about 8 ms, but the chance that at least one call hits its p99 is 1 − 0.99^4 ≈ 4%, so the checkout's own tail gets noticeably worse.

Availability multiplies too. If checkout needs Cart, Inventory and Payments to be up, and each is 99.9% available, checkout is at best 0.999^3 ≈ 99.7%: about 2 hours of downtime a month instead of 43 minutes.

Asynchronous events: publish once, and consumers react in their own time

An asynchronous message (through a queue or event log such as Kafka or RabbitMQ) decouples them: the producer publishes an event like OrderPlaced and moves on; consumers process it when they can. If a consumer is down, its messages wait.

StyleGood atWeak atUse when
Sync request/response (REST, gRPC)Simple; immediate answer; easy to debugAvailability and latency multiply along the chain; risk of cascading failureThe caller needs the answer now: "is this card valid?"
Async commands (a queue)Smooths load spikes; retries; callee can be downCaller cannot get an answer in the same request; duplicates need idempotencyWork that can finish later: send an email, transcode a video
Async events (pub/sub, Kafka)Producer does not know its consumers; new consumers added freelyEventual consistency; harder to trace "who reacted to what"Several services must react to one fact: order placed, user deleted

For every sync call, set a timeout, retry only with backoff and only for idempotent operations, and use a circuit breaker (stop calling a failing service for a while) so one slow dependency cannot tie up all your threads. See circuit breakers and retries.

Data per service, and the consistency it costs

Each service owns its tables, and no other service reads them directly. If Orders could query the Payments tables, Payments could never change its schema without breaking Orders: you would have services in name but a monolith in practice.

The cost: no more single database transaction across orders and payments. Two tools replace it:

  • Saga: a business transaction split into local steps, one per service, each with a compensating step that undoes it. Reserve stock → charge card → confirm order; if the charge fails, run "release stock". The intermediate states are visible for a moment, so the design must tolerate them.
  • Transactional outbox: a service must update its database and publish an event, but doing two separate writes can lose one if it crashes in between. Instead, it writes the event into an outbox table in the same local transaction, and a relay process publishes rows from that table to the broker. The event is published at least once, so consumers must be idempotent (safe to process the same message twice).

The plumbing: gateway, discovery, mesh

PieceWhat it doesExamples
API gatewayOne entry point for clients: authentication, rate limiting, routing to services, sometimes combining several calls into one responseKong, AWS API Gateway, Envoy, NGINX
Service discoveryKeeps the list of healthy instances of each service, so callers find them as they scale up and downKubernetes Services and DNS, Consul
Service meshA small proxy (sidecar) next to every service instance handles mutual TLS, retries, timeouts and per-call metrics, without changing app codeIstio, Linkerd
ObservabilityDistributed tracing becomes essential once a request crosses 5 servicesOpenTelemetry, Jaeger (see monitoring)

A mesh is worth it with dozens of services and strict security needs; with five services it is mostly extra moving parts.

The options side by side

ArchitectureGood atWeak atUse when
MonolithSimple to build, test, debug and deploy; fast in-process calls; one transaction for everythingTeam contention past ~20–30 engineers; whole-app deploys and scalingEarly products, small teams, unclear domain
Modular monolithClean internal module boundaries with one deploy; easy to split laterStill one deploy and one scaling unit; discipline needed to keep modules apartThe default for most companies; a stepping stone
MicroservicesIndependent deploys and scaling; team autonomy; smaller blast radiusNetwork latency and failures; data consistency; much more operations workMany teams, clear domains, parts with very different load or reliability needs

The price of a network hop, and the sensible path: split only when there is a reason

  1. But it isn’t free. Every call between services crosses the network: slower than a function call, and it can fail. You now run many services, many deploys, many dashboards. And data spread across services is harder to keep consistent.
  2. A common, sensible path: start as a modular monolith, one app with clean internal boundaries, and split out a service only when a real need appears, like one part that needs very different scaling, or a team that needs to ship independently.

When not to use microservices

  • A small team. Ten engineers running 15 services spend their time on pipelines, dashboards and on-call, not features.
  • An unclear domain. Early on you do not know where the boundaries are. Wrong boundaries in a monolith are a refactor; between services they are a data migration.
  • No platform yet. Without automated deploys, centralised logs, tracing and health checks, many services are many blind spots.
  • Strong cross-entity consistency. If most operations must atomically change data that would end up in different services, the boundaries are probably wrong.

Start with a modular monolith. Split out a service when a concrete need appears: a part with very different scaling (search, video transcoding), a part with different reliability or security needs (payments), or a team blocked by the shared deploy.

In the interview

Your turn: split a food delivery app

A food delivery monolith has 80 engineers in 8 teams. Restaurant search gets 50× more traffic at lunch; payments must pass a security audit; the dispatch team wants to ship several times a day. Propose services, how they communicate, and what you would leave in the monolith.

Compare with our answer

One reasonable split: Search (read-heavy, scaled separately, fed by events from the restaurant catalogue), Payments (isolated for the audit, small surface, strict access), Dispatch (owned by its team, deployed often). Orders stays the core and publishes OrderPlaced, OrderCancelled; Dispatch and Notifications consume them. Checkout calls Payments synchronously (it needs the answer) with a timeout and circuit breaker; everything else reacts to events. Menus and restaurant admin can stay in the monolith until there is a reason.

A strong answer mentions: boundaries by capability and team · splitting for a concrete reason (scaling, security, deploy speed) · each service owns its data · sync only where an answer is needed now · events for side effects with idempotent consumers · leaving the rest in a modular monolith.

Your turn: spot the problems

A design review shows: 9 services all reading and writing one shared Postgres database; the mobile app calls 5 services directly; placing an order makes a chain of 6 synchronous calls, each hop making up to 3 attempts; there is no tracing. List the problems and fixes.

Compare with our answer
  • Shared database: services are coupled through the schema; give each its own data and expose it through APIs or events.
  • Clients calling services directly: add an API gateway for auth, rate limits and routing, so internal changes don't break the app.
  • Long sync chain: availability and latency multiply (0.999^6 ≈ 99.4%); make side effects asynchronous.
  • Retries at every hop: retry storms (3^6 calls in the worst case); retry at one layer with backoff, a budget and circuit breakers.
  • No tracing: impossible to find the slow hop; add OpenTelemetry with trace IDs propagated through every call.

A strong answer mentions: data per service · an API gateway · shortening sync chains with events · retry storms and their fixes · distributed tracing.

Microservices on one page
  • Monolith = one deployable unit. Its pains are mostly people and deploys: team contention, all-or-nothing releases and scaling.
  • Microservices = independently deployable services, each owning a business capability and its data.
  • Boundaries: business capability, bounded contexts, what changes together, team shape (Conway's law).
  • Sync (REST/gRPC) when you need the answer now; async (queues, events) for side effects. Availability multiplies: 0.999³ ≈ 99.7%.
  • Every sync call: timeout, backoff with jitter, retry at one layer, circuit breaker.
  • No shared database. Sagas with compensating steps replace cross-service transactions; the outbox publishes events reliably; consumers are idempotent.
  • Plumbing: API gateway, service discovery, optional service mesh (sidecars for mTLS, retries, metrics), tracing.
  • Default: modular monolith first; split for a concrete reason. Beware the distributed monolith.
Interactive labPractise it: split the monolithOpens in the ByteAtlas lab. Your progress is saved in this browser.
Check what stuck

1. A 6-person startup is building its first product and wants microservices "so it scales later". What is the best advice?

2. Checkout calls Cart, Inventory and Payments synchronously, and each is 99.9% available. Ignoring other causes, about how available can checkout be?

3. Twelve services share one database, and changing any table needs four teams to deploy together. What has the team built?

4. An Orders service must save an order and publish OrderPlaced. It sometimes crashes between the two, losing events. What fixes this?

5. Payments slows down. Client, gateway and Orders each make up to three attempts per call. What is the main risk?

6. When placing an order, which call should stay synchronous rather than become an event?

7. What does a service mesh such as Istio or Linkerd mainly add?

Score 60% or more and the lesson is marked complete.

Passing the quiz also marks it complete. Progress is saved in this browser only.