In a restaurant kitchen, "chicken" is an ingredient with a weight and a way to cut it. In the storeroom, "chicken" is stock with an item code and an expiry date. At the till, "chicken" is a menu item at $7.50. All three are right in their own department. Force one shared "chicken" record on every department, and every change in one department breaks another.
Domain-Driven Design (DDD) starts from that observation. Software is modelled after the language of its business, then split into several bounded contexts: areas that each have their own model and their own dictionary of words. For microservices, DDD answers the most expensive question: where the service boundaries go.
Bounded context and ubiquitous language
A bounded context is the boundary inside which one model applies. Inside it, every word has one meaning that developers and business people agree on. Those words are called the ubiquitous language (a shared language): used exactly the same way in conversation, documents, the UI, and class names in the code. If the code calls it order, the documents "transaction", and the UI "booking", people will misunderstand each other.
TicketTune has five contexts, the same as the services we already know: account (login), concert (events and seats), order (orders), payment (payments), and ticket (e-tickets and the entry gate).
One word, different meanings: ticket
The word "ticket" is used by three contexts, but it means different things:
| Context | Name in the model | What it holds | "price" means | "status" means |
|---|---|---|---|---|
concert |
Seat |
A-12, VIP category | today's selling price | AVAILABLE, HELD, SOLD |
order |
OrderItem |
a line in order #1001 | the price Budi paid | (follows the order's status) |
ticket |
ETicket |
a pass through the gate, holder's name | (none) | ACTIVE, SCANNED |
Now imagine all three sharing one ticket table with a price column and a status column. VIP has 500 seats, 498 sold (including Budi's A-12 and A-13), 2 left.
| What happens | One shared table | One model per context |
|---|---|---|
| The VIP price goes up to $90 | total #1001 = 2 × $90 = $180, but $150 was paid | Seat.price $90, OrderItem.priceAtPurchase stays $75, total $150 |
| The gate scans A-12 | A-12's status becomes SCANNED, not SOLD: 3 VIP seats count as left, A-12 can be sold again | ETicket.status SCANNED, Seat.status stays SOLD, 2 VIP seats left |
So order reads the total for #1001 as $180. Budi could get an "underpaid by $30" bill for an order he already paid. The problem isn't a bug on one line of code, but one column forced to hold two meanings. With one model per context, order freezes the price at purchase, and ticket has a status of its own.
One bounded context, one service
Ideally one bounded context becomes one service, with its own database. Other contexts don't read its tables; they ask through an API or listen for events, and only keep its IDs (user_id, seat: "A-12"). A database shared between contexts makes the boundary fake: a change in one service breaks another, like the ticket table above.
The same goes for "user". In account it holds an email and a passwordHash; in order a name and the email the e-ticket goes to; in payment a card token. That's normal: every context has its own user model and they don't share a database. Give them different names to make it clear, for example AccountUser, Buyer, Cardholder. One User class holding every field for every context is called a god model, and every team ends up depending on it.
Aggregate: the rule keeper
An aggregate is a group of objects treated as one unit and guarded by one door, the aggregate root. Every change goes through its methods, so a business rule (called an invariant) can't be broken. A TicketTune order has two rules: at most 4 tickets per order, and an empty order can't be submitted.
// The Order aggregate: Order guards its own rules
class Order {
#tickets; // private: only the methods below can change it
constructor(id, qty) {
this.id = id;
this.#tickets = qty;
this.status = "DRAFT";
}
addTickets(n) {
if (this.#tickets + n > 4) throw new Error("An order holds at most 4 tickets");
this.#tickets += n;
}
submit() {
if (this.#tickets === 0) throw new Error("An empty order can't be submitted");
this.status = "WAITING_FOR_PAYMENT";
}
get qty() {
return this.#tickets;
}
}
const p = new Order(1002, 3); // Siti's draft: 3 tickets
p.addTickets(1); // checkout: now 4
try {
p.addTickets(3); // admin import: through the same method
} catch (e) {
console.log("rejected:", e.message); // rejected: An order holds at most 4 tickets
}
p.submit();
console.log(p.qty, p.status); // 4 WAITING_FOR_PAYMENT
#ticketsis a private field: code outside the class can't change it directly.addTicketschecks the rule before changing anything. The checkout path and the admin import path both go through it.submitrejects an empty order and moves the status toWAITING_FOR_PAYMENT.
The opposite is an anemic domain model: the Order class holds only data (getters and setters), and its rules are written in OrderService. It looks tidy, but another path, say an admin import that writes order.tickets directly, skips the rules. In the Lab, the admin import leaves order #1002 with 6 tickets. The heart of DDD: logic lives in the domain object, not scattered across services.
Context map: how contexts relate
A context map draws how contexts depend on each other. The four most common relationships:
- Customer-Supplier: one context depends on another's output.
order(the customer, downstream) usesconcert'sHoldSeatsAPI (the supplier, upstream), and theconcertteam plans for whatorderneeds. - Anti-Corruption Layer (ACL): a translating layer at the edge.
paymentreceives an outside payment gateway's webhook sayingtransaction_status: "settlement"; the ACL turns it into aPaymentwith status SUCCESSFUL, so the gateway's terms don't spread throughpayment's code. - Published Language: an agreed, published exchange format, for example a versioned JSON schema for the
OrderPaidevent thatticketand reporting read. OpenAPI or protobuf contracts count too. - Shared Kernel: two contexts share a small part of the model, for example a
Moneymodule used byorderandpayment. Cheap, but every change needs both teams to agree, so keep it as small as possible.
Strategic first, tactical later
DDD has two layers:
- Strategic DDD (the most important): bounded contexts, ubiquitous language, context maps. This is what decides service boundaries.
- Tactical DDD (optional): entities, value objects, aggregates, repositories, domain events. This is how you write code inside one context.
A team that neatly uses value objects and repositories, yet still has to change many services for every new feature, has usually skipped the strategic part: its context boundaries are wrong. Tactical without strategic is a lot of ceremony that doesn't help.
DDD doesn't require microservices either. A modular monolith with clear bounded contexts (Microservices vs Monolith lesson) is often the sweet spot. The order is: DDD first, microservices if you really need them. Once you do, the contexts whose boundaries are already clear are the ones you move out one by one (Monolith to Microservices lesson).
🎯 How to start with DDD
- Event storming: developers and business people write domain events on sticky notes, for example SeatsHeld, CardCharged, ETicketScanned.
- Group the events that belong together: those are candidate bounded contexts.
- Agree on a ubiquitous language per context, and use it in the code, the documents and the UI.
- Pick the aggregates and their invariants in each context.
- Draw the context map: where an ACL is needed, who is upstream and downstream.
- Build it as a modular monolith first; split it into services when the organisation needs it.
⚠️ Context boundaries that only look right
- A database shared between bounded contexts. The boundary is fake: one
priceorstatuscolumn is forced to hold two meanings. - "User" as a god model. Give each context its own model:
AccountUser,Buyer,Cardholder. - An anemic domain model. A rule in a service doesn't guard the other paths. Put it in the aggregate.
- Tactical without strategic. Neat value objects and repositories don't help when the context boundaries are wrong.
- An inconsistent language. "order" in the code, "transaction" in the documents, "booking" in the UI means three people picturing three things.
TL;DR: DDD models software after the business language and splits it into bounded contexts, each with its own model and ubiquitous language. In TicketTune, "ticket" is a Seat in concert, an OrderItem in order, and an ETicket in ticket. If all three share one table, a VIP price of $90 makes Budi's paid order read $180, and a scanned e-ticket makes seat A-12 count as free. Ideally one context becomes one service with its own database; a different user model per context is normal. An aggregate guards its invariants through its methods (Order.addTickets rejects the 5th ticket); a rule placed in a service (an anemic domain model) can be skipped by another path. A context map charts the relationships: Customer-Supplier, Anti-Corruption Layer, Published Language, Shared Kernel. Strategic DDD (context boundaries) matters more than tactical DDD, and it's DDD first, microservices if needed.
⚡ Bounded context
Meaning: A bounded context is the boundary inside which one model applies, with its own dictionary of words (the ubiquitous language) used exactly the same way in conversation, documents, the UI and the code. TicketTune has five: account, concert, order, payment and ticket.
⚡ One word, different meanings
Guess first: three contexts share one ticket table with one price column. The VIP price goes up to $90. What does the total of Budi's order #1001, paid at $150, now read?
$180. Budi could get an "underpaid by $30" bill for an order he already paid.
Meaning: One model per context: Seat.price may change, OrderItem.priceAtPurchase is frozen at purchase.
⚡ A different user model per context is normal
Meaning: In account a user holds an email and a passwordHash, in order a name and the email the e-ticket goes to, in payment a card token. Every context has its own user model and they don't share a database: AccountUser, Buyer, Cardholder.
⚡ An aggregate guards its rules
Write:
addTickets(n) {
if (this.#tickets + n > 4) throw new Error("An order holds at most 4 tickets");
this.#tickets += n;
}
Meaning: Every change goes through the aggregate root's methods, so the at-most-4-tickets rule can't be broken by any path. In an anemic domain model the rule lives in a service, and another path can skip it.