Project 9: High-Performance Server with epoll

Rewrite your echo/chat server using Linux’s epoll interface, capable of handling 10,000+ concurrent connections with a single thread. Implement proper edge-triggered handling.

Quick Reference

Attribute Value
Primary Language C++
Alternative Languages C, Rust
Difficulty Level 3: Advanced
Time Estimate 1-2 weeks
Knowledge Area Linux epoll, Non-Blocking I/O, Event Loop
Tooling nginx-style event loop
Prerequisites Projects 3-4, Linux system

What You Will Build

Rewrite your echo/chat server using Linux’s epoll interface, capable of handling 10,000+ concurrent connections with a single thread. Implement proper edge-triggered handling.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • epoll API usage → maps to epoll_create, epoll_ctl, epoll_wait
  • Edge-triggered mode → maps to draining sockets completely, handling EAGAIN
  • Non-blocking everything → maps to accept4() with SOCK_NONBLOCK
  • Connection state management → maps to per-connection context objects

Key Concepts

  • epoll: “The Linux Programming Interface” Chapter 63 - Kerrisk
  • Edge vs Level Triggered: “The Linux Programming Interface” Section 63.4.6 - Kerrisk
  • C10K Problem: http://www.kegel.com/c10k.html
  • Event Loop Design: Redis source code (ae.c)

Real-World Outcome

$ ./epoll_echo_server 9000
epoll echo server on port 9000 (edge-triggered)

# Benchmark with many connections:
$ wrk -t4 -c1000 -d10s http://localhost:9000/
Running 10s test @ http://localhost:9000/
  4 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.23ms    0.45ms   12.3ms   98.2%
    Req/Sec    25.1k     1.2k     28.3k    89.1%
  1,000,000 requests in 10.00s
  Requests/sec: 100,000

# Server stats:
$ ./epoll_echo_server 9000 --stats
Connections: 1,000 active
Bytes in: 45 MB/s
Bytes out: 45 MB/s
Events/sec: 250,000

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: LEARN_CPP_NETWORK_PROGRAMMING.md
  • “The Linux Programming Interface” by Michael Kerrisk