r/cpp • u/RefrigeratorFirm7646 • Jun 04 '26
I spent a month optimizing my epoll based HTTP server from 15k req/sec to 125k req/sec
Greetings to my fellow nerds.
A month ago, I had zero network programming experience. So I decided to fix that by building an epoll based HTTP server from scratch and benchmarked every major architectural change along the way.
Performance Benchmarks :
- Benchmark command :
wrk -t4 -c10000 -d10s http://127.0.0.1:8080/ - Request:
GET /index.html - Response: Static HTML file (~1500 bytes)
- CPU: Intel i5-13420H (13th Gen)
- Compiler: Clang (O3)
| Architecture | Throughput (req/sec) | Description |
|---|---|---|
| Blocking | ~15k | Single threaded blocking accept/read/write |
| Epoll (LT) | ~34k | Single threaded event loop utilizing non blocking I/O multiplexing |
| Epoll (LT, keep alive) | ~37.5k | Single threaded event loop with persistent connections |
| Epoll (LT, keep alive, sendfile) | ~41k | Single threaded event loop with persistent connections and zero copy file serving |
| Epoll (LT, keep alive, sendfile, multithreading) | ~125k | Multithreaded architecture running 4 concurrent epoll loops (optimal on test machine) |
Some Surprising Observations :
- sendfile mattered less than I expected... for a server whose entire purpose is to serve files, I was expecting a bigger gain but maybe because my file was only ~1.5KB, it did not help much.
- More threads made things worse :
| Worker Threads | Throughput (req/sec) |
|---|---|
| 1 | ~40k |
| 2 | ~95k |
| 3 | ~115k |
| 4 | ~125k |
| 5 | ~90k |
| 6 | ~90k |
| 8 | ~75k |
| 10 | ~70k |
| 12 | ~65k |
My CPU has 6 physical cores and 12 logical processors, I suspect that the cost of all the syscalls for every loop, context switching, and lock contention on shared kernel objects, dominated on higher thread counts. Though I havent fully investigated it yet.
Profiling with perf :
| Function | Approx. CPU Samples |
|---|---|
| readSock() | ~22% |
| writeSock() | ~16% |
| parse() | ~8% |
| std::format() | ~7% |
| open() | ~3% |
| sendfile() | ~2.5% |
Turns out Im still spending more time reading and parsing requests than sending responses, meaning there might still be room for batched reads or buffer pooling in a future iteration...
Final Thoughts :
I could hunt for possible micro optimizations or even experiment with an edge triggered architecture but im kinda burnt out at this point and this feels like a great point to end this project...
The codebase is pretty small (~1k LOC), so if anyone's interested in taking a look : https://github.com/Raju1173/epoll-http-server
64
u/ChallengeDiaper Jun 04 '26
A couple of observations I’m basing off of your localhost (assuming this is how you tested).
Put wrk on a different machine and retest if you’re curious about real world numbers.
Great job for having no networking experience a month ago!
5
u/Inevitable-Round9995 Jun 05 '26
I've implemented IO_URING in ~300 Loc of C++. https://gist.github.com/EDBCREPO/244601ebf7b5c4094d39911b745daa34
6
u/lovelacedeconstruct Jun 04 '26
Have you tried a single epoll loop that just pushes work into a threadpool
2
u/6502zx81 Jun 04 '26
How many requests/s do other servers serve? 125k ain't much; 15k is doable with select().
2
u/positivcheg Jun 04 '26
How far did you go? Cuz I wonder, did you do the thread to core binning? Because that’s what makes games run smooth, especially on PlayStation platform - they literally reserve some cores for specific threads and there is just no switching whatsoever.
6
u/justkdng Jun 04 '26
If you are on localhost you are testing memory instead of network. Try testing on a raspberry pi since you are only serving a simple index.html to see how it performs in a constrained environment.
5
u/pdath Jun 04 '26
I've just worked up to a single threaded epoll loop myself. But that is all the concurrency my app should need.
1
u/tmlildude Jun 05 '26
is this kind of workload mostly network bound or io bound?
1
u/Charming-Work-2384 Jun 05 '26
Look at GWAN ...server.. please search on web
1
u/Aalexander_Y Jun 06 '26
You can squeeze out things even more with Napi and the kernel
https://netdevconf.info/0x18/docs/netdev-0x18-paper10-talk-slides/Real%20world%20tips,%20tricks,%20and%20notes%20of%20using%20epoll-based%20busy%20polling%20v2.pdf
1
u/Apprehensive_Poet304 Jun 07 '26
This seems quite slow, no? Have you set BUSY_POLL for lower latency or have INCOMING_CPU for NIC routing? Try that as well, or try pinning the threads. I would also try using an Edge Triggered Epoll setup if you want to push things further. There should definitely be far more you can push (if you’re just echoing back to client as of now). I’ve made something similar and hit 2.5 million requests per second, but perhaps that was because I was using a pretty nice laptop I was borrowing from my college—but I’m sure you can go further
17
u/[deleted] Jun 04 '26
[removed] — view removed comment
13
u/[deleted] Jun 04 '26
[removed] — view removed comment
4
u/[deleted] Jun 04 '26
[removed] — view removed comment
0
u/[deleted] Jun 04 '26
[deleted] — view deleted comment