Home / Data Platform / Streaming & CDC

Data PlatformRunbookAdvanced

Sev1: Kafka brokers crashing with 'Too many open files' or 'Map failed' and partitions going offline

Brokers die with IOException Too many open files or OutOfMemoryError Map failed, then fail again during log recovery. Raise nofile and vm.max_map_count, restart in order, then cut segment and partition counts.

Deepak Bhardwaj6 min read

Applies toApache Kafka 3.x and 4.x (ZooKeeper and KRaft) on Linux with systemd; self-managed on VMs, EC2, Azure VMs or OCI Compute

Symptoms

  • A broker's server.log shows java.io.IOException: Too many open files, or java.io.IOException: Map failed / java.lang.OutOfMemoryError: Map failed, followed by the broker shutting down.
  • Partitions led by that broker go offline or under-replicated. Producers with acks=all time out, and consumers stall on those partitions.
  • The broker restarts, runs log recovery, and crashes again at the same point. Sometimes a second broker fails while it takes on the extra leadership and replica traffic.
  • The JVM heap is fine. These errors come from OS limits, not heap size.

Kafka holds an open file handle for every log segment and its index files, plus one socket per client connection. It also memory-maps the offset and time index of every segment. Each mapping counts against vm.max_map_count, which defaults to 65530 on most distributions. Many partitions, small segments or a client connection leak can push a broker past either limit.

1. Confirm scope and impact

Find which limit was hit, per broker:

grep -E "Too many open files|Map failed" /var/log/kafka/server.log | tail -5

Check offline and under-replicated partitions from a healthy broker:

kafka-topics.sh --bootstrap-server $BS --describe --unavailable-partitions
kafka-topics.sh --bootstrap-server $BS --describe --under-replicated-partitions

If more than one broker is down, or offline partitions include __consumer_offsets or __transaction_state, consumer groups and transactional producers across the cluster are affected. Treat that as cluster-wide.

2. Measure against the limits

On a running broker (or right after a restart, before it crashes again):

PID=$(pgrep -f kafka.Kafka)

# Effective open-file limit for the process (not your shell's ulimit)
grep "Max open files" /proc/$PID/limits

# Current open files and memory maps
ls /proc/$PID/fd | wc -l
wc -l < /proc/$PID/maps
sysctl vm.max_map_count

# Log segments on disk: a rough guide to what Kafka will open and map
find /var/lib/kafka/data -name "*.log" | wc -l
Symptom Limit hit Fix
Too many open files, fd count near Max open files nofile for the Kafka process Raise LimitNOFILE in the systemd unit
Map failed, maps count near vm.max_map_count Kernel memory-map limit Raise vm.max_map_count
fd count climbs steadily while segment count stays flat Socket leak from clients Find the clients (step 5), set connection quotas
Segment count very high Too many partitions or tiny segment.bytes / segment.ms Raise limits now, reduce segments after the incident

Tip

Setting ulimit -n in a shell or in /etc/security/limits.conf has no effect on a broker started by systemd. Always read the value from /proc/<pid>/limits.

3. The fix: raise the limits, then restart

Raise the memory-map limit. It applies immediately, without a reboot:

sudo sysctl -w vm.max_map_count=262144
echo "vm.max_map_count=262144" | sudo tee /etc/sysctl.d/90-kafka.conf

Raise the file-descriptor limit with a systemd override:

sudo systemctl edit kafka
[Service]
LimitNOFILE=200000
sudo systemctl daemon-reload
sudo systemctl restart kafka
grep "Max open files" /proc/$(pgrep -f kafka.Kafka)/limits

Pick values with headroom over your measured peak (at least 2x the segment count for maps, plus your expected connection count for files). The numbers above are common starting points, not vendor limits. Your unit name may differ (kafka, kafka-server, confluent-server).

Order of restarts

  1. Apply the limits on every broker first, including healthy ones. They are close to the same limit.
  2. Start the crashed brokers one at a time. Wait until each one finishes log recovery and rejoins the ISR before you start the next.
  3. Watch recovery progress:
tail -f /var/log/kafka/server.log | grep -Ei "recover|loaded|started"

After an unclean shutdown, recovery of a broker with many partitions can take a long time. num.recovery.threads.per.data.dir (default 1) controls how many threads do it. Raising it to the number of cores per data directory speeds up the next restart. It is read at startup.

Caution

Do not enable unclean.leader.election.enable to bring partitions back faster while brokers are only restarting. An out-of-sync replica becoming leader drops every record it never received. Wait for the real leaders to recover.

4. Verify recovery

kafka-topics.sh --bootstrap-server $BS --describe --unavailable-partitions
kafka-topics.sh --bootstrap-server $BS --describe --under-replicated-partitions

Both should return nothing. Then rebalance leadership back to preferred replicas:

kafka-leader-election.sh --bootstrap-server $BS --election-type preferred --all-topic-partitions

Check that producer error rates and consumer lag are back to normal. Then re-check the fd and map counts against the new limits after an hour of normal traffic.

5. Find what drove the count up

Too many segments. Find topics with small segment settings:

kafka-configs.sh --bootstrap-server $BS --entity-type topics --describe --all \
  | grep -E "Dynamic configs|^ +segment\.(ms|bytes)=.*DYNAMIC_TOPIC_CONFIG"

The output format differs a little between versions. What you are looking for is topic-level overrides of segment.ms or segment.bytes.

A topic with segment.ms of a few minutes and long retention builds up thousands of segments per partition. Reset it to the broker default unless there is a reason, for example compaction latency.

Too many partitions. Count partitions per broker. Topics created with hundreds of partitions "for later" cost file handles and maps on every replica.

Connection leak. Count established client connections on the broker port:

ss -tn state established '( sport = :9092 )' | awk 'NR>1 {split($4,a,":"); print a[1]}' \
  | sort | uniq -c | sort -rn | head

A single host with thousands of connections usually means a client creating a producer per message or per request. Cap it with broker settings such as max.connections.per.ip and max.connections, and fix the client.

Tell users

Report which partitions were offline and for how long. Producers using acks=all with retries usually recover without loss. Producers with acks=0 or acks=1, or with short delivery.timeout.ms, may have dropped records during the outage. Ask those teams to check their error logs for the incident window.

Prevent it next time

  • Bake LimitNOFILE and vm.max_map_count into the broker image or configuration management, and check them in a startup preflight.
  • Alert on open fds and memory maps as a percentage of the limit (node exporter, or a cron script reading /proc), at 70%.
  • Review partition counts and segment.ms / segment.bytes in topic-creation requests.
  • Set max.connections.per.ip to catch leaking clients before they take a broker down.
  • See offline and under-replicated partitions for the general recovery runbook.