Best Practice / Idea on How to Add Multi-Tenancy? #1774
Unanswered
matteolavaggi
asked this question in
Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Hello everyone,
I'm looking to build a multi-tenant application using this fantastic full-stack-fastapi-template as a foundation. I've been researching the best ways to approach multi-tenancy, and I see a few common patterns, but I'm not sure which would be the most seamless to integrate with the architecture of this project.
My goal is to ensure that each tenant's data is properly isolated. The main approaches I've come across are:
Database per Tenant: A completely separate database is created for each tenant. This seems to offer the best isolation but might be complex to manage, especially with database migrations (Alembic) and scaling.
Shared Database, Separate Schema per Tenant: A single database is shared, but each tenant gets their own schema (like PostgreSQL schemas). This feels like a good balance between isolation and management overhead.
Shared Database, Shared Schema: All tenants share the same tables, and a tenant_id column is used in each relevant table to distinguish and filter the data. This seems to be the easiest to set up initially, but I'm concerned about data leakage if a query is not written correctly.
I'd love to hear the community's thoughts and get some guidance on the following:
Recommended Approach: Is there a recommended or "best practice" approach for implementing multi-tenancy specifically within the context of this project structure (FastAPI, SQLAlchemy, PGAdmin, Celery, etc.)?
Implementation Details:
How would you recommend identifying the tenant for each request? (e.g., from a subdomain tenant1.example.com, a custom HTTP header, or a claim in a JWT token).
How would you manage database connections or sessions to ensure they are scoped to the correct tenant? Is there an elegant way to handle this with FastAPI's dependency injection system?
Are there any specific considerations for alembic migrations or background tasks with celery in a multi-tenant setup?
Pros & Cons: What are the major pros and cons you see for each approach in relation to this template?
Any insights, code snippets, or links to relevant resources would be greatly appreciated. I'm looking to start on the right foot and build something scalable and secure.
Thanks in advance for your help!
Operating System
Windows
Operating System Details
don't apply
Python Version
3.13
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions