Quick revision
The short answer
Build a new projection side by side, in a new schema, with a new consumer group reading from the beginning, and switch reads to it only after it has caught up and been verified, so the old one keeps serving. Reading is rarely the limit: 48 partitions at 20 MB/s each is about 1 GB/s even with tiered storage latency, while writing 2 TB of rows is the bottleneck, so bulk load with COPY or large batches, build indexes after the load and skip live-only work. A one-partition trial gives the real rate: at 200 MB/s of effective row writes, 2 TB takes close to 3 hours plus index builds, so 4 hours is tight, and rebuilding only affected ranges is the fallback.
What to remember
In this answer, we rebuilt the projection side by side with a new schema and a new consumer group, so the old projection kept serving until we switched. We saw that reading 48 partitions is fast even from tiered storage, and that writing 2 TB is the real bottleneck, which we reduced with COPY, indexes built after the load and no live-only work. A one-partition trial turned the estimate into a measurement: at 200 MB/s, about 3 hours plus indexes, so 4 hours is tight, with a partial rebuild as the fallback. Verification by counts and checksums comes before the switch, and the procedure becomes a rehearsed runbook.
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.
Build a new projection side by side, in a new schema, with a new consumer group reading from the beginning, and switch reads to it only after it has caught up and been verified, so the old one keeps serving. Reading is rarely the limit: 48 partitions at 20 MB/s each is about 1 GB/s even with tiered storage latency, while writing 2 TB of rows is the bottleneck, so bulk load with COPY or large batches, build indexes after the load and skip live-only work. A one-partition trial gives the real rate: at 200 MB/s of effective row writes, 2 TB takes close to 3 hours plus index builds, so 4 hours is tight, and rebuilding only affected ranges is the fallback.
Why this question is asked
Being able to rebuild a projection from the log is the main promise of keeping events in Kafka: if the derived data is wrong, replay the source. But that promise is only worth something if the replay finishes in time, and with 2 TB of output, three years of events and a business deadline, time is the real question. Interviewers ask this to see whether we can plan a rebuild that does not disturb readers, find the actual bottleneck with arithmetic, and prove the result before switching. In this answer, we will set up a side-by-side rebuild, estimate read and write speeds, speed up the writes, check the estimate with a trial, and verify before cutting over.
1Rebuilding Side by Side
The current projection is partly corrupted but still serving reports, so we must not rebuild it in place. Instead, we create a new schema, for example reporting_v2, with the same tables, and start a new consumer group, projection-rebuild-v2, that reads the order events from the beginning of the topic. The live projector keeps updating the old projection, so users keep getting reports, mostly correct, until the switch.
When the rebuild group has caught up with the head of the topic, it simply continues as the new live projector. We then switch reads, for example by changing a view or a connection setting to point at reporting_v2, and stop the old projector. Rollback is the reverse switch, since the old schema still exists.
7 messages in order. Play them, or step through them one at a time.
2Estimating the Read Side
Reading is rarely the limit. A Kafka consumer can usually read tens of megabytes per second per partition, and with 48 partitions and up to 48 consumers in the rebuild group, even 20 MB/s per partition is about 1 GB/s in total. At that rate, reading the whole three years of events, which in compressed form may be a similar size to the projection, takes well under an hour.
There is one difference with tiered storage. Recent segments are on broker disks, but three-year-old segments live in object storage, so each fetch of old data has higher latency, tens to hundreds of milliseconds, and the brokers must download segments before serving them. We compensate by raising fetch.max.bytes and max.partition.fetch.bytes so each request brings back more data, and by running enough consumers to keep many fetches in flight. It is worth checking the broker's remote fetch settings and the object store's request limits with the platform team before starting.
3The Real Bottleneck: Writing 2 TB
Writing is where the time goes. PostgreSQL must turn 2 TB of rows into heap pages, write-ahead log, indexes and constraint checks. Row-by-row inserts with an index update and a commit for each would take days. So we remove every cost the rebuild does not need.
Load with COPY, or batched inserts of thousands of rows per statement, in large transactions. Create the tables without indexes, foreign keys and triggers, and add them after the load, since building an index once over sorted data is far cheaper than updating it 2 billion times. Consider creating the new tables as UNLOGGED during the load and switching them to LOGGED afterwards, which skips WAL during the load but writes the whole table to the WAL when switched back, so measure it. Turn off any per-record work that only the live path needs, such as notifications or cache invalidation. And raise maintenance_work_mem for the index builds.
4Checking the Estimate With a Trial
An estimate without a measurement is a guess, so we run a trial on one partition, or one month of data, into a scratch schema on the production database server, and measure the effective write rate, the volume of finished rows per second including WAL and checkpoints.
Suppose the database sustains 200 MB/s of effective row writes. Then 2 TB, about 2,000,000 MB, takes 2,000,000 / 200 = 10,000 seconds, close to 2 hours and 50 minutes. Index builds on 2 TB may add 30 to 60 minutes, and verification another 20 or so. That lands at roughly 3.5 to 4 hours, which is tight against the 4-hour target with no room for a retry.
If the trial shows less, say 100 MB/s, the full rebuild takes more than 6 hours, and we change the plan: rebuild only what the bug affected. If the corruption is limited to certain dates or certain customers, we can replay all events but write only rows in the affected ranges, or delete and reload just those ranges in place inside transactions. That often cuts the write volume by an order of magnitude.
| Step | Rate or size | Time |
|---|---|---|
| Read events (48 partitions) | about 1 GB/s | under 1 hour, in parallel |
| Write 2 TB of rows | 200 MB/s measured | about 2 h 50 min |
| Build indexes | after load | 30 to 60 min |
| Verify counts and checksums | per partition and date | about 20 min |
| Total | 3.5 to 4 hours: tight |
5Proving Correctness Before Switching
Before switching, we compare the new projection with the source. For each partition or each day, count the events of each type in Kafka and the rows they should produce, and compare with row counts in reporting_v2. Compute checksums, such as the sum of order amounts per day, from both sides. Spot-check a sample of orders end to end. And compare against the old projection in the ranges that were not corrupted, which should match exactly.
Finally, keep the procedure as a tested runbook and rehearse it, for example twice a year on a staging copy. The log is only a source of truth if replay actually works within the time the business needs, and the only way to know is to have done it.
6Follow-up questions
Why not delete the corrupted rows and replay only the affected events?
That is a good option when the corruption is limited and well understood, and it is much faster. The full side-by-side rebuild is safer when we are not sure how far the bug reached, because it does not depend on finding every bad row.
How does the rebuild group know when to take over?
When its lag on every partition is close to zero, it is processing the same live events as the old projector. At that point both projections receive live updates, and we can switch reads at any moment.
Does running the rebuild hurt the live database?
It can, because it competes for I/O and CPU on the same server. Rate-limit the loaders if live report latency rises, or run the rebuild on a separate database and switch the application to it, if the reporting system allows that.
7Conclusion
In this answer, we rebuilt the projection side by side with a new schema and a new consumer group, so the old projection kept serving until we switched. We saw that reading 48 partitions is fast even from tiered storage, and that writing 2 TB is the real bottleneck, which we reduced with COPY, indexes built after the load and no live-only work. A one-partition trial turned the estimate into a measurement: at 200 MB/s, about 3 hours plus indexes, so 4 hours is tight, with a partial rebuild as the fallback. Verification by counts and checksums comes before the switch, and the procedure becomes a rehearsed runbook.
Open with
I would build a new projection next to the old one, in a new schema, with a new consumer group reading from the beginning of the topic, while the old projector keeps serving reads.
Then, if they want more
When the new one has caught up and been verified, I switch reads to it, and the old schema stays for rollback.
For the estimate, reading is rarely the limit: 48 partitions at even 20 megabytes a second each is about a gigabyte a second, although old segments come from object storage with higher latency, so I would raise fetch sizes and run up to 48 consumers. The real bottleneck is writing 2 terabytes of rows into PostgreSQL.
I would bulk load with COPY or large batched inserts, create indexes and constraints after the load, and turn off anything only the live path needs. Then I would run a trial on one partition to measure the real write rate.
If the database sustains about 200 megabytes a second, 2 terabytes takes close to 3 hours plus index builds, so 4 hours is tight. If it is lower, I would rebuild only the affected dates or keys.
Before switching, I would compare row counts and checksums per partition or per day with the events, and keep the whole thing as a tested runbook, because the log is only a source of truth if replay works.
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 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
- Apache Kafka 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 "A bug corrupted part of the projection, and the business wants it rebuilt from the log...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/3cca38c21f/