Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

chris48s
Copy link
Member

@chris48s chris48s commented Jan 28, 2025

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:

class GlobalRequestStorage(object):
    storage = threading.local()

    def get(self):
        if hasattr(self.storage, "request"):
            return self.storage.request
        else:
            return None

...

GLOBAL_REQUEST_STORAGE = GlobalRequestStorage()

...

def get_request():
    return GLOBAL_REQUEST_STORAGE.get()

So fundamentally, it is just:

  • Is there a value set for request (by the middleware)?
  • If yes, return it
  • Otherwise, return None

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() and using= throughout the codebase, but that will happen in a second follow-up PR.

@chris48s chris48s changed the title Only serve from replice when there is a request in scope Only serve from replica when there is a request in scope Jan 28, 2025
@chris48s chris48s requested a review from symroe January 28, 2025 15:34
@coveralls
Copy link

coveralls commented Jan 28, 2025

Coverage Status

coverage: 71.731% (-0.08%) from 71.811%
when pulling f376993 on db-routers20250127
into b379d74 on master.

@chris48s

This comment was marked as outdated.

@chris48s
Copy link
Member Author

Right. I promise I've finished mucking about with this now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants