Quick revision
The short answer
We first measure where the cost goes: WAL bytes per row, index maintenance, and vacuum work on rows that are later deleted. Most of these problems come from how the table is used, so we try batching with COPY, dropping unused indexes, time-ordered keys, larger checkpoint distance, WAL compression and time-based partitioning with DROP PARTITION instead of DELETE. Only if the workload still outgrows PostgreSQL on projected growth, and needs no joins or transactions, do we plan a move to a wide-column store, with the migration cost written next to the tuning results.
What to remember
In this answer, we treated the problem as a diagnosis before a migration. We used pg_stat_wal and index and table statistics to see where the 390 bytes of WAL per row and the vacuum work come from, then applied fixes in order of effort: batched COPY, fewer indexes, time-ordered keys, larger checkpoint distance and WAL compression, and daily partitions dropped instead of deleted. A move to a wide-column store is justified only for an append-only, key-and-time workload whose projected growth outruns a tuned PostgreSQL, and only after its costs are written next to the tuning results.
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 first measure where the cost goes: WAL bytes per row, index maintenance, and vacuum work on rows that are later deleted. Most of these problems come from how the table is used, so we try batching with COPY, dropping unused indexes, time-ordered keys, larger checkpoint distance, WAL compression and time-based partitioning with DROP PARTITION instead of DELETE. Only if the workload still outgrows PostgreSQL on projected growth, and needs no joins or transactions, do we plan a move to a wide-column store, with the migration cost written next to the tuning results.
Why this question is asked
Proposals to replace a database usually start from a real pain, here replica lag and a vacuum that never finishes, but they often skip the step of finding out whether the pain is a limit of the engine or a consequence of how the table is used. Interviewers ask this scenario to see whether we can diagnose before we prescribe, and whether we know the PostgreSQL mechanics well enough to name the cheap fixes. In this answer, we will recap WAL, checkpoints and vacuum, measure where the cost goes, apply the fixes in order of effort, decide when a migration is justified, and discuss the follow-up about replica lag.
1A Quick Recap of WAL, Checkpoints and Vacuum
PostgreSQL writes every change first to the write-ahead log, the WAL, so it can recover after a crash. Replicas apply the same WAL, so replica lag is roughly WAL volume divided by how fast the replica can apply it. Every few minutes a checkpoint flushes dirty pages to the data files. After a checkpoint, the first change to any page writes a full copy of that 8 KB page into the WAL, called a full-page write, to protect against torn pages. So frequent checkpoints and changes scattered over many pages inflate the WAL a lot.
PostgreSQL also uses MVCC: an UPDATE or DELETE leaves the old row version in place, and autovacuum later reclaims the space and freezes old transaction ids. On a table that receives 60,000 inserts a second and deletes old rows with DELETE, vacuum has to visit billions of dead rows and every index entry pointing to them. If it cannot finish before more dead rows arrive, it never finishes.
2Measuring Where the Cost Goes
Two TB of WAL a day at 60,000 rows a second is about 390 bytes of WAL per row. That number alone is a useful clue: if the rows themselves are 150 bytes, more than half of the WAL is something else, usually full-page writes and index updates. We can measure it directly. pg_stat_wal shows full-page images versus total records, pg_stat_user_indexes shows indexes with zero scans that are still maintained on every insert, and pg_stat_user_tables shows dead tuples and how long since the last vacuum.
Then we look at the keys. A random UUIDv4 primary key spreads inserts across the whole index, so every insert dirties a different index page, each one triggers a full-page write after a checkpoint, and the index stops fitting in memory. A time-ordered key such as UUIDv7 or a bigint sequence writes to the right-hand edge of the index, touching a handful of pages. With five indexes on the table, two of them unused, the difference is large.
3The Cheap Fixes, in Order
Batching comes first. Sixty thousand single-row INSERT statements a second means sixty thousand commits, each with its own WAL flush and round trip. Multi-row INSERT or COPY in batches of a few hundred to a few thousand rows cuts per-row overhead sharply. From Java, the PostgreSQL JDBC driver's CopyManager or reWriteBatchedInserts=true does this with little code.
Next we drop unused indexes and switch to time-ordered keys. Then we reduce full-page writes by spacing checkpoints out: raising max_wal_size and checkpoint_timeout (for example to 50 GB and 15 minutes) means each page is written in full once per longer interval. The trade-off is longer crash recovery, which is usually acceptable. Turning on wal_compression (lz4 or zstd in recent versions) compresses those full-page images, often by half or more.
The biggest fix for vacuum is partitioning by time. With declarative partitioning, the events table becomes one partition per day. Removing old data is then DROP TABLE on a partition, or DETACH PARTITION first, which takes milliseconds and leaves no dead rows. Vacuum only has to deal with the current partitions, which are small, and a BRIN index on the timestamp is tiny compared with a B-tree.
| Fix | Cost it removes | Effort |
|---|---|---|
| COPY or multi-row INSERT | per-statement commit and WAL flush | small code change |
| Drop unused indexes | index writes and WAL per row | one migration |
| Time-ordered keys | random index pages and full-page writes | schema change for new rows |
| Larger max_wal_size, wal_compression | full-page write volume | config and restart check |
| Partition by day, DROP old | DELETE plus vacuum of dead rows | migration of the table |
4When a Migration Is Justified
After tuning, we measure again: WAL per row, replica lag at peak, vacuum duration. Then we look forward. If the table is append-only, read only by time and key, needs no joins or transactions, and growth projections show 5 or 10 times today's rate within two years, then a wide-column or time-series store such as Cassandra, ScyllaDB or a dedicated time-series database is a reasonable fit. The decision should rest on that projection, not on today's tuning gaps, because a tuned PostgreSQL often carries 60,000 rows a second comfortably.
We also write down what a migration costs: dual writes or CDC during the transition, a backfill, a new system for the team to operate and debug, and the loss of SQL for analysts who query this table today. Put next to the tuning results, that list usually makes the decision clear one way or the other.
5Follow-up questions
What replica lag would you accept for this data, and who reads from the replicas?
It depends on the readers. If replicas serve dashboards over the last hour, a few seconds of lag is invisible and even 30 seconds is fine. If a product feature reads its own writes from a replica, any lag is a bug, and those reads should go to the primary. A replica used for failover is different again: its lag is the data we could lose or wait for during a failover, so we would set an alert well below a minute and keep that replica on a fast path.
Would synchronous_commit = off help?
It removes the WAL flush wait from each commit and can raise insert throughput a lot, at the risk of losing the last few hundred milliseconds of commits on a crash, without corruption. For event ingestion where producers can resend, that trade can be acceptable, but it does not reduce WAL volume or replica lag, so it is not a fix for the main problems here.
Why not just add more replicas?
More replicas spread reads but do not help writes or WAL volume; every replica must apply the full 2 TB a day. If replicas lag because the apply process is single-threaded and slow, more of them lag in the same way.
6Conclusion
In this answer, we treated the problem as a diagnosis before a migration. We used pg_stat_wal and index and table statistics to see where the 390 bytes of WAL per row and the vacuum work come from, then applied fixes in order of effort: batched COPY, fewer indexes, time-ordered keys, larger checkpoint distance and WAL compression, and daily partitions dropped instead of deleted. A move to a wide-column store is justified only for an append-only, key-and-time workload whose projected growth outruns a tuned PostgreSQL, and only after its costs are written next to the tuning results.
Open with
Before agreeing to a migration I would find out whether PostgreSQL is at its limit or whether we are using it badly. Two terabytes of WAL a day at 60,000 rows a second is about 390 bytes per row, so I would check pg_stat_wal for full-page images, look for unused indexes that are still written on every insert, and check whether the primary key is a random UUID that scatters index writes.
Then, if they want more
Then I would try the cheap fixes: batch with COPY or multi-row inserts instead of single-row statements, drop unused indexes, move to time-ordered keys, raise max_wal_size and checkpoint_timeout so pages are fully logged less often, and turn on WAL compression.
The vacuum problem usually comes from deleting old rows, so I would partition the table by day and drop old partitions, which leaves no dead rows. After that I would measure again and look at the growth projection.
If the data is append-only, read by time and key, needs no joins, and will grow several times, Cassandra or a time-series store is reasonable, but I would write down the migration cost next to it: dual writes, backfill, new operations skills and lost SQL for analysts.
About this answer
- Last updated
- 13 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 official documentation for the version you use. 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 "How do you decide whether this is a limit of the relational database or of how it is...": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/f8df743b5f/