Quick revision
The short answer
We extract billing in stages that each keep the system working: first make billing a clean module inside the monolith with its own package boundary and interfaces, then separate its tables so no other module reads them, then deploy billing as a service behind the same interface and route calls to it gradually (the strangler fig approach). Data moves with replication or dual writes plus verification before ownership switches, and every step keeps a rollback path while we watch errors and latency.
What to remember
In this answer, we saw how to extract billing without a risky big-bang switch. We first made billing a real module with a public interface, checked by Spring Modulith, then separated its tables, then put the new service behind the same interface and routed calls to it gradually as a strangler fig. We moved the data with replication or dual writes plus verification before switching ownership, and we kept a rollback path and clear metrics at every stage.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We extract billing in stages that each keep the system working: first make billing a clean module inside the monolith with its own package boundary and interfaces, then separate its tables so no other module reads them, then deploy billing as a service behind the same interface and route calls to it gradually (the strangler fig approach). Data moves with replication or dual writes plus verification before ownership switches, and every step keeps a rollback path while we watch errors and latency.
Why this question is asked
Extracting a module from a monolith is one of the most common migrations a senior engineer leads, and one of the easiest to get wrong. The failure pattern is familiar: a team copies the billing code into a new repository, points it at a new database, switches traffic in one night, and spends the following month reconciling invoices. Interviewers ask this question to see whether we can break a risky change into small reversible steps and whether we treat the data as the hard part. In this answer, we will walk through the steps in order: drawing the module boundary, separating the data, putting the service behind the same interface, moving the data safely, and switching ownership with rollback at every stage.
1Make Billing a Real Module Inside the Monolith
Before billing can live in its own process, it has to stop being tangled with the rest of the code. In a typical Spring Boot monolith, an OrderService calls InvoiceRepository directly, a reporting job joins the invoice table with orders, and a billing class reaches into customer entities. If we moved billing out as it is, every one of those calls would become a network call, or simply break.
So the first step happens entirely inside the monolith. We give billing its own top-level package, define a small public interface such as BillingApi with the operations other modules actually need, and make everything else in the package internal. Other modules call BillingApi and nothing else. Spring Modulith can check this for us: it treats each direct sub-package of the application's main package as a module, and a test fails when one module uses another module's internal types.
This step is cheap to roll back, because it is ordinary refactoring in one codebase, and it makes every later step smaller. It is also where we discover the surprises, such as a nightly job that updates invoice rows directly.
2Separate the Billing Tables
Code boundaries are not enough if other modules still read billing's tables. A report that joins orders with invoices in SQL will break the moment invoices live in another database. So we find every query that touches billing tables from outside the module and replace it with a call to BillingApi, or with a read model that billing keeps up to date through events.
A good intermediate step is to move billing's tables into their own schema in the same database and give the monolith's other modules a database user that cannot read that schema. The database then enforces the rule for us. Nothing has left the monolith yet, and rollback is still a configuration change.
3Put the New Service Behind the Same Interface
Now we create the billing service as its own Spring Boot application, exposing the same operations as BillingApi over HTTP or messaging. Inside the monolith we write a second implementation of BillingApi that calls the new service. This is the strangler fig pattern: the new system grows around the old one and takes over piece by piece, like the plant it is named after, until the old part can be removed.
The switch between the two implementations is a feature flag, not a deployment. We start by sending a small share of calls, perhaps read-only status checks for 1 percent of customers, to the new service, and compare the answers with the old implementation. Then we increase the share step by step, 5, 25, 50 and 100 percent, watching error rates and latency at each step.
- Caller uses BillingApiOrders module does not know which implementation runs
- Flag says new service for this call?YesHTTP adapter calls billing serviceNoLocal module implementation runs
- Compare results and metricserrors, p99 latency, mismatches
- Raise the share or roll back1, 5, 25, 50, 100 percent
4 steps in order. Play them, or step through them one at a time.
4Move the Data and Switch Ownership
The data move is the riskiest part, so we split it too. One common approach is change data capture: a tool such as Debezium reads the monolith database's change log and replicates billing rows into the new service's database continuously. Another is dual writes, where the monolith writes to both stores for a period. Dual writes are simpler to start but can drift if one write fails, so if we use them we write to one store as the source of truth and repair the other with a reconciliation job.
Either way, we verify before we trust. A job compares counts, sums of amounts per day and a sample of full rows between the old and new stores, and reports any mismatch. Only when the numbers agree for an agreed period, say a full billing cycle, do we switch ownership: the new service becomes the source of truth, writes go there first, and replication runs in the other direction for a while so the old tables still hold correct data if we need to go back.
| Stage | Source of truth | How we roll back |
|---|---|---|
| Module inside monolith | Monolith tables | Revert the refactoring |
| Separate schema | Monolith, billing schema | Restore database grants |
| Service reads, replicated data | Monolith tables | Flag back to local implementation |
| Service owns writes | Billing service database | Reverse replication keeps old tables current |
| Old code removed | Billing service database | Only by a new migration |
5Keeping the System Working Throughout
Two habits hold the whole plan together. The first is that every stage must be reversible until the very last one; if we cannot describe how to go back, the step is too big. The second is measurement. Before we start, we record a baseline for invoice creation errors, p99 latency of billing calls and the number of invoices per day, and we compare against it after each step. A sudden drop in invoices per day is often the first sign of a silent failure.
Billing also needs care because it touches money. Calls to create invoices should carry an idempotency key, a unique id the caller sends so that a retried request does not create a second invoice. That matters the moment a local method call becomes a network call that can time out after the work was actually done.
6Follow-up questions
Why not build the new service first and migrate everything in one go?
Because a single switchover combines every risk into one night: code differences, data differences and operational problems all appear at once, and rolling back means moving data back under pressure. The staged approach exposes each risk separately while the old path still works, so a problem costs us a flag change instead of an incident.
What do you do about transactions that used to span billing and orders?
Inside the monolith, creating an order and its invoice may have been one database transaction. After the split, they cannot be. We usually make order creation publish an event and let billing create the invoice from it, with the transactional outbox pattern so the event is never lost, and we design for the short period when the order exists without an invoice.
How long should the old billing tables be kept?
Until we have run at least one full business cycle, such as a month-end close, on the new service without needing them, and until auditors or finance agree. After that we stop reverse replication, archive the tables and remove the old code.
7Conclusion
In this answer, we saw how to extract billing without a risky big-bang switch. We first made billing a real module with a public interface, checked by Spring Modulith, then separated its tables, then put the new service behind the same interface and routed calls to it gradually as a strangler fig. We moved the data with replication or dual writes plus verification before switching ownership, and we kept a rollback path and clear metrics at every stage.
Open with
I would do it in stages, each of which keeps the system working and can be rolled back. First I make billing a proper module inside the monolith: its own package, a small public interface like BillingApi, and everything else internal.
Then, if they want more
I would use Spring Modulith's verification test so the build fails if another module reaches into billing internals. Second, I separate the data: no other module may query billing tables, so reports and joins go through the API or a read model, and I often move the tables into their own schema with database grants to enforce it.
Third, I build the billing service exposing the same operations and add a second implementation of the interface in the monolith that calls it. A feature flag routes a small share of traffic to the new service, and I raise it gradually while comparing results, errors and latency. That is the strangler fig approach.
For the data, I replicate with change data capture or use dual writes with one source of truth, and I verify counts and amounts before switching ownership. After the switch I keep reverse replication for a while so I can still roll back.
Throughout, I add idempotency keys for invoice creation and watch a baseline of business metrics like invoices per day.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Cloud documentation, Resilience4j documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You are asked to extract the billing module from a Spring Boot monolith into its own...": https://scrivio-interview-library.netlify.app/spring-boot/microservice-design/b958d969b1/