Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue #1391] Set the gunicorn workers based on CPU count #1476

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

chouinar
Copy link
Collaborator

@chouinar chouinar commented Mar 13, 2024

Summary

Fixes #1391

Time to review: 3 mins

Changes proposed

Set the number of gunicorn workers to 2*cpu count + 1

Context for reviewers

We temporarily set the number of workers to exactly 2 while getting the API running initially. This shouldn't be needed anymore, and would mean we are potentially underutilitizing our API instances. This number follows the recommended values:
See: https://docs.gunicorn.org/en/stable/design.html#how-many-workers
also: https://docs.gunicorn.org/en/latest/configure.html#configuration-file

Currently, we run 2 workers always. With our current API configuration, this should change to 5. We actually log the sched_getaffinity value when the process starts up. Pulling from the prod logs, this gives (trimming extra info for brevity):

{
    "name": "src.logging.config",
    "levelname": "INFO",
    "levelno": 20,
    "pathname": "/api/src/logging/config.py",
    "funcName": "log_program_info",
    "cpu_count": 2,
    "cpu_usable": 2,
    "message": "start src: CPython 3.12.2 Linux, hostname ip-10-3-0-56.ec2.internal, pid 7, user 1001(runner)"
}

The cpu_usable metric is just the len(sched_getaffinity(0)) value: https://github.com/HHS/simpler-grants-gov/blob/main/api/src/logging/config.py#L140

Additional information

To test this, you need to adjust the docker-compose file to run the following command: ["poetry", "run", "gunicorn", "src.app:create_app()"] That'll create several workers, which run uneventfully locally, but produce a lot of logs.

@@ -21,9 +21,6 @@
# We use 'os.sched_getaffinity(pid)' not 'os.cpu_count()' because it returns only allowable CPUs.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamesbursa I was curious on your opinion of how we use gunicorn compared to past projects.

Past projects we setup gunicorn in our main function, while here we tell gunicorn to run our app via a specific create_app function. This means all of the code that runs in that create_app function is run extra times for each worker. That means code like logging initialization duplicates. This doesn't seem to cause any issues at the moment, but does make the initialization a bit confusing.

Copy link
Collaborator

@coilysiren coilysiren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense to me 👍🏼 I wish I knew more about this stuff

do you think threads needs a similar treatment?

@chouinar
Copy link
Collaborator Author

makes sense to me 👍🏼 I wish I knew more about this stuff

do you think threads needs a similar treatment?

I've looked around a bit regarding how to set the number of threads, and the recommendation is largely "a few". We've set it as 4 on a few projects, and I can't recall any issues occurring. Note that threads is per-worker, and I think the scaling would be based on the types of queries you receive (more threads is better when doing a lot of DB queries and waiting around I believe?).

@chouinar chouinar merged commit c08ef65 into main Mar 14, 2024
9 checks passed
@chouinar chouinar deleted the chouinar/1391-gunicorn-cpu-count branch March 14, 2024 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Task]: Modify gunicorn config to use recommended number of workers and threads
2 participants