Master/Slave Databasing

With multidb.MasterSlaveRouter all read queries will go to a slave database; all inserts, updates, and deletes will do to the default database.

First, define SLAVE_DATABASES in your settings. It should be a list of database aliases that can be found in DATABASES:

    'default': {...},
    'shadow-1': {...},
    'shadow-2': {...},
SLAVE_DATABASES = ['shadow-1', 'shadow-2']

Then put multidb.MasterSlaveRouter into DATABASE_ROUTERS:

DATABASE_ROUTERS = ('multidb.MasterSlaveRouter',)

The slave databases will be chosen in round-robin fashion.

If you want to get a connection to a slave in your app, use multidb.get_slave():

from django.db import connections
import multidb

connection = connections[multidb.get_slave()]
class multidb.MasterSlaveRouter

Router that sends all reads to a slave, all writes to default.

allow_relation(obj1, obj2, **hints)
Allow all relations, so FK validation stays quiet.
allow_syncdb(db, model)
Only allow syncdb on the master.
db_for_read(model, **hints)
Send reads to slaves in round-robin.
db_for_write(model, **hints)
Send all writes to the master.
Returns the alias of a slave database.

This Page