Batch etsource cache reads with fetch_multi to reduce N+1 queries#1748
Batch etsource cache reads with fetch_multi to reduce N+1 queries#1748aaccensi wants to merge 3 commits into
Conversation
MeritOrder collapses 6 individual cache fetches into one fetch_multi call. Molecules does the same for its 2 keys. Both use Thread.current to ensure fetch_multi runs at most once per request. Closes #1747
985694c to
673325d
Compare
Atlas data only changes on ETSource import, so per-request scoping is wasteful. Class-level memoization is reset via NastyCache#expire_local! whenever an import clears Rails.cache. Closes #1747
|
I've looked at this a couple of times but I am not confident about it without more investigation. I also see as I was typing this you've updated the cache clearance @aaccensi so maybe all moot now - will take a look later on |
|
I was looking at the situation with stale data that we should not get after an ETSource import that the solid cache investigation flagged and decided to try to factor also this in here as a test making it more optimal. |
Discussed with @aaccensi and I understand the approach now - I was a little confused before but the approach makes sense to me now and seems practical! |
Context
Since adopting Solid Cache, several controller actions that trigger a GQL calculation are flagged by Sentry as N+1 queries. This was not visible before because Memcached round-trips are cheap and don't use SQL; with Solid Cache every cache operation is a SQL query. The fix implemented is cache-backend agnostic.
Implemented changes
MeritOrdercollapses 6 individual cache fetches into one fetch_multi call.Moleculesdoes the same for its 2 keys. Both use Thread.current to ensure fetch_multi runs at most once per request.Notice that
FeverandReconciliationwere not changed:Feverhas a single key so batching adds no value, and Reconciliation has a dependency between its two keys (reconciliation_hashis computed usingreconciliation_carriers, so they cannot be fetched in a singlefetch_multi)Related
Closes #1747
Checklist