Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

After a data-centre power cut, one broker with 4,000 partition replicas on two disks takes 40 minutes to start; its log shows it loading and recovering log segments partition by partition while it stays out of every ISR.

Why does this take so long, and what would you change?

After a power cut the broker shut down uncleanly, so it cannot trust anything written after the recovery point it last checkpointed. For each of its 4,000 replicas it re-reads the unflushed segments, validates every batch CRC, truncates torn writes and rebuilds the indexes, and because Kafka does not fsync each write that can be a lot of data. With num.recovery.threads.per.data.dir at a low default and only two disks, the work is nearly serial. We would raise the recovery threads, keep fewer partitions per broker and smaller segments, and make sure maintenance always allows a clean controlled shutdown.

Why this question is asked

A 40-minute broker start is not just an inconvenience. While the broker recovers it serves nothing, so the rest of the cluster carries its load with fewer replicas, and a second failure in that window can take partitions offline. Interviewers ask this because the answer connects three design choices, page-cache writes, replication-based durability and per-directory recovery threads, to a real operational symptom. In this answer, we will recap how Kafka writes and flushes data, see what recovery does after an unclean shutdown, explain why it is slow here, and go through the changes that shorten it and the risk while it runs.

1A Quick Recap of How Kafka Writes to Disk

When a broker appends a batch, it writes it to the segment file through the operating system, and the bytes land in the page cache, memory that the kernel flushes to disk in the background. Kafka does not call fsync for each write by default; flush.messages and flush.ms are effectively unlimited. Durability comes from replication instead: with acks=all, an acknowledged record is in the memory of several brokers, which are unlikely to lose power at the same moment.

Every so often the broker flushes a partition and records how far it is known to be safely on disk. That offset is the recovery point, and the broker writes recovery points for all its partitions into a file called recovery-point-offset-checkpoint in each log directory. On a clean shutdown it also writes a marker file saying that everything was flushed and closed properly.

2What Recovery Does After an Unclean Shutdown

On start, the broker looks for the clean shutdown marker. After a power cut there is none, so for each partition it must assume that anything after the recovery point might be incomplete or corrupt. A segment that was being written when power failed can end in the middle of a batch, and its index may point past the real data.

So for every segment after the recovery point, the broker reads the .log file batch by batch, checks each batch's CRC and size, and stops at the first invalid batch, truncating the file there. It rebuilds the offset index and time index from the batches it read, and rebuilds producer state for idempotence. Only when every partition in every log directory is loaded does the broker finish starting and begin catching up, which is why it stays out of every ISR the whole time.

Step throughLoading one partition at startup
  1. Clean shutdown marker present?
    YesTrust the logs, load quicklyNoRecover from the recovery point
  2. Read each segment after the recovery pointbatch by batch
  3. Batch CRC and size valid?
    YesRebuild index entries, keep goingNoTruncate the segment here
  4. Rebuild producer statefrom the last snapshot plus the log
  5. Partition ready; next partition in the queue

5 steps in order. Play them, or step through them one at a time.

With one recovery thread per directory, these steps run one partition at a time on each disk.

3Why It Takes 40 Minutes Here

Three things multiply. First, the amount of unflushed data. Because Kafka relies on the page cache, the recovery point for a busy partition can lag the end of the log by a large amount, especially on a machine with lots of memory, so recovery may re-read many gigabytes per disk. Second, the number of partitions: 4,000 replicas means 4,000 separate load and recovery jobs, each with file opens, index rebuilds and producer state, even for partitions with little data.

Third, and usually decisive, the parallelism. num.recovery.threads.per.data.dir controls how many threads load partitions in each log directory. It was 1 for a long time and became 2 in Kafka 4.0 (check your version). With two disks and one thread per disk, at most two partitions recover at a time. A single thread rarely saturates a modern disk, so the broker spends 40 minutes mostly waiting on itself.

Properties
# server.properties
log.dirs=/data1/kafka,/data2/kafka
num.recovery.threads.per.data.dir=1 # 2 partitions recovering at once on this broker
# 4,000 replicas / 2 threads = 2,000 jobs each
# at ~1.2 s per job on average -> 2,000 x 1.2 s = 40 minutes

