A gateway holds 50,000 long-lived client connections. You restart it, and every client reconnects at once. The gateway falls over again. Design the recovery so it does not happen.
Sketch your own answer first, then compare it with the one below.
A strong answer
The clients all noticed the drop at the same moment and all retried at the same moment. 50,000 connection attempts in a second, each needing a handshake and authentication, are far above what the gateway handles in steady state. It fails, they all retry again, and the wave repeats.
Fix it in the client first. Reconnect with exponential backoff and full jitter, so the attempts spread over a window. With a base of 1 second and a cap of 60, the first retry is uniform over 0 to 1 second, the second over 0 to 2, and so on. After a few failures the clients are spread across a minute, and the arrival rate falls from 50,000 a second to under 1,000 a second.
Then protect the server. Cap the rate of new connections it accepts and reject the rest quickly with a hint to retry later. Do not do the expensive work, such as loading user state, until after the connection is admitted. Bring instances back gradually, and keep warm capacity so one restart does not land on a cold fleet.
For planned restarts, drain: tell clients to reconnect at random times over the next several minutes before the gateway goes down.
Add a random offset to any timer shared across clients, such as a token refresh. Shared timers produce the same herd every hour.
Sources
- Marc Brooker (2015). Exponential Backoff And Jitter.
- Betsy Beyer, Chris Jones, Jennifer Petoff, Niall Richard Murphy (2016). Site Reliability Engineering, chapter 22, Addressing Cascading Failures.