A high-performance, asynchronous HTTP/1.1 web server built from scratch using C++17. This project explores low-level systems programming, focusing on efficient concurrency models, non-blocking I/O, and robust memory management.
- Custom Thread Pool: Implemented a fixed-size worker pool to manage task execution, reducing the overhead of thread creation/destruction.
- Thread Safety: Utilized
std::mutexandstd::condition_variablefor safe task queuing and producer-consumer synchronization. - Hardware Concurrency: Automatically detects CPU cores to optimize the number of worker threads.
- I/O Multiplexing: Leveraged Linux epoll (Edge-Triggered) to handle thousands of concurrent TCP connections with a single reactive thread.
- Non-blocking I/O: All socket operations are non-blocking to prevent worker threads from stalling on slow network clients.
- HTTP/1.1 Implementation: Handled persistent connections (Keep-Alive) and robust request parsing.
- Smart Pointers: Eliminated manual memory management using
std::unique_ptrandstd::shared_ptrto ensure zero memory leaks. - RAII Wrappers: Encapsulated system resources (File Descriptors, Sockets) in classes to ensure automatic cleanup even during exceptions.
- LRU Cache: Implemented a thread-safe Least Recently Used (LRU) cache to serve frequently requested static files directly from memory.
- Unit Testing: Integrated Google Test (GTest) to validate core logic, including the HTTP parser and the thread pool's task scheduling.
- Stress Tested: Validated stability under load using benchmarking tools, ensuring zero memory growth during sustained high-concurrency traffic.
- Reactor Thread: Listens for new events via
epoll. - Task Queue: New requests are wrapped as tasks and pushed to a thread-safe queue.
- Worker Threads: Pull tasks from the queue, parse HTTP headers, check the LRU cache, and serve files or error codes.
- Logging: Thread-safe logger records access and system errors asynchronously.
.
├── include/ # Header files (Server, ThreadPool, Cache, etc.)
├── src/ # Implementation files
│ ├── main.cpp # Entry point
│ └── epoll_wrapper.cpp
├── tests/ # Unit tests using Google Test
├── public/ # Static assets (HTML, CSS, JS)
├── config.json # Server configuration (Port, Thread count, Cache size)
└── CMakeLists.txt # Build system
- Linux Environment (Ubuntu 20.04+ recommended)
- C++17 compatible compiler (GCC 7+ or Clang)
- CMake 3.16+
# 1. Clone and enter directory
git clone <your-repo-link>
cd multithreaded-webserver-cpp
# 2. Build the project
mkdir build && cd build
cmake ..
make -j$(nproc)
# 3. Start the server
./bin/webserver 8080
# Inside the build directory
./webserver_tests
Under local stress testing, this server demonstrates:
- Throughput: 70,000+ requests per second.
- Stability: Constant memory footprint (approx. 1.7MB) under 5,000+ concurrent connections.
- Efficiency: Uses a token-bucket rate limiter to protect against basic DoS attempts.