Latency and Throughput of FastAPI Services Under Uvicorn: Effects of Handler Type, JSON Serialization and Worker Count
Python web services are often tuned by habit: use async def, switch to a faster JSON library, add worker processes. This report measures how much each of those choices matters for a small FastAPI service served by Uvicorn. Four factors were crossed (sync or async handler, 70-byte or 90 KB response, standard-library json or orjson, and 1, 2 or 4 Uvicorn workers), plus three ways of waiting 5 ms, for 135 closed-loop measurements (five repeats, 64 connections, 10 s windows) on one 2-vCPU Linux virtual machine. None of the 135 measurements recorded an error. For the 90 KB response, orjson gave 3.8 to 5.0 times the throughput of json and about a quarter of the p99 latency. For the 70-byte response the gain was about 5 to 10 percent. For small responses, async def handlers were about 1.4 to 1.5 times faster than def handlers (1.3 in one noisy case). Moving from one to two workers raised throughput by 1.1 to 1.3 times, and four workers gave no gain over two on this two-CPU machine. Calling time.sleep inside an async def handler cut throughput to 180 requests per second, 14 to 23 times lower than the other two ways of waiting. The results come from one machine, with the load generator and server sharing its two CPUs, so they show relative effects and will not transfer exactly to other hardware.
Authors
- Quenasha Byrd
Publication Details
- Journal
- Zenodo (CERN European Organization for Nuclear Research)
- Published
- 2026-11-03
- DOI
- https://doi.org/10.5281/zenodo.23899504
- Primary Topic
- Mobile and Web Applications
- Type
- preprint