[Perf] Cache /v1/users/:userId/related candidate ids#796
Merged
raymondjacobson merged 1 commit intomainfrom May 8, 2026
Merged
[Perf] Cache /v1/users/:userId/related candidate ids#796raymondjacobson merged 1 commit intomainfrom
raymondjacobson merged 1 commit intomainfrom
Conversation
The collaborative-filter and genre-fallback queries that power
/v1/users/:userId/related are expensive — pg_stat_statements shows
mean 100ms with a 4.5s slow variant; Axiom shows p95 957ms / p99
2.4s. The output is a recommendation, not authoritative state, so
caching the candidate id list briefly is safe.
Cache key includes (userId, filter_followed, [myId only when
filter_followed is true], limit, offset, low_follower_count) so
viewers querying the same artist hit the same entry when they're
not asking for follow filtering. The follow-up GetUsers fetch
(which carries the my-perspective fields) still runs fresh.
Verified on local server pointed at prod replica:
Cache miss: 1.14s (Phuture, collaborative filter)
0.84s (low-follower artist, genre fallback)
Cache hit: 100-130ms ~10x faster
10-minute TTL: recommendations don't need to be real-time, and
artist follower counts/genre distributions don't shift fast.
331e4da to
fc92e6f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cache the candidate user-id list returned by
/v1/users/:userId/relatedfor 10 minutes per request shape. Recommendations are not authoritative state, so a brief cache is safe.Why
The two SQL paths (collaborative filter for high-follower artists, genre fallback for low-follower artists) are both expensive:
pg_stat_statements: collaborative filter mean 100 ms, 43.5M calls; slow variant mean 4,478 ms, 240k calls./v1/users/:userId/related: p50 154 ms, p95 957 ms, p99 2,377 ms.The candidate set is request-shape-dependent but not viewer-dependent in the common case (
filter_followed=false), so most callers hit the same cache entry.Impact
Local server pointed at prod read replica:
~10× faster on cache hit. Cache hit time is dominated by the unchanged
GetUsersfollow-up fetch, not the cached query.Cache key design
cacheMyId = myIdonly whenfilter_followed=true; otherwise it's 0. This keeps the unfiltered (and far-more-common) path viewer-independent so cache hit rate is maximized, while the filter-followed path correctly partitions per viewer.Risk
filter_followed=true. Acceptable for a "you may also like" surface.GetUsersfetch carries the my-perspective fields (is_followed,follower_count, etc.), so those stay fresh even on cache hits.TestV1UsersRelated) verifies thatfilter_followed=trueandfilter_followed=falsecannot bleed into each other.Test plan
go test -count=1 ./api/...(full suite, all green)TestV1UsersRelated🤖 Generated with Claude Code