If you specify the max_requests parameter of gunicorn, each worker process automatically restarts the process when it handles max_requests
number of requests. It will start up.
This avoids memory leaks and prevents server resource exhaustion.
Since gunicorn allocates requests to each worker process almost evenly, there are cases where all worker processes reach max_requests
at the same time and restart.
In that case, there is no worker who can handle the request for a moment, so from the user's point of view, it is possible that "Oh, this site suddenly became extremely heavy?"
Therefore, from version 19.2, the max_requests_jitter parameter has been added.
If you read the code of the Worker
class, max_requests
is set in __init__
as follows. [^ 1]
jitter = randint(0, cfg.max_requests_jitter)
self.max_requests = cfg.max_requests + jitter or MAXSIZE
That is, if you set max_requests_jitter
to an integer greater than 0, the max_requests
of each worker process will be incremented by a random value from 0 to max_requests_jitter
. This will cause the max_requests
of each worker process to have different values, and the restart timing will be different to avoid the above problem.
This feature was proposed by a former Reddit engineer alienth. It seems that the code was originally used by Reddit. https://github.com/benoitc/gunicorn/pull/862
According to alienth's comment, "Right now we're using a max_requests of 500, and a max_requests_jitter of 200 so that restarts" It says "are highly randomized.", So if you set max_requests = 500`` max_requests_jitter = 200
, it will be a nice variation.
Recommended Posts