Monoliths and microservices

Topic 8 of 9 in Week 5: Distributed systems and service communication10 min readIntermediateFree course

Overview

“Should this be microservices?” comes up in design interviews and in real architecture debates. Microservices solve real problems, mostly organisational ones, but they bring a heavy cost in distributed-systems complexity. This lesson compares the options and how to draw boundaries if you do split.

Two architectures

Monolith versus microservices. A modular monolith is one deployable unit containing users, orders, payments and search modules sharing one database: deploys are simple, calls are local and one transaction can span modules, but scaling and team boundaries get harder with size. In microservices, an API gateway routes to separate users, orders, payments and search services, each with its own database: teams deploy and scale independently, at the cost of network calls, distributed data and more operational work.

Monolith

One codebase, built and deployed as one unit, usually with one database.

  • Pros: simple to develop, test and deploy; function calls instead of network calls; one database transaction can update everything consistently; easy to refactor across modules.
  • Cons: as the team and codebase grow, deploys get slower and riskier, one bug or resource hog affects everything, and all parts must scale together.

A modular monolith keeps strict internal boundaries (each module owns its tables and exposes an interface) while still deploying as one unit. It gives much of the structure of microservices without the network, and makes a later split far easier.

Microservices

The system is split into services, each owning a business capability and its own data, communicating over the network.

  • Pros: teams work and deploy independently; each service scales on its own (a CPU-heavy search service on big machines, a light profile service on small ones); failures can be isolated; different technologies where justified.
  • Cons: every call can fail or be slow; no cross-service transactions (sagas instead); data is duplicated and eventually consistent; you need service discovery, distributed tracing, centralised logging, per-service CI/CD and on-call ownership. Debugging a request that touches ten services is hard.

When microservices pay off

  • many teams stepping on each other in one codebase,
  • parts with very different scaling or reliability needs,
  • a need for independent release cycles,
  • mature tooling for deployment and observability.

For a small team or a new product, a modular monolith is usually the better choice. Many successful companies split only after years.

Drawing boundaries

Good service boundaries follow business capabilities and data ownership (often called bounded contexts in domain-driven design):

  • One owner per piece of data. The order service owns orders; others ask it or subscribe to its events. Never let two services write the same tables.
  • High cohesion, low coupling. Things that change together stay together. If every feature needs changes to three services in lockstep, the boundaries are wrong.
  • Avoid chatty dependencies. If service A calls B for every request and B calls C, latency and failure rates stack up; consider merging, caching or replicating data via events.
  • Don’t split by technical layer (a “database service”, a “validation service”); that creates a distributed monolith.

Sharing data between services

  • API calls for data you need fresh and occasionally.
  • Events to keep a local, read-only copy of another service’s data you need often (the order service keeps a copy of product names and prices from catalogue events).
  • No shared database. It couples deployments and schemas and defeats the purpose.

Migrating: the strangler fig

Don’t rewrite everything at once. Put a gateway or proxy in front of the monolith, then move one capability at a time into a new service, routing its traffic there, until the old code can be removed. Each step is small and reversible.

In an interview

Most interview designs are drawn as a few services anyway (upload service, feed service, notification service). That’s fine, but explain why they’re separate (“the transcoding workers scale on queue depth and need GPUs, so they’re separate from the API”) and keep the count reasonable. If asked “monolith or microservices?”, answer with the trade-off and the team context rather than a slogan.

Recap

A monolith is simple and consistent; a modular monolith adds clear internal boundaries; microservices let teams and components evolve and scale independently at the cost of distributed-systems complexity. Split along business capabilities with one owner per piece of data, share data through APIs and events rather than shared databases, and migrate gradually when the benefits are real.

Exercise

Split — or not?

A startup with 8 engineers runs a food-ordering monolith with one PostgreSQL database. Deploys take 15 minutes and happen daily. The restaurant-search feature is CPU-heavy and slows everything at lunchtime. The founders want to 'move to microservices'. (a) What would you recommend now, and why? (b) Which part, if any, would you extract first, and how would it get its data? (c) What would need to be in place before splitting further?

Text
(a)
(b)
(c)

Summary

  • A monolith is one deployable application; a modular monolith keeps clear internal boundaries and is often the best starting point.
  • Microservices are small services that each own their data and can be deployed, scaled and changed independently.
  • Split along business capabilities and data ownership, not technical layers; avoid sharing databases between services.
  • Microservices trade code complexity for operational and distributed-systems complexity: network failures, eventual consistency, tracing and deployment tooling.
  • Extract services gradually (strangler fig pattern) when team size, scaling needs or release independence justify it.