Scrivio Interview library
InternalsIntermediate6 min read · updated 13 November 2026

After enabling TLS between clients and brokers, broker CPU rose from 25 to 60 percent and the maximum fetch throughput per broker dropped by about a third.

Explain why, and what you can do about it.

Without TLS, the broker sends log data from the page cache to the socket with sendfile, and the bytes never enter the JVM. With TLS, every byte must be read into the JVM, encrypted and written out, so zero-copy is lost and the CPU pays for copies and encryption. We reduce the cost with a current JDK using hardware-accelerated AES-GCM and TLS 1.3, a check of the negotiated cipher suites, large batches, and capacity planning that includes TLS.

Why this question is asked

Turning on encryption in transit is one of the most common changes a Kafka platform team makes, usually because of a security audit, and the CPU jump surprises people who expected a few percent. Interviewers ask this to see whether we know how the broker moves data internally and can reason from mechanism to numbers instead of guessing. In this answer, we will recap how a plaintext fetch works, see exactly what TLS changes on that path, then go through what we can tune, what we must simply plan for, and one older cause of lost zero-copy that Kafka 4 has removed.

1A Quick Recap: How a Plaintext Fetch Avoids the JVM

A fetch is the request consumers and follower replicas send to read records. On a plaintext listener, the broker finds the segment file and byte range that answer the request and calls FileChannel.transferTo(). On Linux this becomes sendfile, a system call that asks the kernel to move bytes from the page cache, the kernel's memory copy of recently used file blocks, directly into the socket.

So for the record data itself, the broker allocates nothing on the heap, copies nothing in user space and does almost no CPU work. Most of the CPU in a plaintext broker goes to request handling, the network threads and writes, not to moving fetched bytes. That is why a broker can serve several hundred MB per second of fetches at modest CPU.

2What TLS Changes on the Fetch Path

TLS encrypts data inside the process that owns the connection, because the keys live in the JVM's SSLEngine. The kernel cannot encrypt for us with sendfile (Linux kernel TLS exists, but the JVM's TLS stack does not use it, so check before counting on it). So for every fetch the broker must read the bytes from the page cache into a buffer in the JVM, pass them through SSLEngine to encrypt them, and write the encrypted bytes to the socket.

The same fetch with and without TLS
StepPlaintextTLS
Read log dataStays in page cacheCopied into a JVM buffer
EncryptNot neededAES-GCM in SSLEngine, per record batch
Write to socketsendfile, kernel onlyEncrypted copy written from the JVM
CPU per MB servedVery lowSeveral times higher
Heap and GCNo buffers for record dataBuffers churn, more GC work
Encryption is not the only cost: the extra copies and buffer churn add up too.

Each of these steps costs CPU: two extra memory copies, the encryption itself and the work of the TLS record layer. Followers fetching from leaders over a TLS inter-broker listener pay the same price, so replication traffic adds to it. The numbers in our question, CPU from 25 to 60 percent and a third less fetch throughput, are a typical result. The broker is now CPU-bound on a path that used to be almost free.

3Fix 1: Make the Encryption Itself Cheap

Modern CPUs have AES instructions (AES-NI on x86, similar ones on ARM), and current JDKs use them for AES-GCM. A broker on an old JDK, or one that negotiated a slow cipher suite, can spend several times more CPU per byte than necessary. So we check what was actually negotiated rather than assuming.

Properties
# Broker and client side: prefer TLS 1.3 and fast suites
ssl.enabled.protocols=TLSv1.3,TLSv1.2
ssl.protocol=TLSv1.3
# For TLS 1.2 peers, list only AES-GCM suites, for example
ssl.cipher.suites=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
# Check what a listener negotiates
openssl s_client -connect broker1:9093 -tls1_3 </dev/null 2>/dev/null | grep -E 'Protocol|Cipher'
# Protocol : TLSv1.3
# Cipher : TLS_AES_256_GCM_SHA384

We also run the broker on a current LTS JDK (Kafka 4 brokers need Java 17 or later, and Java 21 or 25 is a good choice), because TLS performance has improved steadily across JDK releases. A quick profile with async-profiler or a JFR recording shows whether time goes to the AES code or to copying and buffer management, which tells us where to look next.

