İçeriğe Atla
Mustafa Erbay
Tutorials · 10 min read · görüntülenme Türkçe oku
100%

Zero-Downtime Restart with systemd Socket Activation

A runbook for shrinking deploy impact by separating connection acceptance into a socket unit, so the listening port never drops during service restarts.

Zero-Downtime Restart with systemd Socket Activation — cover image

In production, the sentence “I restarted the service and we ate 502s for 10 seconds” is often normalized. But when a service is genuinely critical (auth, gateway, queue consumer, control-plane), it is possible to push the restart impact close to zero.

systemd’s socket activation capability decouples the work of “listening on a port” from the service process. As a result, while the service process is being restarted, the listening socket stays up and connection acceptance is never interrupted.

What does socket activation actually solve?

Socket activation reduces the following problems:

  • The brief port closure during a restart
  • Load balancer health-check flapping during deploy waves
  • Cold-start effects from “the service didn’t come up fast enough”

Prerequisites

  • Linux + systemd (most modern distros)
  • Your service must be able to consume the systemd socket via LISTEN_FDS / LISTEN_PID
    • Many languages/runtimes support this (Go, Rust, and Node ecosystems have wrappers)
    • If your own service doesn’t support it, a simple “socket-accept” wrapper can also bridge the gap

Since the focus of this post is on the mechanism, I’ll keep the examples minimal.

Architecture: .socket + .service

The model:

  • myapp.socket listens on the port
  • When a connection arrives, myapp.service starts (or, if already running, uses the FD)

1) Sample socket unit

/etc/systemd/system/myapp.socket:

[Unit]
Description=myapp socket

[Socket]
ListenStream=127.0.0.1:8080
NoDelay=true
ReusePort=false

[Install]
WantedBy=sockets.target

Notes:

  • The ListenStream address depends on your security model (if you bind to 0.0.0.0, a firewall is mandatory).
  • ReusePort tends to introduce unintended complexity in most scenarios; keep it off initially.

2) Sample service unit

/etc/systemd/system/myapp.service:

[Unit]
Description=myapp service
After=network.target
Requires=myapp.socket

[Service]
Type=simple
ExecStart=/usr/local/bin/myapp
Restart=on-failure
RestartSec=2

# Hardening (minimum)
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true

[Install]
WantedBy=multi-user.target

Bringing it up

sudo systemctl daemon-reload
sudo systemctl enable --now myapp.socket
sudo systemctl status myapp.socket

You don’t have to start the service manually; it can be triggered by the first connection. But if you’d rather keep it “always warm,” you can also enable myapp.service.

Zero-downtime restart runbook

The goal: during the restart, the port stays up, the LB health-check stays green, and we don’t generate user-visible errors.

  1. Preparation
  • Is myapp.socket enabled and active?
  • Is the LB health-check target correct? (e.g., be careful with localhost if a sidecar is involved)
  1. Restart
sudo systemctl restart myapp.service
  1. Verification
  • Is the port still being listened on per ss -lntp | grep 8080?
  • Are there any “listening failed” entries in the application logs?
  • Did the error rate or p95 latency rise during the deploy?

Operational guardrails

In production, I like to standardize the following:

  • StartLimitIntervalSec / StartLimitBurst: keep crash-loop noise under control
  • A health-check endpoint: don’t return 200 before “ready”
  • Restart wave: canary then pilot then full rollout

Wrap-up

systemd socket activation is a powerful mechanism that breaks the “restart equals brief downtime” reflex. When set up correctly, it reduces LB flap during deploy waves and noticeably lowers incident probability. Combine it with graceful shutdown, observability, and staged rollout, and the service restart becomes operationally “routine.”

Paylaş:

Bu yazı faydalı oldu mu?

Yükleniyor...

Bu yazı nasıldı?

ME

Mustafa Erbay

Sistem Mimarisi · Network Uzmanı · Altyapı, Güvenlik ve Yazılım

2006'dan bu yana sistem mimarisi, network, sunucu altyapıları, büyük yapıların kurulumu, yazılım ve sistem güvenliği ekseninde çalışıyorum. Bu blogda sahada karşılığı olan teknik deneyimlerimi paylaşıyorum.

Kişisel Notlar

Bu notlar sadece sizde saklanır. Tarayıcınızda yerel olarak tutulur.

Hazır 0 karakter

Comments

Server-side AI Moderation

Comments are AI-moderated server-side and stored permanently.

?
0/2000

Server-side AI moderation

✉️ Free · No spam · Unsubscribe anytime

Curated digest, hand-picked by me — not the AI

Once a week: the most important post of the week, behind-the-scenes notes, and a "what I actually used this week" section. Less noise, more signal.

  • 📌
    Best of the week Single most-worth-reading post
  • 🔧
    Toolbox notes Real tools I used this week
  • 🧠
    Behind-the-scenes Notes that don't make it to blog

We don't spam. Unsubscribe anytime. · Tracked only by Umami (self-hosted, no Google).

Your Reading Stats

0

Posts Read

0m

Reading Time

0

Day Streak

-

Favorite Category

Related Posts