Reference · Statelessness

C++20/23 services on containers.

A companion reference set on C++20/23 service design for Linux containers, focused on statelessness as a deployment property — what makes a service safe to kill, replace, and replicate at the orchestrator's discretion, and how the C++ language features, library choices, and operational patterns either support that property or quietly undermine it.

The treatment is opinionated and reference-style. Where positions are taken they are marked as > Opinion. callouts; where multiple acceptable approaches exist, both are described with their trade-offs.

The set

Read in order the first time. Each doc builds on the prior. For a reader with limited time, the irreducible minimum is Doc 01 (vocabulary), Doc 04 (process-scoped state and the State Architecture Table), and Doc 10 (the gRPC capstone integration) — these three together cover the architectural shape without the depth on individual mechanisms.

Index

00 Index and reading guide

The map: reading order, scenario guide, recurring themes, the State Architecture Table. Start here if you have time for only one document.

Doc 01

01 Stateless vs stateful as deployment posture

The vocabulary doc. Statelessness is not a code property; it's a deployment posture the same C++ binary can or cannot satisfy depending on what state it holds. Three-scope vocabulary, six monolith intuitions that mislead.

Doc 02

02 RAII as the foundation for safe stateful work

The mechanism that makes request scope concrete. The RequestContext pattern, the three exception-safety guarantees, the gRPC callback API as the request boundary, the common mistakes (throwing destructors, raw pointers, locks across co_await), and the performance side of construction and destruction in hot paths.

Doc 03

03 PMR's monotonic_buffer_resource as architectural statelessness

PMR as the in-language realization of "request brings its own memory, all releases together." The request arena RAII pattern, the canonical layered monotonic + unsynchronized_pool recipe, container-type choices, the lifetime trap counterexample, C++23 additions.

Doc 04

04 Process-scoped state that's still stateless

Not all state is request-scoped. The State Architecture Table is introduced here. TracerProvider, gRPC channels, connection pools, prepared-statement caches, PMR upstream resources, in-process caches, parsed configuration. The main()-owned wiring pattern. OS container limits as the budget for sizing.

Doc 05

05 Threading and concurrency in a stateless service

The cross-cutting concern that interacts with statelessness in non-obvious ways. TLS as process-scoped (CorrelationGuard), stack-based vs stackless concurrency, gRPC's threading model, inter-thread communication, CPU limits (CFS quota mechanics, throttling tail latency, allocator arena counts under cgroup limits), std::stop_token cooperative cancellation.

Doc 06

06 12-Factor adapted to C++

The opinionated document. Most twelve factors map directly to C++; three collide: Config (compile-time/link-time/env-time/runtime split), Processes (Meyers singleton critique and DI alternative), Disposability (C++ startup tax, staged-startup pattern, constinit).

Doc 07

07 State externalization patterns

What goes outside the process: authoritative session state, counters, durable workflow, queues, business data. Connection pools as process-scoped infrastructure with per-handler RAII checkout. ScopedConnection pattern with invalidate() for broken connections. Idempotency keys, deadlines propagating to backing services, the Outbox pattern.

Doc 08

08 The ephemeral filesystem trap

The container filesystem is ephemeral. The overlayfs layer model, read_only: true as the forcing function, what goes where, logs to stdout, the C++ library traps (spdlog file defaults, crash dumps, ML model caches, coverage data, prometheus_cpp textfiles), restart semantics as a feature.

Doc 09

09 Health checks as the public API of statelessness

The orchestrator's API into the service. The three probes (startup, liveness, readiness). The gRPC standard health protocol with HealthCheckServiceInterface. Separate-port vs same-port trade-offs. The graceful-shutdown sequence that ties together stop_token, pool draining, and reverse-order destruction. Anti-patterns.

Capstone

10 Microservices with gRPC and C++ (capstone)

The capstone integration. A realistic order-pricing service skeleton: proto, Config, RequestContext, process-scoped state wiring, the full handler with PostgreSQL access and outbound gRPC tax calculation and idempotency, the complete main() with twelve cross-referenced steps, full podman-compose.yaml, full Kubernetes manifest. Every prior pattern in one place.

Appendix

11 Build tooling appendix

Conan 2.x setup with version-pinned dependencies, dev and release profiles (AddressSanitizer in dev, LTO + hardening in release), CMake structure with proto codegen, the C++23 feature support table, multi-stage Containerfile pattern, full source for vendored helpers, test infrastructure, common gotchas.

Where this fits in the tutorial

The main tutorial sections — _docs/01 through _docs/16 — cover modern-C++ performance work inside containers: image strategy, compile-time wins, STL/layout, allocators, I/O, observability, isolation, quality. This reference set goes wider on a single architectural concern that touches all of those: what makes a C++ service safe to deploy as a replaceable replica.

Cross-references go both directions. The main tutorial's §3 (RAII discipline), §7 (memory management), and §11 (noisy neighbors) all overlap with material here; this set develops the ideas at the service-design level rather than the performance-knob level.