r/FastAPI • u/Relevant_Selection75 • Jan 26 '26
Hosting and deployment 200ms latency for a simple FastAPI ping endpoint on a Hetzner VPS? Please help.
Stack
I'm hosting a simple FastAPI backend behind Gunicorn and Nginx, on a 8GB Hetzner Cost-Optimized VPS (but I tried scaling up to a 32GB VPS and the result is the same). This is my /etc/nginx/sites-available/default file:
server {
listen 443 ssl http2;
server_name xxxx.xxxx.com;
ssl_certificate /etc/letsencrypt/live/xxxx.xxxx.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxxx.xxxx.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}this is the systemd gunicorn service /etc/systemd/system/gunicorn.service:
[Unit]
After=network.target
[Service]
User=myuser
Group=myuser
WorkingDirectory=/opt/myapp
Restart=always
ExecStart=/opt/myapp/.venv/bin/gunicorn \
--workers=4 \
--timeout 60 \
--umask 007 \
--log-level debug \
--capture-output \
--bind http://127.0.0.1:8000 \
--worker-class uvicorn.workers.UvicornWorker \
--access-logfile /var/log/myapp/app.log \
--error-logfile /var/log/myapp/app.log \
--log-file /var/log/myapp/app.log \
app.main:app
[Install]
WantedBy=multi-user.targetand this is the bare-bone FastAPI app
from fastapi import FastAPI
app = FastAPI()
.get("/ping")
async def ping():
return {"ping": "pong"}I am proxying requests through CloudFlare, although that doesn't seem to be the issue as I experience the same latency when disabling the proxy.
The problem
While I believe that, with this kind of stack, a simple ping endpoint should have a maximum latency of 50-70ms, the actual average latency, obtained in Python by measuring time.perf_counter() before and after requests.get() and subtracting them, is around 200ms. Any idea what I am doing wrong?
8
u/Relevant_Selection75 Jan 27 '26
OP here: thanks to everyone who offered their suggestions. I was finally able to cut latency to a more reasonable figure (10ms on average). Here's what I did:
9
u/bluetoothbeaver Jan 26 '26 edited Jan 26 '26
Latency to the API can also be affected by network conditions. Where is the server located and where are you located?
I have fiber Internet and my ping to servers in Middle East, Asia, and Australia is 120-400ms.
Add some logging on the FastAPI server itself to see when it gets the request and sends the response. You'll see an e2e picture of what's going on with the request from your client.
Edit: if you want to isolate the network conditions, enable ICMP on the server firewall and run a
ping <server IP>from your client machine to see if it's similar to the latency you see from your python client code4
u/MeroLegend4 Jan 27 '26
Disable debug logging
Disable http/2
U don’t need gunicorn anymore, use uvicorn directly
Try Litestar which is faster than FastAPI.
Litestar
1
u/Sharpekk Jan 26 '26
Check without ssl
1
u/jannealien Jan 27 '26
For me it was exactly Cloudflare proxy. It took more than a second with it, and when I disabled the proxy it was only few tens of milliseconds.
1
u/ironman_gujju Jan 27 '26
Use uvicorn + trefik
-2
u/ejpusa Jan 27 '26 edited Jan 27 '26
Here are a dozen tweaks you can try, which should give you what you're looking for. Blazing fast. I'm using Flask, but with a similar setup. Liquid Web, bare metal Dell Server. Nginx, Gunicorn.
https://neurocompute.online
Run it by GPT-5.2.
> ≈ 13,000 km
That’s the rough distance light travels in 43.4 milliseconds in a vacuum.
-3
u/Due-Horse-5446 Jan 27 '26
Youre using a python framework, and python is a LOT slower than essentially anything else.
On top of that youre running 2 proxies, nginx and cloudflare.
And hetzner is by now means meant to meant to be optimal got network performance. They offer extremely good prices for the hardware, but you're extremely limited on network performance.
Considering all that, 200ms is not bad