Latency spike. P99 through the roof. Pages screaming at 3 AM. Again. We deployed aggressive ebpfbased distributed tracing across our core API cluster, convinced we were finally going to stare down those ephemeral tail latencies. We bought into the hype: unparalleled visibility, minimal overhead, kernel-native insights. Instead, our carefully tuned kernel 5.15.0-91-generic, handling 200k req/s, decided to throttle itself. The netdata dashboards were a kaleidoscope of red, CPU utilization spiked in ksoftirqd and kprobe handlers, and my kubectl logs -f api-service-7f8d6c7b9-vz9xj scroll was useless, filled with 504s. ebpfbased distributed tracing didn’t solve our P99 problem; it became the problem. This isn’t a success story. This is a post-mortem on optimism.
Table of Contents
The Illusion of Zero Overhead in ebpfbased distributed tracing
Every vendor stands on a stage and preaches zero-cost observability. They talk about bounded execution, safe verifiers, and bypass paths. It is a lie. Or at least, it is a half-truth wrapped in marketing wool. When you attach a program to a high-frequency execution path running libbpf 1.2.0, you pay a tax. Every single time.
Consider the difference between a rare event and a hot path. Tracing block device IO completion on a database write path is fine. Tracing every single sys_enter_write or sys_enter_read across fifty worker threads handling incoming TLS connections on port 8443 is a self-inflicted denial-of-service attack. The kernel does not care about your distributed trace context. It only cares about executing instructions before the next context switch.
The Trap of Hot-Path Probes
We ran bpftrace v0.19.0 to hook into system calls. The script looked harmless on a laptop staging environment:
bpftrace -e 'tracepoint:syscalls:sys_enter_read { @start[tid] = nsecs; } tracepoint:syscalls:sys_exit_read /@start[tid]/ { @lat = hist(nsecs - @start[tid]); delete(@start[tid]); }'
On a node running api.myproductioncluster.internal:8443 at 45,000 requests per second, that innocent one-liner crippled throughput. Why? Because every entry and exit forced a trip through the tracepoint infrastructure, executed our map lookups, serialized data into a BPF hash map, and triggered kernel-user synchronization primitives.
The overhead isn’t just CPU cycles. It is cache thrashing. Every time a BPF program executes, it pulls instructions and data structures into the L1/L2 cache, evicting the actual application logic your CPU cores are trying to run. When you multiply that by hundreds of thousands of operations per second, the cache miss rate climbs vertically.
Anatomy of the Slowdown: Context Switching and Stack Unwinding
Let’s look at the actual numbers. We pulled out perf to see where the CPU cycles were vanishing. The output wasn’t pretty. It showed the kernel spending an obscene amount of time inside tracing infrastructure rather than processing actual socket buffers.
Analyzing the Perf Output
We ran a standard profile using perf record -F 99 -g -p $(pgrep -n api-service) -- sleep 30. The resulting flamegraph was dominated not by JSON parsing or database queries, but by kernel-space overhead related to our tracing setup.
# Samples: 48K of event 'cpu-clock'
# Event count (approx.): 12240000000
#
# Overhead Command Shared Object Symbol
# ........ ............... .................. ......................................
#
34.21% api-service [kernel.kallsyms] [k] native_queued_spin_lock_slowpath
18.05% api-service [kernel.kallsyms] [k] bpf_prog_3a2f8c1d5e6b7a8f
12.40% api-service [kernel.kallsyms] [k] __perf_event_output
8.12% ksoftirqd/42 [kernel.kallsyms] [k] __netif_receive_skb_core
5.33% api-service [kernel.kallsyms] [k] copy_user_enhanced_fast_string
Look at that second line. The raw BPF program execution was consuming over eighteen percent of our total CPU budget. Combined with the spinlocks required to serialize map updates across multiple cores, we had essentially turned a multi-core machine into a serial bottleneck.
Stack unwinding is the silent killer here. When your tracing framework tries to capture a distributed trace context by walking the stack via frame pointers or DWARF debug info during a kprobe, it stalls the pipeline. If your application was compiled without frame pointers—a common default in many Go and Rust toolchains—the kernel has to guess its way up the stack using external unwiders. This takes thousands of CPU cycles per trace.
The Data Pipeline Bottleneck: Ring Buffers and Perf Event Buffers
Capturing the data is only half the battle. Getting it out of the kernel without dropping events requires careful tuning of the underlying transport layer. Most tools default to legacy perf_event_mmap_page ring buffers.
At high throughput, these buffers saturate instantly. When they overflow, the kernel drops events silently, corrupting your traces, or worse, locks the writer thread while waiting for userspace to drain the buffer.
Tuning the Transport Layer
We migrated from old perf buffers to modern BPF ring buffers available in newer kernels, but even then, the userspace consumer daemon needs explicit tuning. We had to adjust kernel parameters in /etc/sysctl.conf to prevent socket and buffer starvation:
# Increase maximum perf buffer memory allocations
kernel.perf_event_max_sample_rate = 100000
net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
Even with these tweaks, the overhead of copying data from kernel space to userspace remains constant. If your tracing agent is serializing every span into a JSON payload or a heavy protobuf object inside the ring buffer, you are wasting valuable CPU cycles on serialization.
When ebpfbased distributed tracing is a Trap
Not all workloads are created equal. You cannot treat a stateless microservice handling 100 req/s the same way you treat a multi-tenant payment gateway processing 250,000 req/s over gRPC.
Here is where it breaks down completely:
- High-frequency short-lived TCP connections
- Intense multithreaded applications with heavy lock contention
- Environments utilizing mixed kernel versions where BTF (BPF Type Format) metadata is incomplete
- Applications relying heavily on async runtimes that jump threads constantly
When you attach a tracing probe to an async runtime like Tokio or Go’s scheduler, the probe fires across thread boundaries unpredictably. Correlating those traces requires maintaining massive map states in kernel space. Those maps grow, experience hash collisions, and trigger lock contention on the BPF memory allocator.
Hard-Won Optimizations and Mitigation Strategies
We didn’t rip out ebpfbased distributed tracing entirely. We learned how to muzzle it. We treated it like a loaded weapon instead of a default feature.
1. Sampling at the Source
Never trace everything. If you are collecting 100% of your traces at the kernel level, you are doing it wrong. We implemented strict head-based and tail-based probabilistic sampling directly inside the BPF bytecode using pseudo-random number generators seeded by timestamps.
// Example snippet of BPF-side probabilistic sampling
if ((bpf_get_prand_u32() % 100) > 5) {
// Drop 95% of events immediately before map insertion
return 0;
}
2. Bypassing Uprobes for Application Metrics
Stop putting uprobes on every function entry in your user-space binary. The overhead of trapping into the kernel from user space, executing a BPF program, and returning is orders of magnitude higher than a simple in-process counter increment. If you need application-level latency metrics, use in-memory atomic counters or OpenTelemetry SDKs with asynchronous batching rather than forcing the kernel to inspect user-space function arguments.
3. Utilizing bpftool for Inspection
Regularly audit what is actually loaded into your kernel. Orphaned maps and bloated programs linger after agent crashes. Use bpftool to inspect resource consumption:
bpftool prog show
bpftool map show
If you see program IDs consuming excessive CPU cycles or map sizes pushing against their maximum limits, detach them immediately. Do not trust that your monitoring agent is cleaning up after itself during a rolling restart.
Conclusion: Pragmatism Over Dogma
Observability tools are designed to tell you why your system is failing, not to become the reason it fails. ebpfbased distributed tracing is an extraordinary technological achievement. It gives us unprecedented visibility into kernel subsystems without modifying application code. But treating it as a silver bullet is operational malpractice.
Understand the cost of every probe you attach. Measure the overhead before pushing to production. If your P99 latency starts climbing immediately after a tracing deployment, do not look at your database, do not look at your network gear—look at your kernel instruction cycles. Sometimes, the best way to fix your latency is to turn off the telemetry and let the machine just do its job.
Related Articles
Explore more insights and best practices:
- Aws Ai Guide Build And Scale Smarter Applications
- Top Artificial Intelligence Best Practices For Success 3
- Javascript Best Practices Write Cleaner Faster Code