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

Articles

Tiếng Việt · English

Zero-downtime deploys

October 14, 2026 · about 7 min read

The article Shutting down gracefully stopped at one point: however carefully you shut down, if only one instance is running, then for the few seconds of a restart nobody answers requests. This article is about how dine removes that gap: let the old and new instances run side by side for a while, and only then shut down the old one.

With one instance there is always a gap

The simplest kind of deploy is: stop the old instance, start the new one. Between the two there is a window, sometimes only a second or two, when nobody is listening on the service's port. Requests that land in that window get an error, and the home page's live data stream is cut. The more often you deploy, the more often users run into it.

Two ways to deploy, over time. One instance: the old instance stops, there is a window where nobody answers, then the new instance runs. Blue/green: the new instance starts while the old one is still running; the gateway switches to the new instance; the old one drains for a few seconds and then stops; there is no gap. One instance old instance nobody answers new instance Blue/green old instance draining new instance gateway switches to the new instance time
One instance: stop, then start, with a gap in between. Blue/green: the new instance runs first, and the old one only stops once no requests are going to it anymore.

The idea: start the new instance first, stop the old one later

Instead of one slot, the order API has two slots to run in, called blue and green, each with its own port. Normally only one slot is occupied. On every deploy:

  1. Start the new instance in the empty slot. The old instance keeps serving as if nothing were happening.
  2. Wait for the new instance to report "I'm ready".
  3. Tell the old instance to step down. New requests move to the new instance.
  4. The old instance finishes its work in progress, then stops. Its slot becomes the empty slot for the next deploy.

The two colors take turns: if the new instance is in blue this time, it goes in green next time. The names do not matter; what matters is that at least one instance is always answering.

Not every part needs it

Running two instances takes more effort, so dine only does it where it is really needed:

PartDuring a deployNeeds blue/green?
Web site (static files)Copy the new version into a new folder, then switch the path to it; the web server never stopsNo, it is already uninterrupted
Background workers (outbox sending, reply handling, timeout)Restart in about a second; unfinished work is still in the database and Kafka, and carries on once they startNo, nobody is waiting on them
The order APIUsers are waiting for an answer; every second with nobody listening is a visible errorYes

The background workers can survive a restart thanks to the earlier articles: messages waiting to be sent are in the outbox, messages being processed will be redelivered by Kafka, and the receiver filters duplicates.

The front has to know which instance is alive

With two instances, someone has to choose which one gets each request. At dine that is the gateway. The gateway knows both of order's ports, and every second asks each port one question: "are you healthy?". Requests go to the first instance that answers "healthy".

The gateway only switches instances at the moment it opens a connection, before the request has been sent. That way a booking request is never sent to both instances.

The old instance must announce it is stepping down before stopping

This is the easiest part to get wrong. If the old instance closes its port the moment it is told to stop, the gateway only finds out on its next check, and in between, requests are still sent to an instance that is no longer listening.

So dine adds one step to shutting down gracefully, ahead of all three existing steps: draining. When told to stop, the old instance:

It stays like that for 3 seconds, longer than one round of the gateway's checks, and only then closes its port. By that point nobody is sending it anything.

A lesson from trying it for real: at first dine only intended the gateway to "knock, and if nobody opens, try the other door". Testing with Docker showed that was not enough. The service's port is held by Docker, not by the program itself, so the program had stepped down while Docker still kept the door open. The gateway saw an open door, walked in, and found nobody there. Only when the old instance actively announced it was stepping down did the errors go away.

Two instances running at once must tolerate each other

For the few seconds of the switch, the old and new instances serve together, using the same database. That sets a few rules:

What if the new instance is broken

If the new instance does not answer "healthy" within 60 seconds, the deploy stops: the new instance is shut down, the old one keeps running, and users notice nothing. This is a valuable side benefit: a broken version can never replace one that is working.

Results and limits

dine tested this with real Docker and the real gateway, firing a steady 1,600 or so requests per second at order while deploying back and forth six times: 129,262 requests, not a single one failed. The home page's live stream dropped exactly one beat each time, when the old instance stopped, and the browser reconnected to the new instance by itself.

What has not been done yet:

In short: you cannot start a new instance without stopping the old one, but you can change the order: start first, stop later. The front has to know which instance is alive, the old instance must announce it is stepping down before closing its port, and the two instances must tolerate running together for a few seconds.

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.