06. Architecture / Lesson 1 of 4

Monolith vs Microservices

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

  • 10 min
  • Intermediate
  • 10 steps
  • Release time30 min (good)
  • Checkout calls0.006 ms (good)
  • Checkout uptime99.9% (good)
UsersThe monolithDatabase
  • Request
  • Response
  • Failed request
Step 1/10

Step 1 of 10: A monolith: the whole product is one program

Read every step as text
  1. Step 1: A monolith: the whole product is one program

    Orders, payments and the catalog live in one codebase, are built together and run as one process on one database. This is where almost every product starts.

    It is simple: a call between two parts is a normal function call (about 0.001 ms), one transaction can change everything, and there is a single thing to deploy, test and watch.

    A monolith is not a mistake. For a small team it is usually the fastest and cheapest way to build.

  2. Step 2: With 40 engineers, one program becomes a traffic jam

    Now 40 engineers change the same code. A release builds, tests and rolls out everything, say 30 minutes, and only one can go at a time, so an 8-hour day holds at most 16 releases for every team together.

    A change in one corner can break another, and a bug in the reports page that crashes the process takes checkout down with it.

    The pain is mostly about people: waiting for each other, stepping on each other, and a big blast radius for every mistake.

  3. Step 3: Split along the business: orders, payments, catalog

    In a microservice design each part of the business becomes its own small program with its own team: orders, payments and catalog. An API gateway sends each request to the right one.

    Split by what the business does (a domain), not by technical layers. A service that owns one whole job can change without asking the others.

    Good boundaries follow the business, so most changes touch only one service.

  4. Step 4: Each team ships on its own

    A small service builds and tests in about 5 minutes, and the payments team does not wait for the catalog team. Different services can be released at the same time.

    Each service can also be scaled and written separately: the catalog can run on 10 servers and payments on 2, and a team can pick the language that suits its job.

    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.

  5. Step 5: Every call is now a network call

    A function call inside one program costs about 0.001 ms. A call between services crosses the network, say 2 ms inside one data centre, and it can time out or fail. A checkout makes 6 such calls in a row: 6 × 2 = 12 ms, against 0.006 ms before.

    Availability also multiplies. A checkout needs 4 services and each is up 99.9% of the time, so the checkout is up only 99.9% ^ 4 = 99.6%. That is about 2.9 hours down a month, against 43 minutes for one app.

    The more services a request must pass through, the slower and less reliable it is. Keep call chains short.

  6. Step 6: Each service owns its own data

    If three services share one database, a change to a table can break all of them, and they are tied together again. So each service gets its own database, and others may only ask through its API.

    The price: no more JOIN across services and no single transaction over all of them. To show an order with product names, orders calls catalog and merges the answers in code.

    Rule: only the service that owns the data reads or writes it. Everyone else uses its API or its events.

  7. Step 7: One slow request, four places to look

    A user says checkout was slow. In a monolith you read one log. Now the request passed through four services, each with its own logs and its own clock.

    The fix is a trace id: the gateway makes one id per request and every service passes it on and logs it. A tracing tool then shows the whole path and where the time went.

    Microservices need logs, metrics and traces from day one. Without them you are guessing.

  8. Step 8: A middle road: one program, strict modules

    A modular monolith is still one deployable program, but it is built from separate modules with clear boundaries: orders, payments and catalog talk only through small public interfaces, and each owns its tables.

    Calls stay cheap (0.006 ms for a checkout), there is one thing to run, and the boundaries are already drawn, so a module can be pulled out later if it needs to be.

    Many teams get most of the benefit of microservices (clear ownership) with none of the network cost.

  9. Step 9: Split only when there is a clear reason

    Good reasons: teams keep blocking each other; one part needs very different scaling (video encoding needs 20 servers, the rest needs 2); one part must be released far more often, or be isolated for safety.

    Pull out just that part first. Moving the encoder into its own service adds one network call (2 ms) and the checkout then needs 2 programs, so uptime is 99.9% ^ 2 = 99.8%. Splitting for fashion, with 5 engineers, only buys the costs.

    Start with a monolith, keep modules clean, and extract a service when the pain is real and named.

  10. Step 10: Monolith and microservices in one picture

    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.