Microservice Patterns
Most high-level designs end up as a handful of boxes labelled "Service": a user service, an order service, a payment service, a notification service. Drawing them is easy. The questions come right after: why are these separate services? How does the order service find the payment service? What does a mobile app actually call? How do you show a page that needs data from four services? What happens to everything else when one of them is slow?
Those questions are what microservice patterns answer. None of them are complicated on their own, but each one solves a specific problem that splitting a system creates, and each one has a cost. Interviewers are less interested in whether you can name them than in whether you reach for them for the right reasons.
We'll go from the foundation up:
- When splitting into services is worth it, and where to draw the lines.
- How requests get in and find their way around: API gateways, backends for frontends, service discovery and sidecars.
- How services share data and react to each other: event-driven design, CQRS, and splitting an existing monolith without a rewrite.
Monolith or Microservices?
A monolith is one deployable application that contains all of the system's features, usually with one database. Microservices split the system into independently deployable services, each owning one business capability and its own data, talking over the network.
What splitting buys you:
- Independent deployment. The checkout team can ship five times a day without coordinating with the search team. For large engineering organizations, this is the main reason, and it's an organizational benefit as much as a technical one.
- Independent scaling. The search service needs 200 instances during a sale; the account settings service needs 3. In a monolith you scale everything together.
- Fault isolation. A memory leak in the recommendations code crashes recommendations, not checkout. (Only if you design for it, as we'll see.)
- Technology fit. The ML ranking service can be Python on GPUs while the payment service is Java on a relational database.
What it costs:
- Every call is a network call. An in-process function call takes nanoseconds and never fails halfway. A network call takes on the order of a millisecond inside a data center, can time out, and can succeed on the server while the client sees a failure.
- Availability multiplies. If a request needs five services in a chain and each is up 99.9% of the time, the request succeeds about 0.999^5 ≈ 99.5% of the time: roughly 3.6 hours of downtime a month instead of 43 minutes.
- No more cross-service transactions or joins. Data that used to be one
JOINand oneCOMMITaway now lives in different databases (see /system-design/transactions). - Operational overhead. More deployments, more dashboards, distributed tracing to debug anything, and versioned contracts between teams.
A modular monolith, one deployable with strict internal module boundaries, gets many of the code-organization benefits without the network. Many successful systems start there and split out services only when a module's team, scaling profile or failure mode clearly calls for it.
In a product design interview, you don't need to justify every service box, but don't split for its own sake either. A good sentence is: "I'll separate these into services because they scale differently and are owned by different teams; in practice they could start as modules in one deployable." Interviewers like seeing that you know splitting has a price.
Where to Draw the Lines
The most useful rule: split by business capability, and let each service own its data. Orders, payments, inventory, users, notifications. Each maps to a part of the business with its own language and rules, sometimes called a bounded context. Inside one, "order" means one specific thing; the warehouse's idea of an order can be different, and that's fine.
Signs of a good boundary:
- Things that change together live together. If every feature touches services A and B at the same time, they're one service in two deployments.
- Things that must be transactionally consistent live together. If "reserve seat" and "create booking" must be atomic, put them in one service with one database, rather than splitting and then needing a saga.
- Calls between services are coarse: "place this order," not "get field X, then get field Y."
Database per service is the rule that makes the rest work. If two services read and write the same tables, neither can change its schema without breaking the other, and you've built a distributed monolith: all the costs of microservices, with none of the independence. Other services get data through the owner's API or through events it publishes, never by reaching into its database.