Tiếng Việt · English
Zero-downtime deploys
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.
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:
- Start the new instance in the empty slot. The old instance keeps serving as if nothing were happening.
- Wait for the new instance to report "I'm ready".
- Tell the old instance to step down. New requests move to the new instance.
- 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:
| Part | During a deploy | Needs blue/green? |
|---|---|---|
| Web site (static files) | Copy the new version into a new folder, then switch the path to it; the web server never stops | No, 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 start | No, nobody is waiting on them |
The order API | Users are waiting for an answer; every second with nobody listening is a visible error | Yes |
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:
- still answers every request as usual,
- but answers "no" to the question "are you healthy?", so the gateway stops sending it new requests,
- and closes the connection after every response, so no connection is left hanging on to it.
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:
- Database schema changes take two steps. To drop a column: this deploy stops using it, the next deploy deletes it. Delete it right away and the old instance, still running for a few seconds, will fail.
- Don't keep important data in one instance's memory. Everything lives in the database, so whichever instance a request reaches, it sees the same data.
- Messages may arrive twice, exactly as in the article Idempotency. The system already tolerated that, so two instances running side by side do not introduce anything new to get wrong.
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:
- The gateway and the other services still run a single instance, so restarting them still leaves a gap. The same approach can be applied when needed; the gateway would need one more layer in front of it.
- The very first switch to the new approach: the old instance did not yet know how to drain, so there was one very short beat of errors. Only once.
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
-
Home page
The live stream of orders. When
orderis deployed, the stream drops one beat and reconnects by itself, with no error. -
The path of an order
On the path chart, the process name
order-api-blueororder-api-greentells you which instance the order went through.
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.