I have a django application working with replications server and configured to use the master as write only and the slave as read only. But when i started monitoring my database load i found that the master was having a lot of read traffic, after some investigation i found this:
def get_or_create(self, **kwargs):
# Update kwargs with the related object that this
# ForeignRelatedObjectsDescriptor knows about.
kwargs.update({rel_field.name: instance})
db = router.db_for_write(rel_model, instance=instance)
return super(RelatedManager, self.db_manager(db)).get_or_create(**kwargs)
get_or_create.alters_data = True
So from that code i take it’s using the write database for both the read and the write operations needed for get_or_create instead of using the slave to read and master to “create” as expected, has anyone had this issue? Is there anyway to fix it other than re-writing the get_or_create function?
Splitting
get_or_createbetween master and slave is very bad idea. This will lead to race condition. You will either get duplicates or database throwing duplicate key errors.The problem is the replication delay. With standard MySQL master-slave setup, replication is asynchronous. This means eventual consistency, slave will have everything master does, but there is a lag. This lag is usually very low, but it always exists.