Backend mem usage fix - use fixed MOTOR_MAX_WORKERS + switch to gunicorn (#1468)

Refactors backend deployment to:
- Use MOTOR_MAX_WORKERS (defaulting to 1) to reduce threads used by
mongodb connections
- Also sets backend workers to 1 by default to reduce default memory
usage
- Switches to gunicorn with uvloop worker for production use instead of
uvicorn (as recommended by uvicorn)

Lower thread count should address memory leak/increased usage, which
resulted in 5x thread x cpus x workers, eg. potentially 20 or 40 threads
just for mongodb connections. Lower default number of workers should
make it easier to scale backend with HPA if additional capacity.

Fixes #1467
This commit is contained in:
Ilya Kreymer 2024-01-16 15:32:42 -08:00 committed by GitHub
parent 032859f361
commit 90197b2a85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 16 deletions

View File

@ -8,6 +8,4 @@ RUN pip install -r requirements.txt
ADD btrixcloud/ /app/btrixcloud/ ADD btrixcloud/ /app/btrixcloud/
CMD uvicorn btrixcloud.main:app_root --host 0.0.0.0 --access-log --log-level info
EXPOSE 8000 EXPOSE 8000

View File

@ -1,4 +1,5 @@
uvicorn gunicorn
uvicorn[standard]
fastapi==0.103.2 fastapi==0.103.2
motor==3.3.1 motor==3.3.1
passlib passlib

View File

@ -53,6 +53,18 @@ spec:
- name: api - name: api
image: {{ .Values.backend_image }} image: {{ .Values.backend_image }}
imagePullPolicy: {{ .Values.backend_pull_policy }} imagePullPolicy: {{ .Values.backend_pull_policy }}
command:
- gunicorn
- btrixcloud.main:app_root
- --bind
- "0.0.0.0:8000"
- --access-logfile
- "-"
- --workers
- "{{ .Values.backend_workers | default 1 }}"
- --worker-class
- uvicorn.workers.UvicornWorker
envFrom: envFrom:
- configMapRef: - configMapRef:
name: backend-env-config name: backend-env-config
@ -62,8 +74,8 @@ spec:
name: mongo-auth name: mongo-auth
env: env:
- name: WEB_CONCURRENCY - name: MOTOR_MAX_WORKERS
value: "{{ .Values.backend_workers | default 4 }}" value: "{{ .Values.backend_mongodb_workers | default 1 }}"
volumeMounts: volumeMounts:
- name: ops-configs - name: ops-configs
@ -114,15 +126,16 @@ spec:
image: {{ .Values.backend_image }} image: {{ .Values.backend_image }}
imagePullPolicy: {{ .Values.backend_pull_policy }} imagePullPolicy: {{ .Values.backend_pull_policy }}
command: command:
- uvicorn - gunicorn
- btrixcloud.main_op:app_root - btrixcloud.main_op:app_root
- --host - --bind
- 0.0.0.0 - "0.0.0.0:{{ .Values.opPort }}"
- --port - --access-logfile
- "{{ .Values.opPort }}" - "-"
- --access-log - --workers
- --log-level - "{{ .Values.backend_workers | default 1 }}"
- info - --worker-class
- uvicorn.workers.UvicornWorker
envFrom: envFrom:
- configMapRef: - configMapRef:
@ -133,8 +146,8 @@ spec:
name: mongo-auth name: mongo-auth
env: env:
- name: WEB_CONCURRENCY - name: MOTOR_MAX_WORKERS
value: "{{ .Values.operator_workers | default 1 }}" value: "{{ .Values.backend_mongodb_workers | default 1 }}"
volumeMounts: volumeMounts:
- name: config-volume - name: config-volume

View File

@ -102,7 +102,7 @@ backend_password_secret: "PASSWORD!"
backend_num_replicas: 1 backend_num_replicas: 1
# number of workers per pod # number of workers per pod
backend_workers: 2 backend_workers: 1
backend_cpu: "25m" backend_cpu: "25m"