Author: aegisSoft

Tracking Down a Supabase Egress Spike in veilanon

Postmortem on how subscribing permissions.ts to the entire uiStore triggered hundreds of redundant Supabase queries per minute, and how key-change guards plus request deduplication maps fixed it.

#veilanon#supabase#svelte#postmortem#performance

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:

  1. Change guard in permissions.ts: Track lastSpaceId and lastChannelId. If spaceId === lastSpaceId && channelId === lastChannelId, return immediately without touching the network.
  2. In-flight deduplication for permissions: Added fetchInFlight keyed by ${spaceId}:${channelId ?? ''} so concurrent refreshes for the same context share one request.
  3. Request deduplication maps in spaces.ts: Added spacesInFlight and channelsInFlight (Map<string, Promise<...>>). Concurrent calls for the same spaceId receive 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.

Command Palette

Search for a command to run...