Only serve from replica when there is a request in scope #8221
+47
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refs https://app.asana.com/0/1204880927741389/1208838937595665/f
I'm going to walk through each of the changes in this commit one by one.
1. b14e19a - make DB router code easier to understand
This is just a little cleanup of terminology before we start making functional changes. While I was trying to work on this code, I found myself thinking "is the main one default or principal?". Default is sometimes the primary and sometimes the replica, depending on context. I think this makes it clearer what is going on, hopefully 🤞
2. f8c429b - only serve from replica when there is a request in scope
This is the meat of it really. When we talked about this earlier, we decided an important question was: Does the request detection add much overhead? Essentially, we don't want to add something slow to every round-trip to the DB. Having read over the source of django-middleware-global-request, here's how it works...
We register a middleware, which saves the request into a thread-safe global variable.
The middleware runs once per request (or zero times, if there is no request).
The subset of code that gets executed when we call
get_request()
is:So fundamentally, it is just:
so that's the only code we're adding into every DB (read) round-trip.
I'm not worried about the performance impact of this and I don't see the need to try and "optimise" that.
3. 6631799 - explain with comments
In this commit I am just adding a few comments to explain what is going on more clearly
4. 873cc01 - always read from primary in the admin
I'm trying to get ahead of some other problems here. Basically here I am saying that if we're editing things in the admin, also read from the primary. This then guards against the case where you edit something in the admin and it looks like it hasn't updated because it hasn't replicated.
5. Next Steps
As agreed, the next thing I'm going to do is review (and mostly remove) occurrences of
.using()
andusing=
throughout the codebase, but that will happen in a second follow-up PR.