-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only serve from replica when there is a request in scope #8221
base: master
Are you sure you want to change the base?
Conversation
f1679a5
to
9a003a1
Compare
This comment was marked as outdated.
This comment was marked as outdated.
2d8baf0
to
f376993
Compare
f376993
to
873cc01
Compare
Right. I promise I've finished mucking about with this now |
polling_stations/db_routers.py
Outdated
@@ -3,20 +3,27 @@ | |||
from django.conf import settings | |||
from django.db import DEFAULT_DB_ALIAS | |||
|
|||
PRIMARY = settings.PRINCIPAL_DB_NAME |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small point but can we use PRINCIPAL
rather than primary? I think Will has some reason why principal is a better word, that I can't remember, but either way it's confusing to mix the two
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in e422558
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.