Quick revision
The short answer
First introduce a data access or routing layer, so the 15 services stop assuming a single database. Then copy data into the sharded layout with a backfill plus change data capture, verify it with counts and checksums, switch reads gradually per tenant or shard with the option to roll back, and switch writes in short, controlled cutovers. The main risks are missed writes during cutover, cross-shard queries discovered late, and sequences and unique constraints that used to be global, so every step is rehearsed with clear success metrics and rollback criteria.
What to remember
In this answer, we saw how to migrate a live database to a sharded layout without downtime: first a routing layer so services stop assuming one database, then a backfill plus change data capture with continuous verification, a gradual switch of reads, and short scripted write cutovers per tenant or shard with a reverse stream for rollback. We named the main risks, missed writes from bypassing writers, late cross-shard queries and lost global sequences or constraints, and made every step rehearsed with success and rollback criteria.
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.
First introduce a data access or routing layer, so the 15 services stop assuming a single database. Then copy data into the sharded layout with a backfill plus change data capture, verify it with counts and checksums, switch reads gradually per tenant or shard with the option to roll back, and switch writes in short, controlled cutovers. The main risks are missed writes during cutover, cross-shard queries discovered late, and sequences and unique constraints that used to be global, so every step is rehearsed with clear success metrics and rollback criteria.
Why this question is asked
Migrating a live database is often compared to changing the engine of a plane in flight, and with 15 services reading and writing it, the comparison is fair. A big-bang switch over a weekend is tempting but almost always fails, because something nobody knew about breaks at the worst moment. Interviewers ask this to see whether we can break the migration into reversible steps, protect data on the way, and name the risks before they become incidents. In this answer, we will go through the plan step by step: the routing layer, the copy and verification, the read switch, the write cutover, the main risks, and the rehearsals that make it safe.
1Put a Routing Layer in Front of the Database
Today each service opens connections to one database and may assume that every table is in it. Before moving any data, we change that assumption. Every service gets a data access layer, for example a shared library that resolves which data source holds a given tenant, or we put a proxy that can do the routing, such as the router of the sharded system itself (Citus coordinator, Vitess VTGate).
At first the routing layer sends everything to the old database, so nothing changes in behaviour. But now we have one place that can decide, per tenant or per table, where reads and writes go. This step also reveals hidden dependencies: services that join tables across domains, use database-wide sequences or run ad hoc reports. We want to discover those now, not at cutover.
2Copy the Data and Keep It in Sync
Next we build the new layout and fill it without stopping writes. A backfill copies existing rows in batches, tenant by tenant. While it runs, the old database keeps changing, so we also stream changes with change data capture, for example Debezium reading the WAL, and apply them to the new shards. Once the backfill has finished and the change stream has caught up, the new layout is a live copy with a lag of seconds.
Then we verify, and we do it continuously, not once. Row counts per tenant and table, checksums over key columns for chunks of rows, and sampled comparisons of full rows show whether the copy is faithful. A mismatch is investigated before any traffic moves.
- 15 servicesall access through the routing layer
- Routing layerstill points at the old database
- Old databasesource of truth during the copy
- Backfill and CDCcopy history, then stream changes
- Sharded layoutlive copy, a few seconds behind
- Verifiercounts and checksums per tenant
6 layers, from the top down. Step through them one at a time.
3Switch Reads Gradually
Reads are the safest thing to move, because a wrong read can be retried from the old database. We pick a few small internal tenants and route their reads to the new shards, compare results or latency, then widen to 1 percent, 10 percent and so on. A shadow mode helps at the start: the routing layer reads from both and compares answers in the background, logging differences without affecting users.
Because the new layout lags slightly behind the old database while writes still go to the old one, read-your-writes can break for tenants whose reads have moved. So either the read switch for a tenant is followed quickly by its write switch, or reads that follow a user's own write stay on the old database during this phase.
4Cut Over Writes in Short, Controlled Steps
Writes are where data can be lost, so the cutover for each tenant or shard is a short, scripted sequence. We briefly pause writes for that tenant in the routing layer, typically for seconds, wait until CDC has applied the last change, verify the counts, flip the routing so writes go to the new shard, and resume. From then on, the new shard is the source of truth for that tenant, and a reverse change stream back to the old database keeps rollback possible for a while.
- Pause the tenant's writesrequests queue or retry for a few seconds
- Wait for CDC to drainlag reaches zero for this tenant
- Counts and checksums match?YesFlip routing to the new shardNoResume on the old database, investigate
- Resume writesreverse CDC keeps the old copy current
- Watch error rates and latencyroll back within the agreed window if needed
5 steps in order. Play them, or step through them one at a time.
5The Main Risks
The first risk is missed writes during cutover. Any writer that bypasses the routing layer, such as a batch job with its own connection string or a cron script, keeps writing to the old database after the flip, and those rows never reach the new shard. So before the first cutover we make the old database refuse writes for migrated tenants, for example with a trigger or revoked privileges, and we look for connections that do not come from the routing layer.
The second is cross-shard queries discovered late: a report or admin screen that joins all tenants works on the old database and breaks or becomes slow on shards. Logging every query that lacks a shard key during the read phase finds them. The third is anything that used to be global: database sequences, unique constraints across all rows such as a globally unique email, and foreign keys between tables that end up on different shards. Each needs a replacement, such as application-generated ids, a global uniqueness table or a check in a service.
6Rehearse, Measure and Define Rollback
Each step is rehearsed in a production-like environment with a copy of real data volumes and replayed traffic, because timing problems only appear at scale. For each step we write down the success metrics, such as error rate, p99 latency and verification results, and the rollback criteria: what number makes us stop, and how long rolling back takes. The migration then becomes a series of small, boring steps over weeks, which is exactly what we want.
7Follow-up questions
How long should the old database be kept after the last cutover?
Until the reverse change stream has been off for an agreed period without problems, often a few weeks, and the final verification has passed. After that it can be made read-only, archived and removed.
What if a service cannot be changed to use the routing layer in time?
Its tables cannot be migrated until it is, or a proxy must do the routing transparently for it. We order the migration so those tables move last, rather than letting one service block the whole plan.
Would you migrate all tables at once?
No. We move one group of related tables, sharded by the same key, at a time, starting with the simplest and least critical, so the process is proven before it touches the core data.
8Conclusion
In this answer, we saw how to migrate a live database to a sharded layout without downtime: first a routing layer so services stop assuming one database, then a backfill plus change data capture with continuous verification, a gradual switch of reads, and short scripted write cutovers per tenant or shard with a reverse stream for rollback. We named the main risks, missed writes from bypassing writers, late cross-shard queries and lost global sequences or constraints, and made every step rehearsed with success and rollback criteria.
Open with
I would do it in reversible steps. First, put a data access or routing layer in front of the database, either a shared library in the 15 services or the sharded system's own router, initially pointing everything at the old database.
Then, if they want more
That gives me one switch and also surfaces hidden joins, sequences and reports. Second, build the sharded layout and fill it with a backfill plus change data capture, so it becomes a live copy a few seconds behind, and verify continuously with row counts and checksums per tenant.
Third, move reads gradually, starting with internal tenants and a shadow comparison mode, keeping the ability to route back. Fourth, cut over writes per tenant or shard: pause that tenant's writes for a few seconds, wait for CDC to drain, verify, flip the routing, resume, and keep a reverse stream so I can roll back.
The main risks I would plan for are writers that bypass the routing layer and keep writing to the old database, which I would block, cross-shard queries discovered late, which I find by logging queries without a shard key, and global sequences or unique constraints that need replacing.
Every step is rehearsed on production-sized data with defined success metrics and rollback criteria.
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 Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 must migrate a live monolithic database to a sharded layout (application-level...": https://scrivio-interview-library.netlify.app/databases/database-scaling/8378ae0af8/