Skip to content

Batch etsource cache reads with fetch_multi to reduce N+1 queries#1748

Open
aaccensi wants to merge 1 commit into
masterfrom
warm-cache-etsource-classes
Open

Batch etsource cache reads with fetch_multi to reduce N+1 queries#1748
aaccensi wants to merge 1 commit into
masterfrom
warm-cache-etsource-classes

Conversation

@aaccensi
Copy link
Copy Markdown
Member

@aaccensi aaccensi commented May 11, 2026

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

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.

Notice that Fever and Reconciliation were not changed: Fever has a single key so batching adds no value, and Reconciliation has a dependency between its two keys (reconciliation_hash is computed using reconciliation_carriers, so they cannot be fetched in a single fetch_multi)

Related

Closes #1747

Checklist

  • I have tested these changes (I believe this cannot be tester properly locally)
  • I have updated documentation as needed
  • I have tagged the relevant people for review

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
@aaccensi aaccensi requested review from louispt1 and noracato May 11, 2026 15:32
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.

N+1 Cache Queries During GQL Initialization

1 participant