Skip to content

Commit cbe3d9a

Browse files
authored
guides/features: add document for how to gracefully shutdown server (grpc#1388)
1 parent 656d44d commit cbe3d9a

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Graceful Shutdown
3+
description: >-
4+
Explains how to gracefully shut down a gRPC server to avoid causing RPC
5+
failures for connected clients.
6+
---
7+
8+
### Overview
9+
10+
gRPC servers often need to shut down gracefully, ensuring that in-flight RPCs
11+
are completed within a reasonable time frame and new RPCs are no longer
12+
accepted. The "Graceful shutdown function" facilitates this process, allowing
13+
the server to transition smoothly without abruptly terminating active
14+
connections.
15+
16+
When the "Graceful shutdown function" is called, the server immediately
17+
notifies all clients to stop sending new RPCs. Then after the clients have
18+
received that notification, the server will stop accepting new RPCs. In-flight
19+
RPCs are allowed to continue until they complete or a specified deadline is
20+
reached. Once all active RPCs finish or the deadline expires, the server shuts
21+
down completely.
22+
23+
Because graceful shutdown helps prevent clients from encountering RPC failures,
24+
it should be used if possible. However, gRPC also provides a forceful shutdown
25+
mechanism which will immediately cause the server to stop serving and close all
26+
connections, which results in the failure of any in-flight RPCs.
27+
28+
### How to do Graceful Server Shutdown
29+
30+
The exact implementation of the "Graceful shutdown function" varies depending on
31+
the programming language you are using. However, the general pattern
32+
involves:
33+
34+
- Initiating the graceful shutdown process by calling the "Graceful shutdown
35+
function" on your gRPC server object. This function blocks until all
36+
currently running RPCs complete. This ensures that in-flight requests are
37+
allowed to finish processing.
38+
- Specify a timeout period to limit the time allowed for in-progress RPCs to
39+
finish. It's crucial to separately call the "Forceful shutdown function" on
40+
the server object using a timer mechanism (depending on your language) to
41+
trigger a forceful shutdown after a predefined duration. This
42+
acts as a safety net, ensuring that the server eventually shuts down even if
43+
some in-flight RPCs don't complete within a reasonable time frame. This
44+
prevents indefinite blocking.
45+
46+
The following shows the sequence of events that occur during the graceful
47+
shutdown process. When a server's graceful shutdown is invoked, in-flight RPCs
48+
continue to process, but new RPCs are rejected. If some in-flight RPCs are not
49+
finished in time, the server is forcefully shut down.
50+
```mermaid
51+
sequenceDiagram
52+
Client->>Server: New RPC Request 1
53+
Client->>Server: New RPC Request 2
54+
Server-->>Server: Graceful Shutdown Invoked
55+
Server->>Client: Continues Processing In-Flight RPCs
56+
Client->>Client: Detects server shutdown and finds other servers if available
57+
alt RPCs complete within timeout
58+
Server->>Client: Completes RPC 1
59+
Server->>Client: Completes RPC 2
60+
Server-->>Server: Graceful Shutdown Complete
61+
else Timeout reached
62+
Server->>Client: Forceful Shutdown Invoked, terminating pending RPCs
63+
Server-->>Server: Forceful Shutdown Complete
64+
end
65+
```
66+
The following is a state based view
67+
```mermaid
68+
stateDiagram-v2
69+
[*] --> SERVING : Server Started
70+
SERVING --> GRACEFUL_SHUTDOWN : Graceful Shutdown Called (with Timeout)
71+
GRACEFUL_SHUTDOWN --> TERMINATED : In-Flight RPCs Completed (Before Timeout)
72+
GRACEFUL_SHUTDOWN --> TIMER_EXPIRED : Timeout Reached
73+
TIMER_EXPIRED --> TERMINATED : Forceful Shutdown Called
74+
```
75+
76+
### Language Support
77+
78+
| Language | Example |
79+
|----------|------------------|
80+
| C++ | |
81+
| Go | [Go Example] |
82+
| Java | [Java Example] |
83+
| Python | |
84+
85+
86+
[Go example]: https://github.com/grpc/grpc-go/tree/master/examples/features/gracefulstop
87+
[Java example]: https://github.com/grpc/grpc-java/tree/master/examples/example-hostname/src/main/java/io/grpc/examples/hostname

0 commit comments

Comments
 (0)