15. KCP / SCTP / MPTCP
Fast ARQ over UDP, message-oriented multi-streaming, multi-homing, and transparent TCP path aggregation.
1. 15.1 - KCP: ARQ over UDP
KCP is a user-space reliable transport that runs over UDP. It keeps TCP's useful reliability ideas, such as sliding windows, selective acknowledgments, timestamps, and packet retransmission, but removes TCP's kernel congestion-control policy from the protocol core. That is why it is common in games, tunnels, and real-time applications where latency under loss matters more than strict fairness.
The important interview point is that KCP retransmits segments, not byte-stream ranges. In nodelay mode the RTO floor can be about 30 ms, and fast retransmit can fire after two later ACKs instead of waiting for TCP's usual three duplicate ACK pattern or a conservative timeout.
Minimal C Demo - KCP vs TCP Under Loss
2. 15.2 - KCP Tuning Parameters
KCP tuning is explicit. A low-latency profile usually calls ikcp_nodelay(kcp, 1, 10, 2, 1): enable fast mode, flush every 10 ms, retransmit after two skip acknowledgments, and disable congestion window limiting. That last flag, nc=1, is the dangerous one.
| Parameter | Default | Effect | Low-latency value |
|---|---|---|---|
nodelay | 0 | Lower RTO floor and faster ACK behavior. | 1 |
interval | 100 ms | Flush/update cadence. | 10 ms |
resend | 0 | Fast retransmit threshold from later ACKs. | 2 |
nc | 0 | Disable congestion-control window. | 1 only with app shaping |
snd_wnd / rcv_wnd | 32 | Segment send and receive windows. | Workload-specific |
nocwndcan dominate a bottleneck and starve TCP flows unless the application rate-limits.- FEC reduces tail latency when losses are isolated, but it spends extra bandwidth on every protected group.
- KCP does not encrypt, authenticate, or perform NAT traversal by itself; products such as kcptun compose those pieces around it.
3. 15.3 - SCTP: Multi-Streaming and Multi-Homing
SCTP was built for signaling systems that need message boundaries, independent streams, and path failover. A single association can contain many streams. Loss in one stream blocks that stream's ordered delivery, but it does not force unrelated streams to wait behind the same byte-stream gap.
SCTP's four-way startup includes a signed state cookie. The server sends the cookie in INIT-ACK and commits full association state only when the client returns it in COOKIE-ECHO. That makes the cookie mechanism a built-in answer to SYN-flood style state exhaustion.
WebRTC DataChannel uses SCTP semantics, but not raw SCTP packets on the public internet. It runs SCTP over DTLS over UDP so it can reuse ICE, STUN, TURN, encryption, and NAT traversal behavior that browsers already need for media.
Minimal C Demo - SCTP Stream and Path Failover
4. 15.4 - MPTCP: Multipath TCP
MPTCP keeps the application API as a normal TCP socket while creating multiple TCP subflows under one connection. Each subflow has its own four-tuple and TCP sequence space, while MPTCP adds a data-level sequence number so the receiver can reassemble the original byte stream.
The scheduling problem is harder than it first looks. Sending on every available path can increase throughput, but a slow path can create data-sequence gaps that hold back delivery. Practical schedulers consider RTT, bandwidth, loss, backup-path policy, and whether redundant transmission is worth the cost.
Minimal C Demo - MPTCP Scheduler Split
5. Comparison Cheat Sheet
| Protocol | Best fit | Main mechanism | Main risk |
|---|---|---|---|
| KCP | Games, tunnels, latency-sensitive apps. | User-space ARQ over UDP with aggressive retransmit knobs. | Unfairness and extra bandwidth when nocwnd or FEC is used carelessly. |
| SCTP | Telecom signaling, WebRTC DataChannel semantics. | Message chunks, multi-streaming, multi-homing, cookie startup. | Raw SCTP is often blocked by NATs and firewalls. |
| MPTCP | Mobile WiFi+cellular bonding, transparent path aggregation. | Multiple TCP subflows with global data sequence numbers. | Scheduler and reordering behavior can hurt latency. |
6. Interview Prep
| Question | Answer checkpoint |
|---|---|
| How does KCP achieve lower latency than TCP under packet loss? | It uses packet-level ARQ over UDP, low RTO floors, configurable fast retransmit, no Nagle, and optional FEC. |
What is the trade-off of KCP nocwnd mode? | It can improve throughput and latency for that flow, but it is not TCP-friendly and must be shaped externally. |
| How does SCTP's handshake resist SYN floods? | The server returns a signed state cookie in INIT-ACK and allocates full state only after COOKIE-ECHO validates it. |
| What problem does the MPTCP scheduler solve? | It decides which subflow carries each data-sequence range while balancing throughput, RTT, loss, and reordering delay. |
| Why does WebRTC use SCTP over DTLS over UDP? | It gets SCTP stream semantics while using DTLS security and UDP/ICE traversal through NATs and firewalls. |