A single-threaded HTTP/1.1 server written in C, which can manage over 55,000 requests per second on a mid-range desktop.
I got this performance by leaning heavily on Linux kernel features:
epollis used for I/O event notifications;sendfileis used to send files without an intermittent buffer;- namespaces and
pivot_rootare used for filesystem isolation; TCP_CORKto group smallsendandsendfilecalls together.
To elaborate on the namespace part a bit: the ./site/ directory becomes / for
the chttpd process, which means if you request /index.html the file is
actually located at /index.html as far as the server is concerned.
No path processing needed.
I considered making it multithreaded, but honestly I'm not sure I even need to.
If you need more than 55,000 requests per second, use a real server like nginx. <3
The code, as-is, doesn't need root. But what if you want to run it on port 80?
- Change the port from
8080to80. - (Re)build chttpd.
- Run
setcap CAP_NET_BIND_SERVICE=+eip chttpd(probably as root).
In theory it should work!
System configuration:
- Ryzen 5 7600
- 32GB DDR5-5400 (chttpd uses less than 5KB on my system, though)
- 2TB nvme SSD.
Performance test for a large number of small requests:
- Consistently over 55,000 requests/second.
- Response times: 0.0045s slowest / 0.0009s average / 0.0001s fastest.