Why KCP Beats TCP and UDP for Real‑Time Apps: Low Latency, High Throughput
This article explains the limitations of TCP and UDP for real‑time applications, introduces the KCP protocol as a UDP‑based solution that combines reliability with low latency, outlines its features, working principles, code usage, and real‑world deployment examples in gaming, video conferencing, live streaming, and IoT.
Overview
In modern internet applications such as online games, video conferencing, and live streaming, real‑time performance and smoothness are critical. Traditional TCP provides reliable transmission but its congestion control and retransmission mechanisms cause high latency, while UDP is fast but lacks reliability, leading to packet loss and reordering.
Why Introduce KCP
Before presenting KCP, we review TCP and UDP characteristics: TCP guarantees reliability without guaranteeing transmission efficiency; UDP maximizes transmission speed but cannot ensure reliability.
How can we achieve both reliability and high transmission efficiency?
KCP Introduction
The KCP protocol was created to fill the gaps of TCP and UDP, offering lower latency and higher throughput while maintaining reliability.
KCP Protocol Overview
KCP (KCP Protocol) is a UDP‑based reliable transmission protocol open‑sourced by skywind3000 in 2014. It borrows TCP’s reliable mechanisms such as ACK, timeout retransmission, and flow control, but streamlines and optimizes them for real‑time scenarios.
Key Features of KCP
Low latency: aggressive retransmission reduces waiting time.
High throughput: dynamically adjusts sending rate according to network conditions.
Reliability: ensures ordered delivery and supports selective retransmission.
Flexibility: rich configuration options allow tuning of delay, throughput, and reliability trade‑offs.
How KCP Works
KCP implements TCP‑like reliability on top of UDP, including:
ACK: receiver sends acknowledgment packets after receiving data.
Timeout retransmission: sender retransmits if ACK is not received within a set time.
Flow control: sliding‑window mechanism prevents buffer overflow.
Congestion control: simple algorithm adjusts sending rate based on network status.
Application Scenarios
Mobile games (e.g., Honor of Kings) – reduces latency and improves player experience.
Video conferencing – minimizes video stutter and delay.
Live streaming – lowers stream latency and enhances interactivity.
IoT – improves efficiency and reliability of device communication.
Pros and Cons
Advantages :
Low latency
High throughput
High reliability
Configurable flexibility
Disadvantages :
Implementation complexity higher than UDP
Additional bandwidth overhead
Sensitivity to network jitter
How to Use KCP
Repository: https://github.com/xtaci/kcptun
// Initialize KCP object; conv is a session identifier, user is a callback pointer
ikcpcb *kcp = ikcp_create(conv, user); // Define the lower‑level output function that KCP calls to send data
int udp_output(const char *buf, int len, ikcpcb *kcp, void *user) {
// ...
}
// Set the output callback
kcp->output = udp_output; // Periodically call ikcp_update to advance KCP state (e.g., every 10 ms)
ikcp_update(kcp, millisec); // When a UDP packet is received, feed it to KCP
ikcp_input(kcp, received_udp_packet, received_udp_size);Summary
For mobile scenarios where server latency cannot stay under 2 ms and RTT cannot be consistently 3 ms, introducing KCP can replace TCP to achieve 1‑RTT message exchange, shortening app response time. Several commercial products already use KCP for optimization, including:
Genshin Impact – miHoYo uses KCP to reduce game message latency.
SpatialOS – large‑scale multiplayer engine uses KCP for data acceleration.
Westward Studios – employs KCP for game data acceleration.
NetEase CC – uses KCP to speed up video streaming.
NetEase BOBO – KCP accelerates live‑streamer broadcasting.
NetEase UU – UU accelerator uses KCP/KCPTUN for remote transmission acceleration.
Alibaba Cloud – GRTN video transmission acceleration service uses KCP for audio/video data optimization.
By sacrificing an additional 3‑8% bandwidth, KCP provides stable latency and reduces packet loss. Further performance comparisons between TCP and KCP under varying network conditions will be covered in the next article.
JD Tech Talk
Official JD Tech public account delivering best practices and technology innovation.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
