Wednesday, August 30, 2017

Database router with read replica

I'm hoping for some advice on how to utilize read replica databases with Django.  I searched the django-users archive but didn't find much discussion about this.  I also can't find many blog posts or discussion elsewhere.  Do people have experience or general advice on using read replicas with Django?

I am hoping to implement a database router that will intelligently use a read replica while maintaining consistency despite replica lag.  I have a couple things I'd like another opinion on:

First is handling replication lag.  Our read replica typically lags ~20ms behind master, so we want to route all queries that occur after a write within the same request to the master database.  I believe this can be accomplished using a thread local in the database router that is reset after each request using a middleware.  Does that seem like a reasonable approach?

Second is handling atomic blocks.  As far as I can tell, Django will not automatically route all queries within an atomic block to the same database, so this needs to be handled within the router.  Is connection.in_atomic_block a public API, or is there a better way to tell if we're within an atomic block?


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment