From 96248231fab4f36093b3d9750dfe9bee40b78e35 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 19 May 2026 12:51:05 +0100 Subject: [PATCH] ML-169.7: add scrobble stream reset fix task --- ...m-reset-on-every-scrobble-PubSub-update.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 backlog/tasks/ml-169.7 - Fix-Stream-reset-on-every-scrobble-PubSub-update.md diff --git a/backlog/tasks/ml-169.7 - Fix-Stream-reset-on-every-scrobble-PubSub-update.md b/backlog/tasks/ml-169.7 - Fix-Stream-reset-on-every-scrobble-PubSub-update.md new file mode 100644 index 00000000..6993540a --- /dev/null +++ b/backlog/tasks/ml-169.7 - Fix-Stream-reset-on-every-scrobble-PubSub-update.md @@ -0,0 +1,60 @@ +--- +id: ML-169.7 +title: "Fix: Stream reset on every scrobble PubSub update" +status: To Do +assignee: [] +created_date: "2026-05-19 11:48" +labels: + - perf + - fix +dependencies: [] +references: + - >- + backlog/documents/doc-27 - + Phase-4-Performance-Audit-Low-Hanging-Fruit-Findings.md +modified_files: + - lib/music_library_web/live/stats_live/index.ex + - lib/music_library_web/live/scrobbled_tracks_live/index.ex +parent_task_id: ML-169 +priority: high +ordinal: 28000 +--- + +## Description + + + +Fix performance issue where every scrobble batch (cron: every 5 minutes) triggers a PubSub message that causes StatsLive.Index and ScrobbledTracksLive.Index to reload their entire stream with `reset: true`. + +## Problem + +Every scrobble batch triggers a `"listening_stats:update"` PubSub message which causes: + +- **StatsLive.Index**: Re-executes `recent_activity` (expensive 4-correlated-subquery query) + `scrobble_count`, then resets the `:recent_tracks` and `:recent_albums` streams +- **ScrobbledTracksLive.Index**: Re-executes the full paginated list query and resets the `:tracks` stream + +This fires every 5 minutes on ALL connected clients, even idle ones. + +## Files + +- `lib/music_library_web/live/stats_live/index.ex:576-583` — `assign_scrobble_activity` with `reset: true` +- `lib/music_library_web/live/scrobbled_tracks_live/index.ex:279` — `handle_info(%{track_count: _count})` +- `lib/music_library_web/live/scrobbled_tracks_live/index.ex:334` — `load_and_assign_tracks` with `reset: true` + +## Fix + +- **StatsLive**: Use `stream_insert` for new tracks instead of `reset: true`. Keep a cursor (e.g., max `scrobbled_at_uts`) to only insert genuinely new tracks. +- **ScrobbledTracksLive**: Use `stream_insert` for new tracks only if they match the current search/filter. Otherwise, show a "new scrobbles" badge that refreshes on user click. +- Consider debouncing or batching the updates to avoid per-track insert thrash. + + +## Acceptance Criteria + + + +- [ ] #1 StatsLive no longer performs full stream reset on scrobble updates +- [ ] #2 Only new tracks are inserted into streams +- [ ] #3 ScrobbledTracksLive no longer performs full stream reset on scrobble updates +- [ ] #4 Existing stream ordering is preserved after insert +- [ ] #5 No duplicate tracks appear in streams +