Backend · FastAPI · Python

    My FastAPI Production Backend Checklist

    The checklist Anup Ojha uses before shipping a FastAPI backend: project structure, auth, database, performance, testing, docs and deployment.

    Anup Ojha

    /5 min read

    I've shipped a fair number of FastAPI backends by now. Most of them came out of my time at Perimattic in Mumbai, where I worked on three production AI platforms (the CareerMaker.ai backend among them) with PostgreSQL underneath. FastAPI gets you to a working endpoint in about ten minutes, and honestly that's what makes it a little dangerous. The demo works, everyone's happy, and then the first week of real traffic shows you everything you skipped.

    So before anything I build goes live, I sit down with this list. It isn't fancy. It's mostly things I've seen hurt when they were missing.

    Project structure

    I split routers by domain (users, billing, documents, whatever the product has) from the very first commit. Every project I've seen that started with one big main.py still had one big main.py a year later, because nobody wants to be the person who breaks it apart.

    Settings come from environment variables through Pydantic settings. Nothing hard-coded, not even the "temporary" URLs, because temporary has a habit of reaching production.

    I also keep a thin service layer between the routes and the database. Routes deal with HTTP, services deal with the business rules. When the logic lives inside the route function you can only test it through HTTP calls, which is slow and fiddly, and so people quietly stop writing the tests.

    Auth and permissions

    • Short-lived JWT access tokens, with refresh tokens for staying logged in. A leaked token that dies in a few minutes is a much smaller problem than one that lives for a month.
    • Role checks go into FastAPI dependencies. I don't like if user.role == "admin" sprinkled across forty route functions, because one day someone adds the forty-first and forgets.
    • Passwords hashed with bcrypt or argon2. This shouldn't need saying, and yet.
    • Rate limiting on login, password reset and OTP endpoints, since those are the first things bots find.

    One of my FastAPI CRUD services used exactly this setup: modular routers, JWT auth, role-based access, Swagger docs and a test suite. Nothing exotic. It just holds up.

    The database

    PostgreSQL, with Alembic migrations. I never edit a production schema by hand. If a change isn't in a migration, then as far as the next deploy is concerned it doesn't exist.

    Put indexes on the columns you filter, sort and join on. Watch for N+1 queries too. They're easy to write with an ORM and invisible until a list endpoint returns two hundred rows and fires two hundred and one queries to do it, so eager-load what the endpoint actually needs. And size the connection pool for the number of workers you run, otherwise you get odd timeouts that only show up under load.

    On that CRUD service, indexing plus caching the hot queries made the API about 20% faster. I didn't change a line of business logic to get there. That's usually where the cheap wins are hiding.

    Performance

    Async isn't free speed. I use async def where the I/O really is async (an async database driver, an HTTP client like httpx). Call a blocking library inside an async endpoint and you've blocked the event loop, which is worse than plain sync code would have been.

    Anything slow goes to a background job: emails, document processing and above all AI model calls, which can take several seconds and sometimes just fail. On AI products this matters more than almost anywhere else, because a user staring at a spinner for ten seconds assumes the app is broken.

    Cache expensive reads that don't change often. Paginate every list endpoint, including the ones that "will only ever have ten rows".

    When things go wrong

    • One error format across the whole API, with a message a frontend developer can actually use, and no stack traces in responses.
    • A timeout on every outbound HTTP call. Plenty of clients default to waiting more or less forever, and one slow third-party service can tie up every worker you have.
    • Retries, but only on calls that are safe to repeat.
    • A /health endpoint for the load balancer.
    • Structured logs with a request id, so when someone sends a screenshot of an error you can find the exact request behind it.

    Tests and docs

    Every endpoint gets tests, and I care most about the failure cases: wrong role, expired token, missing field. Happy paths usually work. It's the 403 that turns out to be a 200 that bites you.

    Tests run against a seeded test database, never a copy of production data. I also read through the Swagger docs FastAPI generates as if I were the frontend developer seeing them for the first time. Add examples, name things clearly. It saves a lot of messages asking what some field means.

    Deployment

    The Docker image runs as a non-root user. A CI pipeline runs the tests before anything deploys. Secrets live in the hosting platform's secret manager and never in the repo (check your git history as well, not only the current files).

    Backups and uptime alerts get set up before launch. Set them up after your first outage and you've learnt the lesson the expensive way. If hosting cost is the worry, I've written about cutting hosting and deployment costs on small projects, and there's a low-cost deployment service if you'd rather hand it over.

    If you only have a day

    Sometimes there's no time for the whole list. In that case I'd do migrations, auth checks in dependencies, timeouts on outbound calls and backups, in that order. Everything else can be tightened in the first few weeks. Those are the ones that get painful once real users and real data are in the system.

    If you want a backend built properly from the start, or an existing FastAPI app made faster and steadier, have a look at my backend and API development service or book a free consultation.

    Frequently asked questions

    01Is FastAPI good enough for a production backend?

    Yes. I've used it for production AI platforms with PostgreSQL behind them. The framework isn't the weak point; what usually goes wrong is the stuff around it, like missing migrations, no timeouts on outbound calls and no backups. Get those right and FastAPI holds up well.

    02Should every FastAPI endpoint be async?

    No. Use async def only when the I/O underneath is actually async, such as an async database driver or httpx. If you call a blocking library inside an async endpoint you block the event loop, which is slower than plain sync code. Slow work like emails or AI calls is better moved to a background job.

    03How can I make a slow FastAPI API faster?

    Start with the database. Add indexes on the columns you filter and join on, remove N+1 queries and cache expensive reads that rarely change. On one of my CRUD services, indexing and caching alone made the API about 20% faster without touching business logic.

    04How should I handle roles and permissions in FastAPI?

    Put role checks in FastAPI dependencies instead of writing if statements inside each route. That way every protected route declares what it needs, and nobody can forget the check on a new endpoint. Pair it with short-lived JWT access tokens and refresh tokens.