FastAPI
Kubernetes
Performance Optimization
Application Deployment
Cloud Computing

Struggling to get good performance for FastAPI on Kubernetes

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

FastAPI itself is usually not the first reason a Kubernetes deployment feels slow. More often, the bottleneck is worker configuration, CPU throttling, blocking I/O, inefficient autoscaling, or an unmeasured database dependency. The fastest way to improve performance is to profile the whole request path instead of tuning only the framework.

Start by Measuring the Right Layer

Before changing settings, figure out where the latency actually comes from:

  • application code
  • database calls
  • upstream APIs
  • container CPU throttling
  • ingress and network path
  • cold starts or autoscaling lag

If you only watch request duration at the ingress, you can miss whether the app is compute-bound, I/O-bound, or simply underscaled.

Worker Configuration Matters Immediately

FastAPI runs on ASGI, often with Uvicorn or Gunicorn plus Uvicorn workers. Running one worker in a pod with multiple CPU cores often wastes available capacity.

Example command:

bash
1gunicorn app.main:app \
2  -k uvicorn.workers.UvicornWorker \
3  --workers 4 \
4  --bind 0.0.0.0:8000

The right worker count depends on workload type:

  • more workers for CPU-heavy endpoints up to the useful core count
  • fewer workers may be fine for mostly async I/O endpoints

Do not copy a random worker count from a blog post. Load test it.

Avoid Blocking Code Inside Async Endpoints

A common FastAPI performance mistake is writing async def endpoints that still perform blocking work.

Bad example:

python
1from fastapi import FastAPI
2import requests
3
4app = FastAPI()
5
6@app.get("/data")
7async def data():
8    response = requests.get("https://example.com")
9    return response.json()

This blocks the worker thread.

Better:

python
1from fastapi import FastAPI
2import httpx
3
4app = FastAPI()
5
6@app.get("/data")
7async def data():
8    async with httpx.AsyncClient() as client:
9        response = await client.get("https://example.com")
10    return response.json()

The framework cannot save performance if the endpoint uses blocking libraries in the hot path.

Kubernetes CPU Limits Can Cause Throttling

A pod may look underpowered not because FastAPI is slow, but because the container is being CPU-throttled.

Example resource section:

yaml
1resources:
2  requests:
3    cpu: "500m"
4    memory: "512Mi"
5  limits:
6    cpu: "1"
7    memory: "1Gi"

If the app routinely needs more CPU than the limit allows, latency spikes appear even when the node has spare capacity. Monitor CPU throttling metrics rather than assuming more replicas are the only fix.

Autoscaling Needs the Right Signal

Horizontal Pod Autoscaling based only on CPU may be too crude for web APIs. A FastAPI service can become slow because of concurrency saturation, database latency, or event-loop backlog even before CPU looks high.

If possible, scale on signals such as:

  • request rate
  • latency
  • queue depth
  • custom business metrics

CPU-only autoscaling is better than nothing, but it often reacts too late or to the wrong symptom.

Probe and Startup Settings Influence Throughput Too

If readiness probes are too aggressive, pods may churn during startup and reduce available capacity. If startup is expensive, cold pods can hurt performance under burst traffic.

A reasonable deployment should tune:

  • readiness probes
  • liveness probes
  • startup probes if initialization is slow
  • preStop handling for graceful shutdown

Performance is not only about request code. It is also about keeping enough healthy pods in service consistently.

Profile Dependencies, Not Just FastAPI

Many slow FastAPI deployments are really slow database or cache deployments. A fast framework cannot hide:

  • missing indexes
  • slow ORM queries
  • chatty external APIs
  • synchronous logging sinks
  • oversized response payloads

This is why request tracing is more useful than raw pod CPU alone.

Container Image and Networking Still Matter

A bloated image mostly affects startup time, but startup time matters when scaling up. Likewise, ingress configuration, TLS termination, and service mesh overhead can all affect latency.

These are rarely the first place to optimize, but they matter once the application and worker model are already reasonable.

A Practical Tuning Order

A sensible order is:

  1. load test the current deployment
  2. verify worker count and async correctness
  3. inspect database and upstream latency
  4. check CPU throttling and memory pressure
  5. tune autoscaling and probes

This prevents you from tuning Kubernetes YAML before the application path is even understood.

Common Pitfalls

  • Running too few workers for the available CPU.
  • Writing async endpoints that call blocking libraries.
  • Blaming FastAPI when the real bottleneck is the database, cache, or upstream service.
  • Setting tight CPU limits and then wondering why latency spikes under load.
  • Relying only on CPU-based autoscaling without measuring request-level behavior.

Summary

  • FastAPI performance issues on Kubernetes are often deployment and dependency issues, not framework issues.
  • Start with load testing and tracing so you know where the latency comes from.
  • Configure workers appropriately and avoid blocking calls inside async code.
  • Watch CPU throttling, probes, and autoscaling behavior at the pod level.
  • Optimize the whole request path, not only the web framework.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.