In gunicorn (> = 19.2), use max_requests_jitter along with max_requests

What are max_requests?

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.

What's wrong with max_requests alone?

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?"

New features from version 19.2 max_requests_jitter

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

In gunicorn (> = 19.2), use max_requests_jitter along with max_requests
Use "$ in" operator with mongo-go-driver
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Use OpenCV with Python 3 in Window
Use various rabbimq features with pika in python
How to use tkinter with python in pyenv
Use Python in Anaconda environment with VS Code
Use Cursur that closes automatically with sqlite3 in Python
Use mecab-ipadic-neologd with igo-python
Use RTX 3090 with PyTorch
Use config.ini in Python
Use ansible with cygwin
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Mock with pytest
Use DataFrame in Java
Use dates in Python
Use indicator with pd.merge
Use Mean in DataFrame
Use Gentelella with django
Use Valgrind in Python
Use mecab with Python3
Use tensorboard with Chainer
Use DynamoDB with Python
Use pip with MSYS2
Use Python 3.8 with Anaconda
Use pyright with Spacemacs
Use ujson in requests
Use python with docker
Use TypeScript with django-compressor
Use LESS with Django
Use profiler in Python
Use MySQL with Django
Use Enums with SQLAlchemy
Use tensorboard with NNabla
Use GPS with Edison
Use nim with Jupyter
Use "% tensorflow_version 2.x" when using TPU with Tensorflow 2.1.0 in Colaboratory