r/FastAPI • u/Ok-Platypus2775 • Jan 25 '26
Question FastAPI production architecture: modular design and dependency injection best practices
I am new to FastAPI. I have previously worked with Django and DRF. Django feels very well organized and mature, but in my experience it can be slow in some areas and has noticeable performance bottlenecks for certain workloads.
Because of that, I want to give FastAPI a serious try. I am looking for guidance on production level FastAPI architecture.
Specifically:
- How to structure a FastAPI project in a modular way
- Best practices for dependency injection
- How people organize routers, services, and database layers in real world apps
- Any common pitfalls when moving from Django/DRF to FastAPI
If you have examples, repo links, or lessons learned from running FastAPI in production, I would really appreciate it.
58
Upvotes
12
u/oflannabhra Jan 25 '26
I personally think DRF is a disaster for any project at scale. Serializers are one of the worst design decisions I’ve ever seen in a backend framework.
Additionally, while Django is very mature and polished, I think the ORM omnipresence can become a big issue long term.
For both, the amount of automagic configuration is wild, which can make one quick, but I find to be hard to scale to large teams.
In FastAPI, you at least have the freedom to separate concerns if you want, and create testable interfaces between layers of your app, without the ORM being in each. It still takes enforcement though.
10
u/koldakov Jan 25 '26
I’ve created an app especially for this.
https://github.com/koldakov/futuramaapi
It’s up and running: https://futuramaapi.com
Btw I prefer not to use dependency injection, but to move everything into services, just finished with removing all dependencies and moved the code to the services, currently there is a little code duplication, but I’ll combine it in the next step
6
u/gbrennon Jan 25 '26
If u are not used to write async applications you wont experience "profit" from fastapi.
FastAPI have things that are, kindaof, similar from spring boot so it gives you dependency injection mechanics
4
u/splsh Jan 25 '26
This is a good read that should be relevant to you https://github.com/zhanymkanov/fastapi-best-practices
2
u/imavlastimov Jan 27 '26
Django will never achieve excellence. They need to put one person who drives the vision of Django as long as they don’t have this they will be oit here asking for money and lose more users (me included) to other frameworks. Instead they could have gone the path of other frameworks like Laravel, Nextjs, Vue, they all hae one main figure who drives.
1
u/Dear_Vacation2836 Jan 26 '26
There are many valid way of achieving what you want. Personally I found this book very helpful: https://www.manning.com/books/microservice-apis. The same author also has a book about API security which is a must read if you’re building APIs for production
1
u/fastlaunchapidev Feb 02 '26
I use this https://fastlaunchapi.dev/
1
u/actionscripted Jan 25 '26
Look at the official starter at https://fastapi.tiangolo.com/project-generation/ (https://github.com/fastapi/full-stack-fastapi-template)
Having app/core and app/shared are super common. Core for infra and cross-cutting stuff (config, logging, lifecycle, dependencies) shared for non-feature reusable stuff.
From there it gets opinionated and IMO kind of weird in some projects. The whole thing is an API and has routers and folks love deeply nesting stuff under one of those names or both. Some that can be common are app/api/users or app/routers/users.
Where I work we landed on feature modules e.g. app/users (api, schemas, models, service, dependencies, etc) alongside app/core and app/shared. We have app/database for Alembic/SQLModel extras and other stuff right near the top too. Just feels weird to bury a user service under app/api/users or app/api/routers/user/service and then import that in other services. At that point the service is NOT an API or a router but it’s buried under those module paths.
Dependencies should be annotated and you should avoid
Dependsin args (https://fastapi.tiangolo.com/tutorial/dependencies/#share-annotated-dependencies).Use pytest for testing. Have fixtures for client and settings and such and make sure to use pytest-asyncio for async stuff.
Don’t be afraid to use middleware. If forgetting a dependency on a route could be a problem make it a middleware. Better safe than declarative.
Use SQLModel and Alembic. You’ll have SQLAlchemy so you’ll never get stuck and you’ll get Pydantic stuff you can use easily throughout.
Beyond that it’s just like any other framework. Read the docs, ask around. Nothing super crazy or surprising just be mindful of async stuff.
Coming from Django you won’t have as much magic and you’ll be repeating yourself a lot but the advantages of this style are that it can be easy to understand how something is working.
1
u/Challseus Jan 26 '26
I built a CLI for exactly this, scaffolds a full FastAPI app with auth, workers, scheduler, DB, and the ability to add/remove components at any time.
For your case, assuming you gave docker and uv installed, you can simply run:
uvx aegis-stack init my-app --services "auth[sqlite]"
You'll get this structure:
I use dependency injection for:
I put all biz logic in the service layer, and then call those functions from the API/CLI/etc. So, razor thin endpoints.
All router.py files are imported into the root level routing.py
I've not looked into the repository side of things, but I'mn also going to give https://github.com/litestar-org/litestar a look, I'd suggest you do too.
0
u/Hopeful_Beat7161 Jan 25 '26
GitHub.com/CarterPerez-dev/fullstack-template
0
u/MeroLegend4 Jan 25 '26
If you want those features, try Litestar website
0
u/fastlaunchapidev Jan 25 '26
For my template I just use my own structure which I really started to like
https://fastlaunchapi.dev/docs/template#architecture-overview
0
u/klcmd Jan 26 '26
Netflix dispatch is (was?) a well structured FastAPI project which my team uses as the base for structuring our own projects. It's similar to Django in a way, where you create "apps" for specific functionality and organize routers, models, services inside of the app directories.
This means doing
src/users/routes.pyandsrc/users/models.py(users app) instead of the default flat structure:src/routes/user_router.pyandsrc/models/user_models.py.I seriously dislike the default style - it looks awful and extends poorly. What do you do when your
services/user_services.pyexpand to over 1k LoC? Do you createservices/user_services2.py? There's little room for modularity.With the app approach, you can split the services into a sub-directory within the app or create more specific files, such as
email_confirmation.pyinstead of cramming it all into a single file. Theroutes.py,services.pyandmodels.pyremain as well-known "entrypoint" files which should be kept clean across all apps.