Basically, to not have to worry about replication lag if a user is logged in, we want them to read/write to the master; but if the user is not logged in, we want them to read from the replica and write to the master. Is this possible using Django routers?
Share
This might be bad practice, but at any point within your code, as long as you are using Django >= 1.2, with the
using()method andusingkeyword, you have total control over which database select your QuerySets and do your saves and deletes.For example, for a QuerySet:
or saving an object:
So you could always do something like this in a view:
But this could get very cumbersome and should you decide to go along this road, you should seek a more elegant ways to mitigate it. Signals, middlewares and/or model managers come to mind.
See Manually selecting a database from the Django documentation.