The problem
Every external API call authenticated first, and every authentication hit the same small table through the ORM. The table was tiny and the answer barely changed, but the lookup ran on every single request, so the database spent a meaningful share of its time answering the same question.
Approach
A caching layer at the ORM level rather than in each caller.
- Intercepts
searchandreadon the auth model, so nothing calling it needed to change. - Redis holds the result, keyed so a credential change invalidates cleanly.
- Failure falls through to the database rather than failing the request, since an unavailable cache should slow things down, not break them.
Outcome
Repeated auth lookups stopped reaching PostgreSQL. The work happens once and is answered from memory afterwards.