Tracking Down a Supabase Egress Spike in veilanon
In veilanon, a few minutes of normal channel browsing was enough to spike our Supabase REST egress. Nothing in that flow transferred large files, and voice/video traffic already ran over LiveKit SFU, so the culprit had to be small, repeated metadata queries.
Opening the network tab confirmed it: toggling a theme, opening a modal, or collapsing the sidebar triggered bursts of role_members, memberships, roles, channels, spaces, and presence requests.
Root cause: subscribing to the whole UI store
Inside src/lib/stores/permissions.ts, role and override refreshes were wired directly to uiStore.subscribe(...). The catch was that uiStore held more than activeSpaceId and activeChannelId — every UI state change (theme updates, sidebar toggles, modal flags) lived in the same store.
Because Svelte store subscriptions fire on every store update and the callback did not track the previous spaceId or channelId, any UI interaction re-ran the full permissions fetch chain. Under normal clicking, that added up to hundreds of redundant Supabase queries per minute.
There was a second multiplier in src/lib/stores/spaces.ts: loadSpaces() and loadChannels(spaceId) had no in-flight deduplication, so components mounting together fired identical parallel requests on startup.
The fix (Commit fccd431)
Three small changes stopped the loop:
- Change guard in
permissions.ts: TracklastSpaceIdandlastChannelId. IfspaceId === lastSpaceId && channelId === lastChannelId, return immediately without touching the network. - In-flight deduplication for permissions: Added
fetchInFlightkeyed by${spaceId}:${channelId ?? ''}so concurrent refreshes for the same context share one request. - Request deduplication maps in
spaces.ts: AddedspacesInFlightandchannelsInFlight(Map<string, Promise<...>>). Concurrent calls for the samespaceIdreceive the pending promise, and.finally()clears the entry when it settles.
const channelsInFlight = new Map<string, Promise<Channel[]>>();
async loadChannels(spaceId: string) {
const existing = channelsInFlight.get(spaceId);
if (existing) return existing;
const promise = channelApi.list(spaceId)
.then((channels) => {
this.setChannels(spaceId, channels);
return channels;
})
.catch(() => [] as Channel[])
.finally(() => {
channelsInFlight.delete(spaceId);
});
channelsInFlight.set(spaceId, promise);
return promise;
}
Takeaway
Subscribing directly to a broad UI store without a key selector or equality check turns innocent client-side state updates into database load. Any store subscriber that triggers network I/O needs explicit key-change guards and in-flight deduplication from day one.