When to split, and where
A monolith is one application that does everything: products, orders, payments. A microservice architecture splits it into separate applications that talk over the network, each deployed on its own.
Microservices help when:
- Many teams work on the system and keep getting in each other’s way.
- Parts need to scale differently, like search under heavy load while invoicing runs once a night.
- Parts change at different speeds, so one team can release ten times a day without waiting for the others.
They also cost a lot: every call can fail, data is spread out, testing and debugging are harder. That’s why many teams start with a modular monolith: one deployable application, but split inside into modules with clear boundaries. Splitting it later is much easier.
When you do split, two rules matter most:
- Split by business capability: catalog, ordering, payments, shipping. Not by technical layer (“the database service”).
- Each service owns its data. Other services ask it through its API or listen to its events. They never read its tables directly.
Microservices buy independent deployability, team autonomy and independent scaling at the price of distributed-systems problems: partial failure, latency, eventual consistency, versioned contracts and harder observability. A modular monolith with enforced module boundaries (packages or Gradle modules, ArchUnit or Spring Modulith checks) captures most of the design benefit and leaves the option to extract services when team or scaling pressure is real. Boundaries follow business capabilities / bounded contexts: a service owns a cohesive set of rules and its data, exposes them via an API and events, and can change its schema freely. Choose synchronous calls (REST, gRPC) when the caller needs the result to proceed, asynchronous events when other services just react; prefer events for cross-service side effects, which removes temporal coupling. Warning signs of a distributed monolith: shared databases, chains of synchronous calls per request, services that must deploy together, and changes that always touch several services. Gateways or backend-for-frontends aggregate APIs for clients (especially mobile) rather than making clients call many services.
@Service
class CheckoutService(
private val inventory: InventoryClient, // synchronous: checkout can't continue without it
private val orders: OrderRepository,
private val outbox: OutboxRepository,
) {
@Transactional
fun placeOrder(cart: Cart): Order {
if (!inventory.reserve(cart.items)) throw OutOfStockException()
val order = orders.save(Order.from(cart))
// asynchronous: email, loyalty and analytics react to the event at their own pace
outbox.save(OutboxEvent.of("OrderPlaced", order.id, OrderPlaced.from(order)))
return order
}
}@Service
class CheckoutService {
private final InventoryClient inventory; // synchronous: checkout can't continue without it
private final OrderRepository orders;
private final OutboxRepository outbox;
CheckoutService(InventoryClient inventory, OrderRepository orders, OutboxRepository outbox) {
this.inventory = inventory;
this.orders = orders;
this.outbox = outbox;
}
@Transactional
public Order placeOrder(Cart cart) {
if (!inventory.reserve(cart.items())) throw new OutOfStockException();
Order order = orders.save(Order.from(cart));
// asynchronous: email, loyalty and analytics react to the event at their own pace
outbox.save(OutboxEvent.of("OrderPlaced", order.id(), OrderPlaced.from(order)));
return order;
}
}- Inventory is called directly, because checkout must know whether the items are available before it can continue.
- Email, loyalty points and analytics don’t need to answer checkout. They read the
OrderPlacedevent later. If the email service is down for an hour, checkout keeps working. - The event is saved in an outbox table in the same transaction as the order, then published to Kafka by a separate process, so an order is never saved without its event. The Kafka lesson shows the details.
Predict the output
The orders service reads the customers table of the customer service's database directly, to save an HTTP call. The customer team renames the column phone to phone_number and deploys. What happens?
A shared database turns a private implementation detail into a public contract nobody declared. The customer team can't change its own schema without coordinating with every reader, so services stop being independent. Each service owns its data; others use its API or subscribe to its events, which can evolve with versioning.
Tap the line with the bug.
fun listProducts(products: List<Product>): List<ProductView> {
val views = mutableListOf<ProductView>()
for (p in products) {
val price = pricingClient.priceFor(p.id)
val stock = inventoryClient.stockFor(p.id)
views += ProductView(p, price, stock)
}
return views
}List<ProductView> listProducts(List<Product> products) {
List<ProductView> views = new ArrayList<>();
for (Product p : products) {
Price price = pricingClient.priceFor(p.id());
Stock stock = inventoryClient.stockFor(p.id());
views.add(new ProductView(p, price, stock));
}
return views;
}Hint: How many network calls does a page of 50 products make?
Two remote calls per product: a page of 50 products makes 100 sequential HTTP calls. At 50 ms each, that’s 5 seconds, and any slow call makes it worse. It’s the network version of the N+1 query problem, and a sign that the boundaries or the APIs don’t fit how the data is used.
Ask for everything in one batch call per service (pricingClient.pricesFor(ids), inventoryClient.stockFor(ids)), run the two calls in parallel, or keep a local copy of prices and stock updated from their events if the page is read far more often than they change.
Contracts between services
- Version APIs and events: additive changes only within a version; consumers ignore unknown fields; breaking changes get a new version and a deprecation period.
- Consumer-driven contract tests (Pact, Spring Cloud Contract) catch breaking changes in the provider’s CI, before deployment.
- Schemas for events (Avro or Protobuf with a schema registry, or JSON Schema) with compatibility rules enforced on publish.
- Correlation: propagate trace context (W3C
traceparent) and a request id through calls and events, so one user action can be followed across services.
- Microservices = independent deploy and scale, paid for with partial failure, latency, eventual consistency, contracts. Start with a modular monolith (Spring Modulith, ArchUnit) and extract under real pressure.
- Boundaries by business capability; each service owns its data; no shared database.
- Sync (REST/gRPC) when the caller needs the answer; events (with an outbox) for side effects.
- Distributed monolith signs: shared tables, chatty per-item calls, lockstep deploys, cross-service changes for every feature.
- Versioned APIs and event schemas, contract tests, trace propagation; BFF/gateway for mobile clients.
Check this part
How should microservices be split?
Checkout must also send a confirmation email. Call the email service synchronously, or publish an event?
Which is a sign of a distributed monolith?
What usually captures most of the benefit with less risk early on?