import random
class ReadWriteRouter:
"""Route reads to replicas, writes to primary."""
def db_for_read(self, model, **hints):
"""Send reads to random replica."""
return random.choice(['replica1', 'replica2'])
def db_for_write(self, model, **hints):
"""Send all writes to primary."""
return 'default'
def allow_relation(self, obj1, obj2, **hints):
"""Allow relations between objects in same database."""
db_set = {'default', 'replica1', 'replica2'}
if obj1._state.db in db_set and obj2._state.db in db_set:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""Only run migrations on primary."""
return db == 'default'
DATABASES = {
'default': { # Primary for writes
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'HOST': 'primary.db.example.com',
},
'replica1': { # Read replica
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'HOST': 'replica1.db.example.com',
},
'replica2': { # Read replica
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'HOST': 'replica2.db.example.com',
},
}
DATABASE_ROUTERS = ['core.routers.ReadWriteRouter']
# Read from primary (for consistency)
user = User.objects.using('default').get(id=1)
# Explicitly use replica
posts = Post.objects.using('replica1').filter(status='published')
Database routers enable read-write splitting for scaling. I direct reads to replicas and writes to primary. The router examines hints and model to make routing decisions. For read-heavy apps, this distributes load across multiple databases. I use using() to force specific databases when needed. Connection pooling is important with multiple databases. I monitor replica lag to ensure data consistency. For critical reads, I can force primary database. This architecture scales Django beyond single-database limits.