4Fix 2: Keep Requests Big and Plan for the Cost

TLS has a per-record-layer and per-request overhead on top of the per-byte cost. Small batches and tiny fetches make it worse, so we keep producer batches large (linger.ms and batch.size), keep compression on (fewer bytes to encrypt), and let consumers fetch reasonably large chunks with fetch.min.bytes and fetch.max.wait.ms.

After tuning, some cost remains, and it is the price of encryption. We put it in the capacity plan: measure throughput per broker at a target CPU of, say, 60 percent with TLS on, and size the cluster from that number, not from plaintext benchmarks. That may mean more brokers or instance types with more cores. If some traffic runs inside a trusted network segment, a few teams keep plaintext for inter-broker replication, but that is a security decision for the security team, not a tuning trick.

5An Older Cause That Kafka 4 Removed

Before Kafka 4, there was a second way to lose zero-copy. Very old consumers understood only the old message formats (v0 and v1). When such a client fetched data stored in the current format (v2), the broker had to down-convert it: read it into the JVM, rewrite every batch in the old format and send that. Down-conversion was CPU-heavy and memory-heavy and showed up even on plaintext listeners.

Kafka 4.0 removed the old message formats and the old protocol versions that needed them, so on current brokers this cause is gone. If we are investigating an older cluster, the broker metrics for fetch down-conversions are still worth checking before blaming TLS.

6Follow-up questions

Does TLS also slow down producers?

Yes, but less dramatically. Produce requests are read from the socket into the JVM anyway, because the broker has to validate the batch and append it to the log, so there was never zero-copy on the write path. TLS adds decryption on top of that work. The largest change is on fetches, where TLS turns a kernel-only path into a JVM path.

How would you confirm that TLS is the cause rather than something else that changed?

Compare CPU per MB fetched before and after on the same broker, and look at a CPU profile: SSLEngine, AES-GCM and buffer copying should dominate the new time. A cleaner test is a load test with two listeners on a staging broker, one plaintext and one TLS, sending the same traffic. If the difference matches what we saw in production, we know the cause and can measure each tuning change against it.

Is mutual TLS more expensive than one-way TLS?

The handshake costs more, because both sides present and verify certificates, but handshakes happen once per connection. The steady-state cost of encrypting data is the same. mTLS only becomes a problem when clients reconnect constantly, which is worth fixing for other reasons too.

7Conclusion

In this answer, we saw that the CPU jump after enabling TLS comes from losing zero-copy: plaintext fetches go from the page cache to the socket with sendfile, while TLS fetches must be copied into the JVM, encrypted and written back out. We make the encryption cheap with a current JDK, TLS 1.3 and AES-GCM, keep batches and fetches large so per-request costs stay small, and plan capacity with TLS included. Down-conversion for very old clients used to defeat zero-copy too, but Kafka 4 removed the old formats that needed it.

What to say in the interviewAbout 90 seconds aloud

Open with

The key is how a broker serves fetches. On a plaintext listener it uses FileChannel.transferTo, which is sendfile on Linux, so record bytes go from the page cache straight to the socket and never enter the JVM.

Then, if they want more

That is why fetches are cheap. TLS breaks that, because encryption happens in the JVM's SSLEngine. Now every fetched byte is read into a JVM buffer, encrypted and written out again, so we pay for extra copies, for the encryption and for buffer churn in GC. Replication between brokers pays it too if the inter-broker listener uses TLS.

CPU going from 25 to 60 percent and fetch throughput dropping by a third is a normal result of that. What can I do? First, make encryption cheap: run a current JDK, which uses the CPU's AES instructions, prefer TLS 1.3, and check with openssl s_client or a profile that we really negotiated an AES-GCM suite and not something slow.

Second, keep requests large: good producer batching, compression so there are fewer bytes to encrypt, and consumers fetching bigger chunks. Third, accept the remaining cost and plan for it, measuring throughput per broker with TLS on and adding brokers or cores.

I would also mention that very old clients used to force down-conversion, which killed zero-copy even without TLS, but Kafka 4 removed those old formats, so that is not a factor on current versions.

About this answer

Last updated
13 November 2026
Versions it names
Java 17, Java 21, 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 "Explain why, and what you can do about it.": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/c300a1e68a/