Microservices in C++: Where to Start in 2024
\n\nWhen you hear "microservices," Go, Java, or Python usually come to mind. But C++? It seems like this language is too low-level, complex, and not meant for the web at all. But that's a mistake. In recent years, C++ has experienced a rebirth in backend development: modern standards (C++17/20), lightweight frameworks, and mature tools make it an excellent choice for high-load microservices. If your project requires minimal memory consumption, maximum performance, or work on ARM/MIPS, C++ can be a secret weapon.
\n\nIn this article, I'll show you where to actually start: we'll choose a framework, write a simple HTTP service, add gRPC, and wrap everything in Docker. No fluff — just code and practice.
\n\n\n\n
Why C++ for Microservices Isn't Crazy?
\n\nThe main argument of C++ opponents: "slow to write, easy to make mistakes." But modern C++ with smart pointers, STL containers, and RAII allows you to write almost as fast as in Go, while giving you full control over memory. The advantages are obvious:
\n\n- \n
- Speed — C++ is 2-10 times faster than Java/Python on I/O operations and computations. \n
- Memory consumption — a typical C++ microservice takes 5-15 MB compared to 50-150 MB in Java. \n
- No GC — no garbage collector "freezes," predictable latency. \n
- Ecosystem — gRPC, Boost, nlohmann/json, Crow, Drogon — everything works out of the box. \n
There are also downsides: stricter compiler, longer build times, harder debugging. But if you need performance at the level of financial systems, game servers, or IoT — C++ is justified.
\n\n\n\n
Choosing a Framework: Crow vs Drogon vs Pistache
\n\nFor a quick start, you don't need to write an HTTP server from scratch on sockets. There are three popular options:
\n\n1. Crow (lightweight, header-only)
\nIdeal for microservices with up to 5-10 endpoints. Just include one header file — and you're done. Downside: no HTTP/2 support, weak routing.
\n\n2. Drogon (full-featured)
\nThe best choice for a serious project: HTTP/1.1 and HTTP/2, WebSocket, AOP, database support, ORM. Drogon is inspired by ASP.NET and Django — it can even automatically generate JSON from models.
\n\n3. Pistache (modern but raw)
\nAn interesting alternative, but not yet stable for production.
\n\nI recommend starting with Crow — it's minimalistic and lets you get a working REST API in 10 minutes. For serious projects, look towards Drogon.
\n\n\n\n
Practice: Writing a Microservice in C++ with Crow
\n\nLet's create a simple service that accepts JSON with a username and returns a greeting. This is a classic "Hello World," but in microservice style.
\n\nStep 1. Installing Dependencies
\nYou'll need CMake and a compiler with C++17 support (gcc 9+, clang 10+). Crow is header-only, so just copy the crow_all.h file from the repository.
# Download Crow\nwget https://github.com/CrowCpp/Crow/releases/download/v1.1.0/crow_all.h\n\n\nStep 2. Service Code
\n\n// main.cpp\n#include "crow_all.h"\n#include <iostream>\n#include <string>\n\nint main() {\n crow::SimpleApp app;\n\n // GET /hello — health check\n CROW_ROUTE(app, "/hello")\n ([](){\n return crow::response(200, "Hello, World!");\n });\n\n // POST /greet — accepts JSON {"name": "Alice"}\n CROW_ROUTE(app, "/greet").methods(crow::HTTPMethod::POST)\n ([](const crow::request& req){\n auto body = crow::json::load(req.body);\n if (!body || !body.has("name")) {\n return crow::response(400, "Bad Request: missing 'name'");\n }\n std::string name = body["name"].s();\n crow::json::wvalue result;\n result