Self-Hosted eBPF-Based TCP Congestion Control and Bufferbloat Elimination with BBRv3 and CAKE
Quick Summary (TL;DR)
- Bufferbloat is excess packet buffering that spikes latency under load—killing interactive self-hosted services.
- eBPF (via
bpftraceorbcc) lets you measure queueing latency in the kernel with microsecond precision.- BBRv3 (Linux 6.1+) is a model-based congestion control algorithm that avoids filling buffers and plays well with Active Queue Management (AQM).
- CAKE is a smart qdisc that actively manages the queue, drops excess in a fair way, and keeps latency flat.
- Deploy BBRv3 on endpoints (servers/clients) and CAKE on the bottleneck interface (usually the WAN gateway).
- Expected result: single-digit millisecond latency under saturating load, stable jitter, and high throughput.
Introduction: Why Bufferbloat Matters for Self-Hosted Services
You’ve tuned your reverse proxy, your database, and your TLS ciphers. Yet your self-hosted Nextcloud feels sluggish when a large backup is running. Your WireGuard tunnel stutters during file syncs. Your self-hosted VoIP drops packets during uploads. This is not a CPU problem—it’s a network queueing problem called bufferbloat.
Self-hosted operators are uniquely exposed: you control every hop on your side of the connection, so you can actually fix what most users cannot. Two modern kernel tools make this practical: BBRv3 (the third-generation Bottleneck Bandwidth and Round-trip propagation time congestion control algorithm) and CAKE (Common Applications Kept Enhanced queue discipline). And to know whether you’ve fixed it, you need eBPF to see queueing latency inside the kernel.
Understanding Bufferbloat and Its Impact on Latency
The Anatomy of Bufferbloat
Every network device has a buffer—a queue of packets waiting to be transmitted. Small buffers prevent drops during micro-bursts. But oversized buffers, combined with a congestion control algorithm that only reacts to loss, create a pathology: senders keep filling the buffer until it’s full, and every packet then waits in line.
If a link has 200 ms of standing queue, your interactive SSH session and your bulk download are treated identically—both wait behind a wall of bytes. Latency under load can jump from 10 ms to hundreds of milliseconds, while throughput looks “fine.”
The classic symptoms:
- Idle latency: 15 ms
- Latency during upload/download: 300–800 ms
- Jitter: wildly variable, breaking VoIP and gaming
- Throughput: still near line rate, so tools like
iperf3look healthy
How BBRv3 and CAKE Address Bufferbloat
These two tools attack the problem from opposite ends:
- BBRv3 (sender-side): Models the network path to estimate bottleneck bandwidth and minimum RTT. Instead of filling buffers until loss, it paces packets to match the estimated bottleneck rate. Result: senders stop creating standing queues.
- CAKE (queue-side): Actively manages the queue at the bottleneck. It uses an AQM (CoDel or BLUE), FQ-CoDel-style flow isolation (so one elephant flow can’t starve others), and per-host fairness. Result: even when a queue forms, latency stays bounded.
Together, BBRv3 avoids building queues and CAKE bounds the ones that do form.
flowchart LR
A[Sender<br/>BBRv3] -->|paced packets| B[Gateway<br/>CAKE qdisc]
B -->|shaped egress| C[Bottleneck Link]
C --> D[Internet]
D --> E[Remote Peer]
B -.AQM drops.-> B
style A fill:#e3f2fd
style B fill:#fff3e0
style C fill:#fce4ec
Diagnosing Bufferbloat with eBPF Latency Histograms
Prerequisites for eBPF Monitoring
You need a modern kernel (5.10+ recommended), bpftrace, and root. On Debian/Ubuntu:
sudo apt install -y bpftrace linux-tools-common linux-tools-$(uname -r)
Verify eBPF works:
sudo bpftrace -e 'kprobe:tcp_rcv_established { @[comm] = count(); }'
Using bpftrace to Measure Queueing Latency
The most direct signal is socket send queue latency—how long packets wait before hitting the NIC. Trace the tcp_sendmsg → dev_queue_xmit gap:
sudo bpftrace -e '
kprobe:tcp_sendmsg { @start[tid] = nsecs; }
kprobe:dev_queue_xmit /@start[tid]/ {
$lat = (nsecs - @start[tid]) / 1000;
@us = hist($lat);
delete(@start[tid]);
}'
For a more granular view of kernel queueing, trace qdisc_enqueue to qdisc_dequeue:
sudo bpftrace -e '
kprobe:qdisc_enqueue { @qt[tid] = nsecs; }
kprobe:qdisc_dequeue /@qt[tid]/ {
@qdelay_us = hist((nsecs - @qt[tid]) / 1000);
delete(@qt[tid]);
}'
Interpreting Latency Histograms
A healthy histogram is bimodal and tight: most packets at <100 µs, a small tail at a few ms. Bufferbloat shows up as a fat tail stretching into tens or hundreds of milliseconds, growing exactly when you push traffic.
graph TD
A[Run baseline histogram<br/>idle link] --> B[Push load with iperf3]
B --> C[Capture histogram under load]
C --> D{Tail beyond 10ms?}
D -->|Yes| E[Bufferbloat confirmed]
D -->|No| F[Link is healthy]
E --> G[Deploy CAKE on WAN]
G --> H[Enable BBRv3 on endpoints]
H --> I[Re-measure]
Deploying BBRv3 Congestion Control
Checking Kernel Support for BBRv3
uname -r # 6.1+ recommended
sysctl net.ipv4.tcp_available_congestion_control
modinfo tcp_bbr | grep version # shows '3' for BBRv3
If tcp_bbr reports version: 3, you have BBRv3. If it reports 1 or 2, upgrade the kernel (Ubuntu 24.04+, Fedora 39+, Debian 13 all ship BBRv3-capable kernels).
Enabling BBRv3 System-wide
Create /etc/sysctl.d/99-bbrv3.conf:
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
net.ipv4.tcp_notsent_lowat = 16384
net.ipv4.tcp_mtu_probing = 1
net.ipv4.tcp_slow_start_after_idle = 0
Apply:
sudo sysctl --system
Note: BBRv3 still registers under the name
bbr; the version is internal.fqas the default qdisc gives BBR the pacing it needs. On the gateway you’ll override this with CAKE on the WAN interface.
Verifying BBRv3 Activation
sysctl net.ipv4.tcp_congestion_control
# net.ipv4.tcp_congestion_control = bbr
ss -tin state established '( dport = :443 )' | grep -o 'bbr' | head
Every new TCP connection will now negotiate BBRv3.
Tuning CAKE qdisc for Low Latency
Installing and Configuring CAKE
CAKE ships as the sch_cake module in mainline kernels. Verify:
sudo modprobe sch_cake
tc qdisc show dev eth0
Key CAKE Parameters for Bufferbloat Control
| Parameter | Purpose | Recommended |
|---|---|---|
bandwidth | Shape to 85–95% of link rate | 90mbit for a 100 Mbit link |
rtt | Expected RTT (improves AQM) | 50ms for typical broadband |
diffserv4 | Prioritize DSCP classes | Yes |
nat | Handle NATed flows fairly | Yes if gateway does NAT |
wash | Clear DSCP from untrusted ingress | Yes on WAN |
dual-srchost / dual-dsthost | Per-host fairness | dual-srchost on WAN |
ack-filter | Compress ACKs (saves upstream) | Yes on asymmetric links |
Example CAKE Configuration for a Gateway
Assuming eth0 is the WAN and the link is 100/20 Mbit:
# Egress (upload) shaping
tc qdisc replace dev eth0 root cake \
bandwidth 19mbit \
rtt 50ms \
diffserv4 \
nat \
wash \
dual-srchost \
ack-filter
# Ingress (download) shaping via IFB
modprobe ifb
ip link set ifb0 up
tc qdisc add dev eth0 handle ffff: ingress
tc filter add dev eth0 parent ffff: \
protocol all prio 2 u32 match u32 0 0 flowid 1:1 \
action mirred egress redirect dev ifb0
tc qdisc replace dev ifb0 root cake \
bandwidth 90mbit \
rtt 50ms \
diffserv4 \
nat \
wash \
dual-dsthost
Persist this with a systemd unit or if-up.d script so it survives reboots.
sequenceDiagram
participant Client
participant GW as Gateway (CAKE)
participant Net as Internet
Client->>GW: Bulk upload (BBRv3 paced)
GW->>GW: Classify (diffserv4) + shape
GW->>GW: AQM drop if queue > target
GW->>Net: Egress at 19mbit
Note over GW: Standing queue bounded to ~5ms
Comparison: BBRv3 vs. Other Congestion Control Algorithms
| Algorithm | Loss-based? | Bufferbloat behavior | Fairness | Best use |
|---|---|---|---|---|
| CUBIC (default) | Yes | Fills buffers → high latency | Good | Legacy compatibility |
| Reno | Yes | Worse than CUBIC | Good | Ancient kernels |
| BBRv1 | No | Low latency, aggressive vs loss-based | Poor vs CUBIC | Modern links, mixed traffic |
| BBRv2 | Hybrid (ECN+loss) | Better fairness, slightly higher latency | Good | Mixed deployments |
| BBRv3 | No (model-based) | Lowest latency, best fairness, ECN-aware | Excellent | Recommended |
| Vegas | Delay-based | Very low latency | Poor on shared links | Controlled environments |
Verdict: BBRv3 is the best default for self-hosted deployments on kernels 6.1+.
Comparison: CAKE vs. Other Queue Disciplines
| qdisc | AQM | Flow fairness | Per-host fairness | Overhead | Notes |
|---|---|---|---|---|---|
| pfifo_fast | None | None | None | Minimal | Default; bufferbloat-prone |
| fq_codel | CoDel | Yes | No | Low | Great general default |
| fq | None | Yes | No | Low | Pairs with BBR pacing |
| CAKE | CoDel + BLUE | Yes | Yes | Moderate | Best for gateways |
| HTB + fq_codel | CoDel | Yes | Manual | Higher | Complex shaping |
| TBF | None | No | No | Low | Simple rate limit only |
Verdict: CAKE is the strongest choice for a self-hosted gateway because it combines shaping, AQM, and per-host fairness in one qdisc.
Validating the Results: Post-Tuning Latency Measurement
Re-run your eBPF histogram under load. Then confirm end-to-end with flent:
sudo apt install flent
flent rrul -H your-server.example.com -t "after-cake-bbrv3"
Compare before/after charts. Success criteria:
- Idle latency: unchanged
- Loaded latency: within 2× idle (e.g., 15 ms → 25 ms)
- Jitter: <5 ms
- Throughput: ≥90% of link rate
If loaded latency is still high, check:
- CAKE is on the actual bottleneck interface (not a LAN bridge).
bandwidthis not set above real link rate.- BBRv3 is active on both endpoints (
ss -tinshowsbbr).
flowchart TD
A[Latency still high?] --> B{CAKE on WAN?}
B -->|No| C[Move CAKE to WAN interface]
B -->|Yes| D{bandwidth <= 95% link?}
D -->|No| E[Lower bandwidth value]
D -->|Yes| F{BBRv3 on both ends?}
F -->|No| G[Enable sysctl on peer]
F -->|Yes| H[Investigate ISP-side buffering]
Conclusion: Achieving Low-Latency Self-Hosted Networking
Bufferbloat isn’t a mystery—it’s a measurable, fixable engineering problem. By combining eBPF histograms for diagnosis, BBRv3 on your endpoints to avoid building queues, and CAKE on your gateway to bound the queues that do form, you can turn a jittery, sluggish self-hosted network into a consistently low-latency one.
Start small: measure baseline latency under load, enable BBRv3, deploy CAKE on your WAN, and re-measure. The difference is often dramatic—and it costs nothing but a few sysctl lines and one tc command.
FAQ
What is bufferbloat and why does it affect my self-hosted services? Bufferbloat is excessive packet buffering in network equipment, causing high latency and jitter under load. For self-hosted services, it leads to sluggish response times, poor VoIP quality, and lag in interactive applications.
Do I need a custom kernel to use BBRv3?
BBRv3 is included in Linux kernel 6.1 and later. Most modern distributions (e.g., Ubuntu 22.04+, Fedora 38+) ship with a kernel that supports BBRv3, so a custom kernel is usually not required. Check with modinfo tcp_bbr | grep version.
Can I use CAKE without BBRv3? Yes, CAKE works with any congestion control algorithm. However, combining CAKE with BBRv3 often yields the best latency and throughput, as BBRv3 is designed to operate well with AQM.
How do I know if CAKE is working correctly?
Use tc -s qdisc show to check CAKE statistics, and measure latency under load with tools like flent or irtt. You should see reduced latency and minimal dropped packets. eBPF histograms confirm the tail latency has collapsed.
What is the difference between eBPF and traditional monitoring for bufferbloat?
eBPF allows kernel-level, low-overhead tracing of queueing latency with high precision. Traditional tools like ping only measure end-to-end latency and cannot pinpoint bufferbloat within the kernel—eBPF shows you where the time is spent.
Can I apply CAKE on a per-interface basis?
Yes, CAKE can be attached to specific interfaces (e.g., tc qdisc add dev eth0 root cake bandwidth 1gbit). For gateways, apply it to the WAN interface to control egress traffic, and use an IFB device for ingress shaping.
What bandwidth should I set for CAKE?
Set CAKE’s bandwidth to about 85–95% of your actual link capacity to account for overhead and prevent queue buildup. For example, for a 100 Mbps link, use 90mbit.