TCP Server
A high-performance TCP server built with epoll for efficient I/O multiplexing. Designed to handle 10,000+ concurrent connections with minimal CPU and memory overhead, implementing the C10K problem solution.
Overview
The C10K problem - handling 10,000 concurrent connections on a single server - was a significant engineering challenge that drove innovation in server architecture. This project implements a solution using Linux's epoll for efficient event-driven I/O.
Unlike traditional thread-per-connection models that don't scale, this server uses a single-threaded event loop that can handle thousands of connections efficiently. It's the same architecture used by nginx and Redis.
Key Features
epoll-based I/O
Edge-triggered epoll for O(1) event notification, avoiding the O(n) overhead of select/poll on large connection sets.
Non-blocking Sockets
All socket operations are non-blocking, allowing the server to handle partial reads/writes gracefully without stalling.
Buffer Pool
Pre-allocated buffer pool to avoid per-connection memory allocation, reducing memory fragmentation and GC pressure.
Connection Timeouts
Timer wheel implementation for efficient O(1) timeout management across thousands of connections.
Architecture
The server follows a reactor pattern with a single event loop. When data arrives on any socket, epoll wakes up and returns a list of ready file descriptors. The server then processes each one non-blocking, reading available data into buffers and writing pending responses.
Connection State Machine: Each connection maintains its own state (reading headers, reading body, writing response) allowing the event loop to pick up where it left off after each epoll wait.
Benchmarks show the server handling 15,000 concurrent connections with only 50MB of memory and single-digit CPU utilization under moderate load.
Tech Stack
What I Learned
This project gave me hands-on experience with systems programming at a low level. I learned about edge-triggered vs level-triggered epoll, the importance of non-blocking I/O, and how to design state machines for connection handling.
I also learned about TCP internals: the three-way handshake, Nagle's algorithm (and when to disable it), and how to tune socket buffer sizes for optimal throughput.