Reference FastAPI codebases for production grade applications #15311
Replies: 7 comments 4 replies
-
|
Here is an example for GCP and Vertex AI that provides a more comprehensive starting point for production-grade FastAPI applications: https://github.com/janisto/fastapi-playground |
Beta Was this translation helpful? Give feedback.
-
|
polar backend is powered by fastapi. |
Beta Was this translation helpful? Give feedback.
-
Elevating FastAPI: Architecture Patterns for Production-Grade AppsI see you're looking to move beyond the standard templates to build a truly scalable and maintainable backend. When moving from a prototype to a "production-grade" system (especially with Redis, SSE, and S3), the most critical shift is moving from Route-Heavy logic to a Service/Repository Pattern. Here are the key architectural pillars I recommend for a professional FastAPI codebase: 1. The Service/Repository Layer (Encapsulation)The
Why this matters: It makes your code unit-testable without needing a database or a running app. 2. Bridging with Dependency InjectionUse FastAPI's # app/api/deps.py
def get_user_service(db: Session = Depends(get_db)) -> UserService:
repo = UserRepository(db)
return UserService(repo)
# app/api/endpoints/users.py
@router.get("/{user_id}")
def get_user(user_id: str, service: UserService = Depends(get_user_service)):
return service.get_user_details(user_id)3. Lifespan Events for InfrastructureFor Redis and SSE, avoid global variables. Use Lifespan Events to manage connection pools. This ensures your app shuts down gracefully without leaking connections. @asynccontextmanager
async def lifespan(app: FastAPI):
# Startup: Setup Redis/S3 clients
app.state.redis = await aioredis.from_url("redis://localhost")
yield
# Shutdown: Close connections
await app.state.redis.close()4. Real-World ReferencesBeyond the ones already mentioned, I highly recommend checking out:
5. Middleware & ObservabilityIn production, don't forget a custom Middleware for correlation IDs (Request IDs). This is vital for tracing a single request through your logs when things go wrong in a distributed system. If this architectural breakdown helps you structure your next big project, please consider marking it as the answer! |
Beta Was this translation helpful? Give feedback.
-
|
@toptechie156 IMO there is no one-size-fits all for production grade applications, so it is difficult (impossible) to have a fully generic template. Some aspects/questions to answer that I have observed over several projects, which will definitely impact the final code:
And so on... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
NADescription
Are there any open-source codebases the community recommends as references for building production-grade applications? I'm particularly focused on structuring a scalable, maintainable backend.
My stack includes Postgres, Redis, SSE for streaming, S3 for storage, and integrations with third-party services like payments and LLM APIs.
I only found https://github.com/fastapi/full-stack-fastapi-template/tree/master but I feel this still lacks lots of concepts that a production grade application would have like middleware, logging, lifespan etc.
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.135.3
Pydantic Version
latest
Python Version
latest
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions