Question

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

Solved · 399 viewsasked by kai_makes

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

The deploy logs for this morning's first request:

What I’ve tried

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

Comment

3 answers

Marked as helpful by the asker
olu_backend

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
Moved the pandas import into the export endpoint, which is the only place that uses it. Plus the uptime ping. Nobody has waited this week. kai_makes
diego_mx

Look at the second line of your log: the start command runs pip install every time the container boots. So every cold start downloads and installs pandas and numpy before uvicorn even starts. That's most of your 40 seconds.

Install at build time, start with only the server:

# railway.toml
[build]
buildCommand = "pip install -r requirements.txt"

[deploy]
startCommand = "uvicorn main:app --host 0.0.0.0 --port $PORT"

ChatGPT writes that combined start command a lot because it works on the first try. It just works slowly, forever.

Comment
Oh no. Yes, that was in the start command. Cold start is 6 seconds now, even without the ping. kai_makes
Good eye, I read straight past that line. olu_backend
jonas_k

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