← Explore·

#serverdevelopment

Public entries tagged #serverdevelopment

🛑 Graceful Shutdown in C++ — because kill -9 is not a deployment strategy

Your server handles thousands of connections. You deploy a new version. What happens to the clients mid-request when the process dies?

Without graceful shutdown — nothing good. 💀

🔌 Step 1 — Catch the signal
The OS sends SIGTERM before it sends SIGKILL. That’s your window.
signal(SIGTERM, on_shutdown);
Set a flag, don’t do heavy work inside the handler. Simple and safe.

🚫 Step 2 — Stop accepting new connections
Close or stop listening on your socket immediately. New clients will get a clean refusal instead of a sudden reset.

⏳ Step 3 — Drain existing connections
Let active requests finish. Give them a deadline — 5 or 10 seconds is usually enough. After that, you cut them loose. Your call.

📝 Step 4 — Flush your logs
Before the process exits, tell spdlog to flush:
—> spdlog::shutdown();
Because the last thing you want is missing log entries right before a crash or restart.

🔄 Combined with SO_REUSEPORT the full flow becomes elegant:
→ New process starts, binds the port
→ Old process catches SIGTERM
→ Drains connections, flushes logs
→ Exits cleanly

Zero downtime. Zero lost requests. Zero mystery gaps in your logs. 🐧

Continue reading →

Subscribe to #serverdevelopment entries via RSS feed