Introduction: Why C++ for Microservices?
Microservice architecture has firmly entered the world of development, but is traditionally associated with languages like Go, Java, or Python. However, C++ remains indispensable where performance, low latency, and efficient memory usage are critical. In this article, we will analyze how to build microservices in C++, which libraries to use, and how to organize the infrastructure.
Main advantages of C++ in microservices:
- Maximum performance (natively compiled code)
- Minimal memory consumption
- Full control over resources
- No garbage collector (predictable latency)
We will examine architecture, REST API implementation, gRPC, working with queues, and containerization using practical examples.
Microservice Architecture in C++
Basic Principles
A microservice in C++ should be:
- Autonomous — each service works independently
- Loosely coupled — minimal dependencies between services
- Business-function oriented — one service solves one task
- Fault-tolerant — graceful degradation
Typical Project Structure
my_microservice/├── CMakeLists.txt├── src/│ ├── main.cpp│ ├── server/│ │ ├── http_server.cpp│ │ └── http_server.h│ ├── service/│ │ ├── order_service.cpp│ │ └── order_service.h│ ├── repository/│ │ ├── order_repository.cpp│ │ └── order_repository.h│ └── config/│ ├── config.cpp│ └── config.h├── include/│ └── (public headers)├── tests/│ └── test_order_service.cpp└── DockerfilePractical Implementation: REST API Microservice
Choosing an HTTP Library
For C++, the best options are cpp-httplib (lightweight, header-only) and Drogon (full-featured framework). Let's look at an example with cpp-httplib.
Example: Order Management Service
// main.cpp#include <httplib.h>#include <nlohmann/json.hpp>#include <iostream>#include <memory>#include "order_service.h"
using json = nlohmann::json;
int main() { httplib::Server svr; auto orderService = std::make_shared<OrderService>();
// POST /orders — create order svr.Post("/orders", [orderService](const httplib::Request& req, httplib::Response& res) { try { auto data = json::parse(req.body); auto order = orderService->createOrder(data); res.set_content(order.dump(), "application/json"); res.status = 201; } catch (const std::exception& e) { res.set_content(json{{"error", e.what()}}.dump(), "application/json"); res.status = 400; } });
// GET /orders/{id} — get order svr.Get(R"(/orders/(\\d+))", [orderService](const httplib::Request& req, httplib::Response& res) { int orderId = std::stoi(req.matches[1]); auto order = orderService->getOrder(orderId); if (order) { res.set_content(order->dump(), "application/json"); } else { res.status = 404; res.set_content(json{{"error", "Order not found"}}.dump(), "application/json"); } });
std::cout << "Server started on port 8080" << std::endl; svr.listen("0.0.0.0", 8080); return 0;}// order_service.cpp#include "order_service.h"#include <stdexcept>
using json = nlohmann::json;
json OrderService::createOrder(const json& data) { if (!data.contains("product_id") || !data.contains("quantity")) { throw std::invalid_argument("Missing required fields"); }
Order or