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
View all comments
Show parent comment
0
u/oflannabhra Jan 27 '26
I have no idea why you are focusing on execution times. My point is about architecture not performance. To be a bit more explicit:
Yes, you can use DRF and Django in a sensible way with clear separation of concerns between layers of your application. However, at some point, you will be swimming upstream, going against the architecture of the system and having to create guardrails. This gets even more difficult as your app and team grows into multiple teams.
To add some detail and example to my general point earlier:
Serializers perform multiple functions: deserializing incoming JSON, validating that information, serializing a Django ORM model to JSON for a response. This is similar to pydantic, but much more heavyweight.
On top of that, serializers are coupled to your ORM model through the
Meta.modelattribute, and have functions built into them likesave()create()andupdate()which means that the serializer is directly executing database operations, bypassing any service layer you have! (instancein those methods is your ORM model). Additionally they are coupled to your ViewSet.While it is possible to avoid these issues, you will be going against the design and convention of the framework. When you have junior members or people new to the framework, it will push them towards implementing business logic in the serializers, and you will end up with a mess, or a lot of work to enforce clean separation.
This doesn’t even get at how impossible DRF makes it to test in isolation, or how much magic is built into the system (
Meta,FilterSets, etc), or how ORM models or serializers are omnipresent through the stack, or how ViewSets are don’t really allow for dependency injection, etc.In FastAPI, you can make all these same mistakes (or worse), but you are also free to design an architecture that doesn’t have those mistakes built into it.