Skip to the page

Python services in production · mid

Gunicorn workers, memory growth and Celery worker sizing in containers

In a container, Python often sees the node's CPUs, not the pod limit, so default worker counts are too high. Worker memory grows from leaks or one big request, and recycling plus a fix keeps pods alive.

The notes

What you must know

  • multiprocessing.cpu_count() returns the node's CPUs, not the pod limit. 2 x 16 + 1 = 33 workers at about 180 MB is about 6 GB in a 2 GiB pod. Set workers explicitly (--workers or WEB_CONCURRENCY) and scale with pods.
  • Worker classes: sync for a typical Django app behind Nginx; gthread (--threads) if views wait on I/O and memory is tight; ASGI workers (Uvicorn) for async apps.
  • timeout (30 s by default) kills a worker that has not checked in. graceful_timeout (30 s by default) is the time to finish on restart, so terminationGracePeriodSeconds must be longer. max_requests with max_requests_jitter replaces workers now and then to cap slow memory growth.
  • --preload loads the app before forking so memory pages can be shared, but reference counting slowly un-shares them. More workers than CPUs mostly adds memory and context switches.
  • Memory growth: a leak rises steadily with traffic; one big request (lakhs of rows loaded) is a jump that stays, because freed memory is rarely returned to the operating system. Compare tracemalloc snapshots on one worker, use memray, py-spy dump.
  • Typical causes: lru_cache(maxsize=None) on a method keeps every self alive (use a bounded or TTL cache or Redis); list(Order.objects.filter(...)) builds 5 lakh objects (use .iterator(chunk_size=...), values_list and a streamed response, or Celery).
  • Celery prefork concurrency also defaults to the CPUs Python sees (32 on the node, 4 in the pod). Set --concurrency explicitly. Use worker_max_tasks_per_child and worker_max_memory_per_child to replace children after heavy tasks.
  • Task design for pandas: read in chunks (chunksize or a server-side cursor), select only needed columns, write output to S3 not through the result backend, use separate queues and pod sizes for heavy tasks, and set limits from the measured peak memory per child x concurrency.

The red pen

Common traps: mistake → fix

  • Mistake: The 2 x cores + 1 formula is right in containers

    Fix: cores means the node, not the pod.

  • Mistake: Memory that grows must be a leak

    Fix: it can be one big request.

  • Mistake: More workers make the app faster

    Fix: after the CPU count they add memory and switching.

  • Mistake: The same worker settings fit the VM and Kubernetes

    Fix: defaults depend on what Python sees.

Easy to hard

Questions interviewers ask, easy to hard

  1. Gunicorn starts 33 workers in a 2-CPU pod and is OOMKilled. Why?
  2. How do you pick the worker class and count?
  3. Workers grow from 200 MB to 1.5 GB. How do you find the cause?
  4. What does max_requests do and when is it only a bandage?
  5. Celery pods are OOMKilled after the move to Kubernetes. What do you check?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview