Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

You must migrate a 2 TB MySQL 8.0 database to PostgreSQL with less than 15 minutes of downtime. Outline the plan.

We convert the schema and types carefully, do an initial bulk load, and then keep PostgreSQL in sync with change data capture from MySQL's binlog using Debezium, AWS DMS or similar. While both run, we compare data with row counts and checksums, replay read traffic to test queries and plans, and rewrite SQL that relied on MySQL behaviour. Cutover is short: stop writes, let replication catch up, verify, switch connection strings, and keep reverse replication ready for rollback.

Why this question is asked

Two terabytes cannot be dumped and loaded in 15 minutes; a plain copy would take hours, and the business would be down for all of them. The way to meet the target is to do almost all the work while MySQL stays live, so that the downtime window only covers the final switch. Interviewers ask this scenario to see whether we know the pieces of an online migration, the type and behaviour differences between the engines, and how to prove the data matches before we switch. In this answer, we will convert the schema, load and replicate the data, validate it, fix MySQL-specific SQL, and plan the cutover and rollback.

1Converting the Schema and Types

The schema conversion is where most silent errors start, so we do it by hand with a tool's help, not blindly. Tools such as pgloader or AWS SCT produce a first draft; we then review each type. Unsigned integers have no direct equivalent: an INT UNSIGNED can exceed PostgreSQL's integer, so it becomes bigint, and a BIGINT UNSIGNED may need numeric(20). TINYINT(1) used as a flag becomes boolean. DATETIME has no time zone and maps to timestamp, while MySQL TIMESTAMP values are stored in UTC and fit timestamptz; we decide per column and make the time zone handling explicit. Zero dates such as '0000-00-00' are invalid in PostgreSQL and must be cleaned or mapped to NULL.

AUTO_INCREMENT columns become identity columns, and after the load each identity's sequence must be set past the current maximum, or the first insert on PostgreSQL will collide. Collations need a decision too: MySQL 8's default is case-insensitive, so unique indexes on emails or usernames may contain values PostgreSQL would treat as distinct, and queries may rely on case-insensitive matching. Identifiers are folded to lower case in PostgreSQL unless quoted.

Type and behaviour mapping
MySQL 8.0PostgreSQLWatch out for
INT UNSIGNEDbigintvalues above 2,147,483,647
BIGINT UNSIGNEDnumeric(20) or bigint if safevalues above 2^63 - 1
TINYINT(1)booleancode comparing with 1 and 0
DATETIME / TIMESTAMPtimestamp / timestamptztime zone shifts, zero dates
AUTO_INCREMENTidentity columnsetval past max id after load
utf8mb4_0900_ai_cidefault collation, citext or lower()case-insensitive matches and uniques
ENUMtext with CHECK, or enum typeordering by enum position

2Bulk Load, Then Change Data Capture

The data moves in two phases. First an initial load copies the existing 2 TB while MySQL keeps serving traffic. Then change data capture, CDC, reads MySQL's binary log, the binlog, which records every row change, and applies those changes to PostgreSQL continuously. Debezium's MySQL connector does this into Kafka, from where a sink connector writes to PostgreSQL; AWS DMS does a full load plus ongoing replication in one service. Either way MySQL must have binlog_format = ROW (the default in 8.0) with full row images, and the binlog retention must be long enough to cover the whole initial load.

The key detail is consistency between the two phases: the CDC stream must start from the binlog position that matches the snapshot the initial load used, so no change is missed or applied twice. Debezium records this position as part of its snapshot, and DMS handles it in full-load-and-CDC mode. We load with indexes and foreign keys created after the bulk copy where possible, because that makes the load much faster, then create them before CDC catches up.

Step throughOnline migration with CDC
AppMySQLCDC toolPostgreSQL
reads and writes continue
snapshot at binlog position P
bulk load 2 TB, then build indexes
read binlog from position P
apply changes, lag falls to seconds
cutover: writes stopped
final changes applied, lag 0
connection strings switched, writes resume
reverse replication for rollback

9 messages in order. Play them, or step through them one at a time.

Hours of copying happen while MySQL is live; only the last four steps are inside the downtime window.

3Running in Parallel and Proving the Data Matches

Once CDC keeps PostgreSQL within seconds of MySQL, we run both for days or weeks. During this time we validate the data at several levels. Row counts per table are the cheap first check. Checksums go further: we split each table into primary key ranges, compute a hash over the normalised values of each range on both sides, and compare the results, drilling into any range that differs. Normalisation matters, because booleans, timestamps and decimals are formatted differently by the two engines and must be compared as values, not raw text.

We also test behaviour, not just data. Replaying a sample of real read traffic against PostgreSQL shows whether queries return the same results and how they perform; PostgreSQL's planner will choose different plans, and some queries will need new indexes. We look at p99 latency for the top queries before cutover, not after.

