Why KCP Beats TCP/UDP for Real‑Time Apps and How to Implement It

This article explains the shortcomings of TCP and UDP for latency‑sensitive applications, introduces the KCP protocol as a reliable yet low‑latency UDP‑based solution, outlines its key features and operation, and provides practical code examples for integration.

JD Cloud Developers
JD Cloud Developers
JD Cloud Developers
Why KCP Beats TCP/UDP for Real‑Time Apps and How to Implement It

Overview

In modern internet applications, real‑time and smooth communication are critical, especially for online games, video conferencing, and live streaming. TCP provides reliability but incurs high latency due to congestion control and retransmission, while UDP is fast but lacks reliability.

Why Introduce KCP

TCP guarantees connection reliability without guaranteeing transmission efficiency. UDP offers maximum transmission efficiency but cannot guarantee reliability.

KCP Introduction

The KCP protocol was created to fill the gap between 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 in 2014 by skywind3000. It adopts TCP‑like mechanisms such as ACK, timeout retransmission, and flow control, but streamlines and optimizes them for real‑time scenarios.

Key Features

Low latency: aggressive retransmission reduces waiting time.

High throughput: dynamically adjusts sending rate to fully utilize bandwidth.

Reliability: ensures ordered delivery and selective retransmission of lost packets.

Flexibility: rich configuration options allow trade‑offs between latency, throughput, and reliability.

How KCP Works

KCP implements TCP‑like reliability on top of UDP, including:

ACK: receiver sends acknowledgment after receiving a packet.

Timeout retransmission: sender retransmits if ACK not received within a timer.

Flow control: sliding window prevents buffer overflow.

Congestion control: simple algorithm adjusts sending rate based on network conditions.

Application Scenarios

KCP is used in many latency‑sensitive contexts such as mobile gaming, video conferencing, live streaming, and IoT data transfer.

Mobile games (e.g., Honor of Kings, real‑time robot control)

Video conferencing

Live streaming

IoT device communication

Pros and Cons

Advantages

Low latency

High throughput

High reliability

Configurable

Disadvantages

More complex to implement than UDP

Additional bandwidth overhead

Sensitivity to network jitter

How to Use

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);
// Output function called by KCP to send data
int udp_output(const char *buf, int len, ikcpcb *kcp, void *user) {
    // ...
}
kcp->output = udp_output;
// Periodically call ikcp_update with current clock (ms)
ikcp_update(kcp, millisec);
// When a UDP packet is received, feed it to KCP
ikcp_input(kcp, received_udp_packet, received_udp_size);

Conclusion

For mobile scenarios where server RTT cannot stay under 2 ms and message RTT cannot stay under 3 ms, introducing KCP can replace TCP to achieve 1‑RTT message exchange, shortening app response time. Several commercial products already use KCP to improve user experience, such as Genshin Impact, SpatialOS, Westward Games, NetEase CC, NetEase BOBO, NetEase UU accelerator, and Alibaba Cloud video acceleration.

TCPUDPprotocol optimizationKCPreal-time networking
JD Cloud Developers
Written by

JD Cloud Developers

JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.