WIP: Use database replicas for read requests
Fixes #2634
The connection switching is managed using the standard Resolver, which chooses the connection based on the HTTP verb. In the small number of cases where we update the database during GET requests, we override the default choice of connections.
Additionally, after any write in a particular user session, the resolver will continue to use the same (write) connection for two seconds for subsequent reads. This gives time for the changes to be replicated before switching back.
Unfortunately there's a bug in rails when running migrations so we can't merge this yet, but I thought I would push this now for review and discussion anyway. It contains a lot fewer connection overrides than when I last looked at this, since there's been some refactoring in the meantime as we move more things to standard new/create/update/etc.
Upstream bug has been fixed.
Upstream bug has been fixed.
Great! I'll update this PR and check if everything is working now.
I've rebased this PR and resolved the conflicts today.
The fix that we need in Rails hasn't yet made it into a formal release, but I expect that there to be one (rails 7.1) before the end of 2022. I've tested the code with rails main and it now works! (Glossing over some unrelated rails-7.1 related breakages).
So we're back to waiting, but at least we know that we can use our read replicas soon. The steps from here are:
- Wait for the next Rails release
- Update this app to work on that release (e.g. wait for composite_primary_keys and activerecord-import to be updated)
- Rebase this PR
- Should be good to go!