SQL
-- Same chunk on both sides; compare the two results
-- MySQL
SELECT COUNT(*), SUM(CRC32(CONCAT_WS('|', id, customer_id, total_cents, status))) FROM orders WHERE id BETWEEN 1 AND 100000;
-- PostgreSQL (needs a matching hash; often done in a comparison tool instead)
SELECT count(*), md5(string_agg(concat_ws('|', id, customer_id, total_cents, status), ',' ORDER BY id)) FROM orders WHERE id BETWEEN 1 AND 100000;
-- counts must match exactly; hashes are compared by a tool that computes the same function on both sides

Notice that the two engines do not share hash functions, so in practice a comparison tool reads both sides, normalises each row and computes the same hash in its own code. The SQL above shows the idea: compare counts and a digest per key range.

4Rewriting SQL That Depends on MySQL

MySQL is permissive in ways PostgreSQL is not. It casts implicitly, so WHERE varchar_column = 123 works and '12abc' compares equal to 12, whereas PostgreSQL raises an error for comparing text with an integer. Older code may use loose GROUP BY, selecting columns that are neither grouped nor aggregated; MySQL 8 forbids this by default with ONLY_FULL_GROUP_BY, but some installations disable it, and PostgreSQL always rejects it. ON DUPLICATE KEY UPDATE becomes INSERT ... ON CONFLICT (key) DO UPDATE, and functions like IFNULL, GROUP_CONCAT and DATE_FORMAT need equivalents.

We find this SQL by searching the code for native queries and by running the replayed traffic against PostgreSQL and collecting errors. Each fix is deployed so that it works on both engines, where possible, before the cutover, which keeps the cutover itself free of code changes.

5Cutover and Rollback

The cutover is rehearsed in staging until it fits comfortably inside 15 minutes. On the day, we stop writes, by putting the application in maintenance or read-only mode, and confirm in MySQL that no writes are arriving. We wait for CDC lag to reach zero, typically seconds, and run quick final checks: row counts on the main tables and a checksum on recently changed ranges. We set identity sequences past the maximum ids, switch the application's connection strings to PostgreSQL through configuration, and resume writes.

Rollback must be ready before we start. We set up reverse replication from PostgreSQL to MySQL, for example with Debezium reading PostgreSQL's logical decoding, so that writes made on PostgreSQL after the cutover also reach MySQL. If a serious problem appears in the first hours or days, we can switch back without losing those writes. Once PostgreSQL has run cleanly for an agreed period, we stop the reverse path and retire MySQL.

6Follow-up questions

How do you validate that the data matches?

In layers. Row counts per table first, then checksums over normalised values per primary key range, compared by a tool that computes the same hash on both sides, with drill-down into any range that differs. Finally, application-level checks: replayed reads return the same results, and business totals such as sums of order amounts per day match. Validation runs repeatedly during the parallel period and once more, on recent ranges, during the cutover.

What if CDC lag does not reach zero during cutover?

Then writes are not fully stopped, or the apply side is too slow. We confirm no writes arrive at MySQL and check the sink's throughput. Rehearsals should have shown how long catch-up takes; if it exceeds the window, we abort and keep MySQL as primary.

What about tables without primary keys?

CDC and chunked validation both need a key. We add primary keys to those tables before the migration, or a unique key that can serve as one.

7Conclusion

In this answer, we planned a 2 TB MySQL to PostgreSQL migration with under 15 minutes of downtime. We converted types and behaviour carefully, did a bulk load followed by CDC from the binlog, ran both systems in parallel while validating with row counts, checksums and replayed reads, rewrote SQL that relied on MySQL's permissiveness, and cut over by stopping writes, waiting for zero lag, switching connection strings and keeping reverse replication ready for rollback.

What to say in the interviewAbout 80 seconds aloud

Open with

The only way to fit 2 TB into a 15-minute window is to do the copying while MySQL stays live. First I would convert the schema carefully: unsigned integers to wider types, tinyint flags to boolean, DATETIME and TIMESTAMP to timestamp or timestamptz with explicit time zone handling, zero dates cleaned up, AUTO_INCREMENT to identity columns, and a decision on case-insensitive collations.

Then, if they want more

Then an initial bulk load followed by change data capture from the binlog, with Debezium or AWS DMS, starting from the exact binlog position of the snapshot so nothing is missed.

With PostgreSQL seconds behind, I would run both in parallel and validate: row counts, checksums over normalised values per primary key range compared by a tool, and replayed read traffic to compare results and query plans.

I would also find and fix SQL that relies on MySQL behaviour, like implicit casts, loose GROUP BY and ON DUPLICATE KEY UPDATE, ideally deploying fixes that work on both. The cutover is rehearsed: stop writes, wait for lag zero, quick final checks, set sequences past the max ids, switch connection strings, resume writes.

And I would have reverse replication to MySQL running so we can roll back without losing writes.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8
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.
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 2 TB MySQL 8.0 database to PostgreSQL with less than 15 minutes of...": https://scrivio-interview-library.netlify.app/databases/database-operations/0405231c31/