Notice the arithmetic. Even a modest average per partition becomes long when the jobs queue up behind one thread.

4What We Would Change

The quickest win is more recovery threads. Setting num.recovery.threads.per.data.dir to 8 or 16 on SSDs lets many partitions load in parallel per disk, and these threads are only used during startup and shutdown, so the cost during normal operation is nothing. We size it by watching disk utilisation during a test recovery: increase it until the disks, not the threads, are the limit.

Next we reduce the work. Fewer partition replicas per broker, by adding brokers or cleaning up unused topics, means fewer jobs. Smaller segments mean less data to re-read in the active segment, although too small causes other problems, so this is a moderate change. Some teams also set a moderate flush.ms on their busiest topics so the recovery point stays closer to the end of the log; this trades some extra disk writes for faster recovery and is worth testing rather than assuming.

Most importantly, avoid unclean shutdowns where we can. A controlled shutdown writes the clean marker and skips recovery entirely. Power cuts cannot be avoided, but maintenance can: give brokers enough time to shut down (for example, a systemd stop timeout of several minutes instead of the default 90 seconds) so that a planned restart never turns into a recovery.

5The Risk While the Broker Recovers

During those 40 minutes the cluster runs with this broker's replicas missing. Every partition it hosted has one replica fewer, so UnderReplicatedPartitions is high, and partitions where the remaining ISR is already at min.insync.replicas are one failure away from rejecting acks=all writes. The other brokers also carry its leadership and its fetch traffic.

So the team should watch under-replicated and under-min-ISR partitions until the broker has recovered and caught up, avoid other maintenance in that window, and treat a long recovery as a capacity and availability risk to fix, not as normal startup time.

6Follow-up questions

Why does Kafka not just fsync every write to avoid this?

Because an fsync per write would cut throughput dramatically and add latency to every produce request. Kafka's design assumes that replicas in different failure domains make a simultaneous loss unlikely, so it lets the kernel flush in the background and pays the price in recovery time after rare unclean shutdowns.

Does the broker lose data in recovery?

It can lose the tail that was not flushed, but with replication that tail exists on other replicas. After recovery the broker truncates to match the current leaders using leader epochs and fetches whatever it is missing, so nothing acknowledged with acks=all is lost unless every replica lost it together.

How do I know a broker is in log recovery?

The broker log shows lines about loading and recovering logs for each partition, often with a running count of logs remaining in each directory. Newer versions also expose metrics for remaining logs and segments to recover per directory and thread; check your version for their names.

7Conclusion

In this answer, we saw that Kafka writes through the page cache and relies on replication rather than per-write fsync, so after a power cut a broker must re-read and validate everything after each partition's recovery point, truncate torn writes and rebuild indexes. With 4,000 replicas, a lot of unflushed data and only one or two recovery threads per disk, that becomes a long serial queue. More recovery threads, fewer partitions per broker, moderate segment sizes and reliable controlled shutdowns shorten or avoid it, and while it runs we watch under-replicated partitions closely.

What to say in the interviewAbout 90 seconds aloud

Open with

The broker is doing log recovery. Kafka writes through the page cache and does not fsync each write; it relies on replication for durability. It periodically records a recovery point per partition, and on a clean shutdown it writes a marker.

Then, if they want more

After a power cut there is no marker, so for every partition it cannot trust anything after the recovery point: it re-reads those segments, checks each batch's CRC, truncates any torn write and rebuilds the offset and time indexes and the producer state.

With 4,000 replicas and possibly a lot of unflushed data, that is a lot of work, and the key problem is that num.recovery.threads.per.data.dir is 1 by default in older versions and 2 since 4.0, so with two disks only two or four partitions recover at a time.

I would raise that setting to something like 8 or 16 per directory on SSDs, since those threads only run during startup and shutdown. I would also reduce the work: fewer partitions per broker and not oversized segments.

And for planned maintenance I would make sure the broker always gets a clean controlled shutdown, with a long enough stop timeout, because that skips recovery entirely. While it recovers it serves nothing, so I would watch under-replicated and under-min-ISR partitions and avoid other maintenance until it is back in the ISRs.

About this answer

Last updated
13 November 2026
Versions it names
Kafka 4.0
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 "Why does this take so long, and what would you change?": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/4217a47f61/