Designing and Decomposing Microservices
🎯 Introduction
The previous article established that splitting a Node.js project into multiple processes does not automatically produce well-designed microservices. Suppose we divide the store into user, order, inventory, and payment services. Why those four? Why not create a separate service for order items? This article starts with concrete business capabilities, assigns responsibilities step by step, and evaluates whether the resulting boundaries make sense.
🧱 Why Database Tables Should Not Define Service Boundaries
A store might have three tables: orders, order_items, and payments. If every table becomes a service, creating an order requires the order service and order-item service to communicate just to save one complete order.
Orders and their items usually belong to the same business capability: creating, viewing, and managing orders. Canceling an order, for example, requires its status and items to remain correct. Splitting them too finely turns an operation that could stay within one service into a network interaction. If a step fails, recovery also becomes a concern.
Payment records, on the other hand, have their own rules for payment attempts, outcomes, and failure reasons, even though they relate to orders. Payments can therefore be a separate responsibility.
This gives us our first principle: database tables store data; they do not define service boundaries. Identify business responsibilities first, then decide who manages the data.
🧩 Deriving Services from Business Capabilities
Step 1: List what the system needs to do
Set aside file locations and table counts. Start by listing the store's business needs:
| Business functionality | The question it answers |
|---|---|
| Register users, edit profiles, manage account status | Who is this user, and can they use the account? |
| Create, view, and cancel orders | What did the user buy, and what stage has the order reached? |
| Query available quantities, reserve stock, release stock | How many units can still be sold, and how many are reserved? |
| Initiate simulated payments, record payment outcomes | Did this payment succeed, and if not, why? |
At this stage, identify business capabilities: categories of business work the system provides, rather than individual endpoints or tables.
Step 2: Group closely related functionality
Creating, viewing, and canceling orders may use three endpoints, but they all manage the lifecycle of the same order. If each becomes a service, changing order rules may require coordinated changes across several services.
Conversely, canceling an order may require releasing stock, but that does not mean orders and inventory must be merged. The order service decides whether cancellation is allowed; inventory decides how to release the reserved quantity. They collaborate while owning different rules.
Using this approach, the course starts with four services:
| Service | Business responsibilities | Primary data |
|---|---|---|
| User service | User profiles and account status | User profiles and account status |
| Order service | Create, view, and cancel orders; maintain order status | Orders and order items |
| Inventory service | Query, reserve, and release stock | Stock and reservation records |
| Simulated payment service | Initiate simulated payments and record success or failure | Payment records |
Step 3: Specify who may modify data
Suppose a user buys two units. The order service can ask inventory to reserve them, but should not modify the inventory table directly.
The reason is practical: if both orders and payments can deduct stock themselves, it becomes difficult to determine which service violated reservation rules when stock runs out. Changing inventory calculations would also require inspecting every service that touches the table.
The inventory service therefore maintains inventory rules and data. Other services collaborate through its public API or agreed messages. Similarly, payments does not modify the order table directly. It reports the payment outcome, and the order service decides how to update order status.
Data ownership identifies which service maintains a set of business data. It does not require a separate physical database server for each service. Even when services temporarily share a database instance, they should avoid directly accessing one another's business tables.
Step 4: Understand business boundaries and bounded contexts
A business boundary defines the rules a service owns and the capabilities it exposes. Other services need to know how to request those capabilities, without understanding their implementation.
Another common term is bounded context: the scope within which a set of business concepts and rules has a specific meaning.
For example, orders and payments both have a status, but the meanings differ:
- An order can be awaiting payment, paid, or canceled.
- A payment can be processing, successful, or failed.
A successful payment does not mean that all subsequent order processing is complete. The shared word “status” is therefore no reason to force both services to use one status field or enumeration.
Bounded contexts help identify business boundaries, but are not deployment units: a bounded context does not necessarily correspond to an independent microservice. For now, understand that different business responsibilities have their own concepts and rules.
⚖️ Evaluating the Service Split
There is no formula for the ideal split. Evaluate it with four questions.
1. Is the functionality within each service closely related?
This is high cohesion: keep related business rules and data together. Order creation, cancellation, and status management belong in the order service, rather than becoming separate services.
2. Does a service depend on another service's internals?
This is low coupling: services can collaborate, but should depend primarily on explicit APIs or message contracts rather than another service's tables, internal functions, or private code. For example, orders calls a stock reservation operation instead of executing inventory SQL directly.
3. Does an ordinary change always require changes to several services?
If adding a display-only order note requires changing inventory, their responsibilities may be entangled. A new sales rule affecting inventory may legitimately require both services to change, however. A cross-service change does not automatically indicate a design error.
4. Are the services too small?
If creating an order requires sequential calls to many services that each own only one table, network traffic and failure handling increase. Small modules that frequently change together and maintain shared business rules can initially stay within one service.
| Area | A reasonable design | A design worth reconsidering |
|---|---|---|
| Responsibilities | Each service owns a clear business capability | Boundaries follow only table names or endpoint counts |
| Cohesion | Related business rules stay together | A simple capability spans many tiny services |
| Coupling | Collaboration uses explicit APIs or events | Services depend directly on other services' tables and code |
| Changes | Ordinary local changes usually affect only their owning service | Unrelated changes require coordinated releases |
🛠 Checking Responsibilities with Order Creation
Test the proposed boundaries with an order for two units. Start with a simplified business flow:
Client submits a product and quantity
↓
Order service receives the order creation request
↓
Order service asks inventory to reserve two units
↓
Inventory checks the quantity and records the reservation result
↓
Order service records an order awaiting payment
↓
Simulated payment service handles payment later
↓
Order service updates order status after receiving the payment outcome
This flow shows who owns each step:
- Orders decides whether to create an order and what status it has.
- Inventory decides whether enough stock exists and how to reserve or release it.
- Simulated payments decides whether a simulated payment succeeds and provides the outcome.
It does not resolve every failure. What if the reservation succeeds but saving the order fails? How should stock be released? What if the order service times out waiting for inventory even though the reservation has already succeeded?
The later articles “Distributed Transactions and Data Consistency” and “Fault Tolerance and Reliability” address these questions. Do not treat the responsibility flow above as a complete transaction implementation ready for production.
❓ Knowledge Check
Q1: Are bounded contexts and microservices the same thing?
No. A bounded context defines where business concepts and rules apply; a microservice runs and deploys independently. They are often related, but need not have a one-to-one mapping.
Q2: Can services share code at all?
Yes. They can share general-purpose, stable utilities that make no business decisions. Avoid making several services maintain a shared business model that changes frequently: a rule change in one service could force changes in others.
Q3: Is an order service calling inventory a form of coupling?
It is a business dependency, but not necessarily a problem. Order creation really does require an inventory result. The goal is to reduce unnecessary dependencies, rather than eliminate every connection.
🧪 Exercise
Suppose the store adds order cancellation. Design the following:
- List the actions owned by the order and inventory services.
- Specify who decides whether an order can be canceled and who releases reserved stock.
- Draw a simplified collaboration flow and mark where stock release could fail.
Acceptance criteria: Every action has a clear owner; orders does not modify the inventory table directly; you can identify at least one cross-service failure requiring further handling.
🎉 Congratulations on completing this article! You can now design service boundaries around business responsibilities, rather than merely splitting a project into multiple folders.
Introduction to Microservices
Understand the differences between monolithic, multiservice, and microservices architectures, learn what service autonomy and independent deployment mean, and decide when microservices are appropriate.
Project Structure and Setup
Build four independent Express services for users, orders, inventory, and simulated payments, understand project structure and shared code boundaries, and verify independent execution.
