Skip to content

[Perf] Cap my_follows CTE at 200 in GetTracks/GetPlaylists#793

Merged
raymondjacobson merged 1 commit intomainfrom
ray/perf-cap-my-follows-limit
May 8, 2026
Merged

[Perf] Cap my_follows CTE at 200 in GetTracks/GetPlaylists#793
raymondjacobson merged 1 commit intomainfrom
ray/perf-cap-my-follows-limit

Conversation

@raymondjacobson
Copy link
Copy Markdown
Member

Summary

Lower LIMIT 5000 to LIMIT 200 in the my_follows CTE used by GetTracks and GetPlaylists. The CTE feeds two consumers (followee_reposts, followee_favorites) that each emit at most 3 rows ordered by follower_count DESC, so 5000 was almost always materializing more than the LATERAL ever consumed.

Impact

Verified on the prod read replica with user 20 (1752 follows), 10-track id list, three warm runs each (with #792 / MATERIALIZED applied):

LIMIT runs (ms) mean
5000 86, 97, 92 92 ms
200 43, 31, 23 32 ms (~2.9×)

Stacks with #792. GetTracks and GetPlaylists together represent ~310M calls / 4.5B ms in pg_stat_statements.

Risk

  • A user whose only follower-of-X reposter is ranked >200 by follower_count will no longer surface in followee_reposts / followee_favorites. Acceptable trade-off — those low-fanout reposts are already dominated by the top-200 in the rendered top-3 social proof.
  • No correctness change for the >99% of users with <200 follows. Existing TestGetTrackPersonalization (added in [Perf] MATERIALIZE my_follows CTE in GetTracks/GetPlaylists #792) and the rest of the suite cover the personalization shape.

Test plan

  • go test -count=1 ./api/... (full suite, all green)
  • EXPLAIN ANALYZE on read replica shows ~2.9× warm-cache speedup
  • Local server hits /v1/tracks/trending?user_id=Wem1e (Phuture, 1752 follows) — 400-600ms warm

Stacks on

🤖 Generated with Claude Code

The CTE feeds two consumers (followee_reposts, followee_favorites)
that each emit at most 3 rows ordered by follower_count DESC. The
existing LIMIT 5000 was non-binding for nearly every user and
materialized far more rows than the LATERAL ever needs.

Verified on the prod read replica with user 20 (1752 follows) and a
10-track id list, three warm runs each (PR 1 MATERIALIZED applied):

  LIMIT 5000:  86, 97, 92 ms   (mean 92)
  LIMIT 200:   43, 31, 23 ms   (mean 32)   ~2.9x

Edge case: a user whose only follower-of-X is ranked >200 by their own
follower_count will no longer surface in followee_*. Acceptable
trade-off: those low-fanout reposts are dominated by the top-200
in the social-proof rendering anyway.
@raymondjacobson raymondjacobson merged commit 1e8504a into main May 8, 2026
5 checks passed
@raymondjacobson raymondjacobson deleted the ray/perf-cap-my-follows-limit branch May 8, 2026 01:01
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.

1 participant