skywind3000/kcp logo

skywind3000/kcp

Free

:zap: KCP - A Fast and Reliable ARQ Protocol

FreeFree tier
Type
Open Source

About skywind3000/kcp

KCP is a fast and reliable ARQ (Automatic Repeat-reQuest) protocol implemented in pure C. It is designed to reduce average latency by 30-40% and maximum latency by three times compared to TCP, at the cost of 10-20% additional bandwidth usage. KCP is a pure algorithm implementation that does not handle underlying protocol transmission (e.g., UDP); it requires the user to define how to send packets via callbacks. The protocol consists of only two source files (ikcp.h and ikcp.c), making it easy to integrate into existing projects. KCP employs several techniques to improve transmission speed: non-exponential RTO backoff (1.5x instead of 2x), selective retransmission, fast retransmit (triggers after two duplicate ACKs), non-delayed ACK, and combined UNA+ACK in every packet. It also provides configurable congestion control, with a normal mode similar to TCP and a fast mode that sacrifices some fairness for lower latency. KCP is widely used in real-time applications such as online gaming, live streaming, and other scenarios where low latency over unreliable transport is critical.

Key Features

Fast retransmit triggered by duplicate ACKs (configurable threshold)
Selective retransmission (only retransmits lost packets, not all after the lost one)
Non-exponential RTO backoff (1.5x instead of TCP's 2x) for faster loss detection
Combined UNA+ACK in every packet for efficient acknowledgement
Configurable non-delayed ACK mode
Optional non-congestion control (non-backoff flow control) for latency-sensitive small data
Normal and fast operation modes via ikcp_nodelay()
Pure algorithm with no system calls; requires external clock and callback for packet output
Minimal footprint: only ikcp.h and ikcp.c
Supports customizable MTU, window size, and minimum RTO

Pros & Cons

Pros
  • Significantly lower latency (30-40% average, up to 3x max) compared to TCP
  • Open-source and permissively licensed (MIT-like)
  • Very easy to integrate: two source files, no external dependencies
  • Highly configurable to balance between speed and bandwidth usage
  • Pure algorithm – no OS-specific calls, portable to any platform
Cons
  • Uses 10-20% more bandwidth than TCP for the same throughput
  • Not a full transport protocol; requires underlying unreliable transport (e.g., UDP) and manual packet I/O
  • No built-in MTU discovery; MTU must be set manually
  • Does not handle session management, encryption, or congestion control in the same comprehensive way as TCP
  • Not designed for bulk data transfer where bandwidth efficiency is paramount

Best For

Real-time multiplayer online games over UDPLive streaming and video conferencing requiring low latencyP2P file transfer and communication protocolsCustom reliable transport layer on top of UDP or other unreliable protocolsIoT and embedded systems needing efficient reliable data transmission

FAQ

What is KCP?
KCP is a fast and reliable ARQ protocol that prioritizes low latency over bandwidth efficiency. It is a pure algorithm implemented in C, consisting of ikcp.h and ikcp.c, and can be easily integrated into custom UDP-based protocols.
How does KCP differ from TCP?
KCP is designed for speed of delivery (per-packet latency) rather than throughput. It uses techniques like non-exponential RTO backoff, selective retransmission, and fast retransmit to reduce latency by 30-40% on average, at the cost of 10-20% more bandwidth usage.
How do I install KCP?
You can copy the ikcp.h and ikcp.c files directly into your project, or use vcpkg: 'vcpkg install kcp'. The library is open-source and hosted on GitHub at github.com/skywind3000/kcp.
Does KCP work with TCP?
KCP is independent of TCP. It operates on top of unreliable protocols like UDP. However, you can combine KCP with TCP if needed, but the primary use case is replacing TCP in latency-sensitive applications.
What is the difference between normal mode and fast mode?
Normal mode behaves similarly to TCP with fair congestion control. Fast mode (enabled via ikcp_nodelay with resend=2, nc=1) disables congestion control and uses aggressive retransmission to achieve lower latency, at the expense of fairness and higher bandwidth usage.