Question

FastAPI backend on Railway times out on the first request each morning

Solved · 12 views · asked by kai_makes · edited

First request of the day takes 40 seconds and often times out, after that everything is fast. Users think the app is broken.

What I’ve tried

Increased the timeout in the frontend to 60s. Works but feels bad.

Comment

2 answers

Marked as helpful by the asker
olu_backend · edited

That's a cold start: the platform scales your service to zero when idle and boots it on the first request. Options, cheapest first:

  1. A free uptime monitor pinging /health every 5 minutes keeps it warm. Free, slightly ugly.
  2. Turn off scale-to-zero ("always on") in Railway, costs a few euros a month.
  3. Make startup faster: lazy-load heavy imports (pandas, torch) inside the endpoint that needs them instead of at module level.

Do 3 anyway, it's the real fix.

Comment
jonas_k · edited

If your frontend is on Vercel you can also add a cron there that hits the backend every 10 minutes. Same trick, no extra service.

Comment