r/FastAPI • u/JeromeCui • Nov 11 '25
Question FastAPI server with high CPU usage
I have a microservice with FastAPI framework, and built in asynchronous way for concurrency. We have got a serious performance issue since we put our service to production: some instances may got really high CPU usage (>90%) and never fall back. We tried to find the root cause but failed, and we have to add a alarm and kill any instance with that issue after we receive an alarm.
Our service is deployed to AWS ECS, and I have enabled execute command so that I could connect to the container and do some debugging. I tried with py-spy and generated flame graph with suggestions from ChatGPT and Gemini. Still got no idea.
Could you guys give me any advice? I am a developer with 10 years experience, but most are with C++/Java/Golang. I jump in Pyhon early this year and got this huge challenge. I will appreciate your help.


================================
13 Nov Update
I got this issue again:

4
u/latkde Nov 11 '25
This is definitely odd. Your profiles show that at least 1/4 of CPU time is spent just doing async overhead, which is not how that's supposed to work.
Things I'd try to do to locate the problem:
can this pattern be reproduced locally?
does the high CPU usage start immediately when the application launches, or only after certain requests? Does it grow worse over time, suggesting some kind of resource leak?
what are your request latencies, do they seem reasonable?
does the same problem occur when you're running raw uvicorn without using gunicorn as a supervisor?
does the same problem occur with different versions of Python or your dependencies? If there's a bug, even minor versions could make a huge difference.
In my experience, there are three main ways to fuck up async Python applications, though none of them would help explain your observations:
blocking the main thread, e.g. having an
async defpath operation but doing blocking I/O or CPU-bound work within it. Python's async concurrency model is fundamentally different from Go's or Java's. Sometimes, you can schedule blocking operations on a background thread viaasyncio.to_thread(). Some libraries offer both blocking and async variants, and you must take care toawaitthe async functions.leaking resources. Python doesn't have C++ style RAII, you must manage resources via
withstatements. Certain APIs likeasyncio.gather()orasyncio.create_task()are difficult to use in an exception-safe manner (the solution for both isasyncio.TaskGroup). Similarly, combining async+yield can easily lead to broken code.Specifically for FastAPI: there's no good way to initialize application state. Most tutorials use global variables. Using the "lifespan" feature to yield a dict is more correct (as it's the only way to get proper resource management), but also quite underdocumented.