dine. a kitchen run by sagas Book a table
Log in with Google (coming soon)

Articles

Tiếng Việt · English

Shutting down gracefully

October 14, 2026 · about 7 min read

Every deploy of a new version means shutting down the old one. At dine, that happens while orders are still running. This article is about what happens when you shut down carelessly, what "shutting down gracefully" means, when it helps and when it does not.

What happens if you shut down carelessly

Picture the service being stopped instantly, like pulling the plug, right while it is busy:

None of these leaves the system permanently wrong (the end of the article explains why), but each one is extra cleanup, and guests can see it. The more often you deploy, the more often this happens.

Two ways to stop a program

On Linux, you stop a program by sending it a signal. Two signals matter:

The two usually come together: docker stop sends SIGTERM, waits 10 seconds, and if the program has not exited by then, sends SIGKILL. So shutting down gracefully really means finishing the cleanup within the time you are given.

What shutting down gracefully means

On receiving SIGTERM, the service does three things in order:

  1. Stop taking new work: no new requests, no new messages fetched.
  2. Finish the work in progress: complete the requests that are running, finish processing the messages it is holding.
  3. Close connections, then exit: flush the remaining logs, close the connections to the database and Kafka.

When it helps, and when it does not

A graceful shutdown is just a chance, given in advance, to clean up. Sometimes you get that chance, sometimes you do not.

It helpsIt does not help
Deploying a new versionkill -9 (SIGKILL)
A planned restart, machine maintenanceOut of memory, the operating system kills the process itself
Scaling down the number of running instancesPower loss, the server dies, the machine's network is cut off completely
Pressing Ctrl+C during a test runThe program is hung and no longer reacts to signals

The left column is the times you choose to shut down, which is also most of the shutdowns in practice. The right column is incidents: no warning, no line of code gets to run.

Done, but still not effective

This is the part that is often missed: you wrote graceful shutdown code, but it still does not do its job, for these reasons.

In Go: context

Go already ships almost everything you need, so there is not much to write:

When you don't get to shut down gracefully

For the "does not help" column above, there is no way around it. So dine is designed to still be correct in that case, and almost every mechanism from the earlier articles plays a part:

Dying suddenly while…What saves it
Writing an order to the database, not yet committedThe database rolls back the unfinished transaction itself, as if nothing happened
Claiming a message in the outbox to sendAfter 30 seconds the stuck message is handed back to another worker (Transactional outbox)
Having processed a message, before telling KafkaKafka redelivers that message, and the receiver filters the duplicate (Idempotency)
Holding a lock on a stock itemThe lock expires by itself after 5 seconds (Two orders fighting over the last ingredients)
A service dies mid-processing and never repliesAfter 5 seconds order cancels and undoes (Why 5 seconds cancels an order)

So what is a graceful shutdown for, if the system is correct without it? To need rescuing less often: fewer messages to redeliver, fewer locks to wait out, fewer orders cancelled for nothing. And it works exactly when shutdowns happen most: during deploys.

In short: design for the worst case (the plug is pulled), optimize for the common case (you get a warning). A graceful shutdown makes the shutdowns you choose go smoothly, but the system's correctness must not depend on it.

See it for yourself

Spotted a mistake, or have a question? Write to [email protected].

dine is a personal project about distributed systems, written in Go: orchestrated sagas, transactional outbox, Kafka. More articles.