r/cprogramming • u/Whole-Low-2995 • Oct 10 '26
Should I Replace a Ring Buffer with a Lock-Free Queue in My Web Framework?
I have an experimental web framework I'm currently working on.
While going through the code to clean up styling before tagging a stable release, I've been debating this specific part in the HTTPS module:
https://github.com/c4punks/CWIST/blob/dev/src/net/http/https.c
Right now, this code just uses a dumb, massive ring buffer to absorb connection bursts, and I'm wondering if I should switch it to a lock-free queue.
There are three main things that make me hesitate:
* State can get pretty tangled and messy in a lock-free queue.
* We have a tiny team and I'm maintaining almost all of this alone. If something breaks with CAS (even if the odds are low, if I mess up the code during refactoring), the blast radius on the dev branch will just be too big.
* I also wonder how often real-world traffic actually warrants lock-free. Plenty of web frameworks handle traffic just fine without this level of extreme optimization.
Considering that I'm still a junior developer and lack experience, is it really worth swapping this out for a lock-free structure, and would it actually make a noticeable difference?
11
u/mrtlo Oct 10 '26
How about profiling it?
5
u/oriolid Oct 10 '26 •
This may be a hot take, but lock free usually isn't worth it. If your locks aren't contented, the standard mutex on Linux or BSD has really low overhead and you shouldn't really be running servers on Windows. If your locks are contented, lock free usually degrades into spinlock and that's almost always worse than mutex. Wait free algorithms that don't do that exist but they get really complicated.
If we turn the question around and you want to do lock free and need an application where it's required, try realtime audio applications. HFT might qualify too.
2
u/zhivago Oct 10 '26
Are you not i/o bound?
1
u/Nothing_from_void Oct 10 '26
Lock free is almost never worth it while also being really fiddly to program. Did you have a particular reason for trying it?
1
u/epasveer Oct 10 '26
Try both. Let's us know.
1
u/DanKegel Oct 10 '26 •
How do you test this server?
Before any such refactoring, I'd make sure the project's fuzz testing / static code analysis / dynamic code analysis / benchmarking / AI code review game was top notch.
Also, two million active connections? That's a lot of file descriptors! If each one uses 32KB of RAM total, that's 64GB. Golly.
1
u/DanKegel Oct 10 '26
Nice to see zero errors at c100k scale :-)
2
u/LiquidityC Oct 11 '26
If it’s not broken don’t fix it. Also I always liked ring buffers. Fun to build sweet sweet static memory footprint. Homer Simpson drooling noise.
2
u/Interesting_Debate57 Oct 11 '26
You need to know a lot more and why you would want to switch. If it's not your idea, why do they want to do it? If it doesn't sound like it's going to massively improve things, why do it? Just for fun is fine; I recommend looking at Rochester's cs department for a few very simple but correct implementations.
CAS, generally, is chip level. You shouldn't be using it directly unless you have an extremely good reason to do so.
1
u/DanKegel Oct 10 '26
You want a really good set of unit and system tests for this.
And then, this being the future and all, you should see if AI can find bugs. Github copilot is pretty good at that, so have it review each and every commit, and fix everything it finds.
1
u/throwaroo202020 Oct 10 '26
The tcp/ip stack is your connection queue.