APIs with FastAPI · Chapter 23 of 26
Deploying
By the end of this chapter you can say what happens when your API runs on a server for real: how uvicorn's workers share (nothing), what a health check and a readiness check each promise, how a server stops without dropping requests, how one image runs in every environment with settings from outside, and how to write the container it runs in. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Production
- 4 tasks
- FastAPI
- In the all labs plan
The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.
Why you need this
The festive rush starts in three weeks. Devika has signed the two biggest merchants, and one of them asked a question no test answers: "What happens to my bookings when you deploy?" Until now the API has run as one uvicorn process in the lab's terminal. In production it runs as several processes on several machines, behind a load balancer that sends each request to one of them, and a platform that starts, checks, stops and replaces them on its own.
Aman lists what that changes. "A dictionary in memory is now one of eight dictionaries. The platform asks /health every ten seconds and restarts anything that does not answer. Deploys stop processes in the middle of requests. And the same code must run on your laptop, in staging and in production, with different databases and secrets, without editing a line."
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
probes.py: health and readiness
The platform that runs DakiyaGo asks every instance two questions. Make an app reading its database from the environment variable DATABASE_URL (a SQLAlchemy URL), with GET /health answering {"status": "ok"} without touching the database, and GET /ready answering 200 {"status": "ready"} when a query on the database works right now, or 503 {"status": "not ready"} when it does not (no error text in the answer).
The check makes a database, asks both, drops the database and asks again (within 5 seconds), then starts your app with a database that does not exist: it must still start, and say so on /ready.
- Task 2
settings.py: one image, every environment
Make a Settings class with pydantic-settings, read once at startup: app_env (development, staging or production; default development, from APP_ENV), database_url (required, from DATABASE_URL) and log_level (debug, info or warning; default info, from LOG_LEVEL). A wrong or missing setting stops the app at startup.
In production /docs, /redoc and /openapi.json answer 404; elsewhere they are on. GET /config answers {"env": ..., "log_level": ..., "docs": true or false}, and never the database URL.
- Task 3
Dockerfile: a container for the API
Write the Dockerfile for DakiyaGo's API, an app in main.py with its packages listed in requirements.txt. It builds FROM the slim Python 3.13 image, works in its own folder, copies requirements.txt and installs it (pip with --no-cache-dir) before copying the rest of the code, switches to a user that is not root after installing, exposes port 8000, and starts the API with a CMD in the JSON list form: fastapi run, or uvicorn listening on 0.0.0.0 port 8000, never reloading.
The lab has no Docker, so the check reads your Dockerfile as text, instruction by instruction.
- Task 4 · interview question
Interview: workers.py, a rate limit for four workers
Chapter 20's rate limiter counted in memory. In production, with four workers, merchants got four times the limit. Make an app with GET /rates answering the rate card (/workspace/data/rate_card.json) to a request with an X-Api-Key header (any key; without one, 401), at most 5 requests per key in any 60 seconds; the next answers 429 with a Retry-After header in seconds.
Keep the counts in the Postgres database at DATABASE_URL (make your table when the app starts if it is not there). The check runs two servers of your app at once and spreads one key's requests over both. The interviewer will ask: why does the in-memory rate limiter break with four workers?
What you will learn
- Workers: what --workers 4 starts, and what they share.
- Health and readiness: two questions, two endpoints.
- Graceful shutdown: finishing what was started before stopping.
- Configuration per environment with pydantic-settings.
- A container for the API, and what fastapi run does.
More in Production
Build "Deploying" yourself
DakiyaGo, a Jaipur courier startup, takes bookings on WhatsApp. Merchants want an API, and you build it. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.