|
1 | | -# Limit CPU usage |
| 1 | +# Limit CPU and memory usage |
2 | 2 |
|
3 | | -In order to avoid high CPU/RAM usage, it is possible to set the number of workers |
4 | | -using the environment variable `GUNICORN_NUM_WORKERS`. |
| 3 | +In the recommended docker-based setup, Gramps Web uses [Gunicorn](https://gunicorn.org/) to serve the |
| 4 | +backend and [Celery](https://docs.celeryq.dev) for background tasks. In both cases, several worker |
| 5 | +processes can be run in parallel, which makes the application more responsive from a user perspective. |
| 6 | +However, increasing the number of workers also increase the amount of RAM used (even when the application is idle) |
| 7 | +and allowing requests to be processed in parallel can lead to high CPU usage (in particular when many users |
| 8 | +are using the application simultaneously). Both Gunicorn and Celery allow to limit the number of parallel workers. |
5 | 9 |
|
6 | | -Here, we will take a number of workers = 2. Adjust it according to your needs. |
7 | | -It may be a good idea to check the CPU/Threads available before choosing the value: |
| 10 | +## Get information about your system |
8 | 11 |
|
9 | | -> lscpu | grep CPU |
| 12 | +On Linux, you can check the number of cores available on your system with the following command: |
10 | 13 |
|
11 | | -The easiest way is to declare the variable in the `docker-compose.yml` file, |
12 | | -under the "environment". |
| 14 | +```bash |
| 15 | +lscpu | grep CPU |
| 16 | +``` |
13 | 17 |
|
| 18 | +To see how much memory and swap space you have available, use |
| 19 | + |
| 20 | +```bash |
| 21 | +free -h |
14 | 22 | ``` |
15 | | -version: "3.7" |
| 23 | + |
| 24 | + |
| 25 | +## Limiting the number of Gunicorn workers |
| 26 | + |
| 27 | +The easiest way to set the number of Gunicorn workers when using the default Gramps Web |
| 28 | +docker image is to set the environment variable `GUNICORN_NUM_WORKERS`, e.g. by declaring it |
| 29 | +in the `docker-compose.yml` file, |
| 30 | +under the "environment". |
| 31 | + |
| 32 | +```yaml |
16 | 33 | services: |
17 | 34 | grampsweb: |
18 | 35 | environment: |
19 | 36 | GUNICORN_NUM_WORKERS: 2 |
20 | 37 | ``` |
21 | 38 |
|
22 | | -Other ways are possible, for example by storing the variable in a file, |
23 | | -and calling it in the startup command: |
| 39 | +See [the Gunicorn documentation](https://docs.gunicorn.org/en/stable/design.html#how-many-workers) to decide |
| 40 | +about the ideal number of workers. |
| 41 | +
|
| 42 | +
|
| 43 | +
|
| 44 | +## Limiting the number of Celery workers |
| 45 | +
|
| 46 | +To set the number of Celery workers, adapt the `concurrency` setting in the Docker compose file: |
| 47 | + |
| 48 | +```yaml |
| 49 | + grampsweb_celery: |
| 50 | + command: celery -A gramps_webapi.celery worker --loglevel=INFO --concurrency=2 |
| 51 | +``` |
24 | 52 |
|
25 | | -> docker compose --env-file ./env up |
| 53 | +See [the Celery documentation](https://docs.celeryq.dev/en/stable/userguide/workers.html#concurrency) to decide |
| 54 | +about the ideal number of workers. |
26 | 55 |
|
27 | | -In this case, the `env` file would contain a single line: GUNICORN_NUM_WORKERS=2 |
| 56 | +!!! info |
| 57 | + If the `concurrency` flag is omitted (which was the case in the Gramps Web documentation until v2.5.0), it |
| 58 | + defaults to the number of CPU cores available on the system, which might consume a substantial amount of memory. |
0 commit comments