Zero-Downtime Multi-Tenancy in Django: From User-centric to Organizations-based Architecture
Migrating a production Django app to multi-tenancy without downtime or bugs
seems impossible
At Sendcloud, we refactored a 600k+ lines Django app using proxy models, dual FKs, and strategic
migrations, with zero downtime, and no bugs.
At SendCloud, we tackled this exact challenge across our Django monolith: 600k+ lines of Python
code with continuous production traffic. This talk shares our battle-tested strategy that allowed
continuous deployments throughout the migration, and enabled us to move slowly, one Django app at
a time.
The approach:
- Proxy models as a bridge: Creating an Organization proxy of User, which allowed us to change
ForeignKeys in code while maintaining database compatibility
- Progressive refactoring: Using helpers like a base class with fallback code, and custom codemods
to systematically update models, tests, and business logic across all apps
- Dual foreign keys: Adding new FK constraints pointing to the organization table alongside existing
user FKs, using PostgreSQL’s NOT VALID to avoid table locks
- Seamless cutover: Swapping from proxy to real Organization model without SQL execution, then
dropping old constraints