Our first domain model made the payment system barely better
The first time we applied Domain-Driven Design to a payment system, the transaction failure rate went from 8% to 6%. A whole new architecture, and most of the problem was still there. The second attempt took it to 1%.
The difference between the two was not the pattern. It was who was in the room when we drew the model.
The starting point
The company sold courses on its own e-learning platform in Brazil. Payments lived inside that platform: one monolith handled orders, charges and invoices for credit card, boleto (the Brazilian bank slip) and PIX (the central bank's instant payment system).
Every new payment method or consumer system meant a new point-to-point integration. We had between 6 and 8 of them. Average processing time was 5–25 s, and 8% of transactions failed. Connecting a new consumer system took 3–6 months.
The plan was to move payments out of the monolith into a payment gateway of our own, built in .NET on Azure, with a clean domain model. I was the technical lead and architect.
Attempt one: the model we drew
We ran workshops with the technical, product and integration teams, and we came out with three contexts: Orders, Payments and Invoices.
It looked right on the whiteboard. In practice it was the monolith's structure with new names. An order was an order, whatever the payment method. A payment was one flow for everything.
What was missing were the people who knew how each payment method actually behaves: finance and the business side. Without them we didn't know which rules would show up later, so we didn't leave room for them.
The cost showed up in the code:
- Aggregates and services with unclear responsibilities, so each payment method ended up as conditionals inside the same flow.
- No shared language between teams. The same word meant different things in different parts of the code.
- Integrations still built one by one.
Results after this first version: 5–15 s of processing time, 6% of transactions failing, 5–7 external integrations. Better, but far from what we needed, and with a lot of rework ahead.
Attempt two: one context per payment method
We went back to the domain, this time with the business side involved, and split the model by how each payment method really works:
- Boleto: its own context, with barcode generation, due date tracking and payment clearance.
- Credit card: a separate flow to check the customer's credit limit before the transaction.
- Recurring card payments: a flow of their own for subscriptions and monthly charges, with renewals, customer notifications and payment history.
- PIX: instant validation, refunds and the integration with the central bank's system.
Consumers didn't need to know any of this. We standardised the gateway's API so every system integrated the same way, whatever the payment method. The gateway became the single layer between the company's products and the payment providers, and it also connected to the invoicing systems.
What got harder
Splitting the model did not make everything simpler.
- More moving parts. More order types and payment flows meant more dependencies between them, and more to maintain.
- Boundaries kept leaking. Some rules crossed contexts, and we had to revisit them more than once to keep the contexts consistent.
- Performance. When one operation involved more than one payment method, the extra calls between services added up. We had to optimise those calls to keep response times low.
Results
| Metric | Monolith | First model | Second model |
|---|---|---|---|
| Average processing time | 5–25 s | 5–15 s | 1–3 s |
| Transaction failure rate | 8% | 6% | 1% |
| External integrations | 6–8 | 5–7 | 2 |
Connecting a new consumer system went from 3–6 months to about 1 month.
How we measured: processing time from production logs in an ELK stack, on the same infrastructure across all phases. Failure rate as the share of attempts that did not succeed. So these numbers compare versions of our own system; they are not a benchmark.
What I'd do differently
- Bring the business side in on day one. We modelled what we knew, and we didn't know enough.
- Split by behaviour. "Payment" was too big to be one thing. The useful question was: which payment methods follow different rules?
- Plan for the cost of the split. More contexts mean more calls and more boundaries to look after. That cost is worth paying, as long as it is in the plan from the start.