HACKER Q&A
📣 golly_ned

Learning how to build high-performance servers in C++/Java?


Hi,

I've developed an interest in performance, and I'd like to try to build a high-performance server from scratch in C++ or Java. I'm looking for good resources about how to do this. I'd considered looking at codebases for existing servers, but I think I'd end up missing a lot of the important details. Does anyone have any suggestions about good resources for this?

Thank you.


  👤 devdude1337 Accepted Answer ✓
Your question seems a bit too generalized imho. It’s a bit like asking for resources about building a fast vehicle, without specifying what kind of…a fast car is certainly much differently built than a fast ship or plane. I assume you want to make a high-performance web-server. Now performance can mean different things: do you need to serve lots of parallel requests, or do you need to shovel large amounts of data, or you need fast complex calculations? Or all of it? Every type of "performance" is a separate problem and by solving and integrating them you could achieve a "high-performance" server. However, performance per se is not a reason to start a project. It is sometimes a necessity (e.g. financial transactions, game servers, page load times, etc.) for the desired quality of a service and its business requirements. Writing efficient and effective code is the way to reach good speed. In C++ for example it’s often wise to use vector.reserve and plan ahead your memory need. Using static and const where appropriate and enable the compiler to generate best results. There may be similar approaches for Java, too… Anyway performance is achieved by efficient code and well-designed architecture. That’s why you may not find much resources about such a broad topic. And oh, don’t write a web server from scratch in C++ - at least wait for C++-20 to be adopted everywhere

👤 jonahbenton
From a pure performance perspective the most important aspect of servers is the kernel/userspace IO handoff, and relatedly the scheduling of work. The best way to learn and study this is to work in C.

Writing a server in C++ or (especially) Java is an exercise in sacrificing some elements of pure performance to deliver an artifact your team has the skills to maintain or build on. There are specific- quite different- performance considerations in those languages that are really about the quite different runtimes of those languages. The mechanical things one does in Java to avoid garbage are different than what one does in C++ to solve for codegen.