Skip to content

sync: cut fetch memory and drop the MessagePack protocol - #116

Merged
thecodrr merged 4 commits into
masterfrom
sync/memory-improvements
Oct 30, 2026
Merged

thecodrr merged 4 commits into
masterfrom
sync/memory-improvements

Conversation

@thecodrr

@thecodrr thecodrr commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fetches used to hold a whole cursor batch of decoded SyncItems, with every cipher turned into a UTF-16 string, while the client applied each chunk. On prod, the sync servers run a full (gen2) GC about every 20s per replica, which points to large-object churn on this path.

Changes

  • Size-bounded chunks, sent as stored. Chunks are planned from _ids plus a server-computed $bsonSize, capped at 3MB and 500 items. Each chunk is then loaded by _id as RawSyncItem, which keeps the stored BSON bytes and writes JSON straight from them. Clients get the same JSON, byte for byte (see RawSyncItemTests). The server holds only the chunk awaiting an ack (see SyncV2HubSignalRTests).
  • Generations respected. Both queries filter by the generation pinned when the fetch starts, so a fetch never mixes generations. A fetch already running when a reset lands still finishes on the old generation, as on master. An item replaced under the new generation is skipped and stays pending.
  • Drop the MessagePack hub protocol. Only /hubs/sync/v2 is mapped. Clients have used JsonHubProtocol since 2.6.17 (Mar 2024), and v2 hub clients are all newer than that.
  • Fetch failures labelled by reason: disconnected, timeout, client_error or server_error.
  • Quieter connection rejections. HubConnectionHandler logged each one (e.g. unconfirmed emails, about 1/s on prod) as an error. Its logging is now off, and SyncAuthorizationHubFilter logs rejections at Info and unexpected failures at Error. Production filters out Info, so a new sync.v2.connection_rejections counter, labelled reason, is what shows rejections: unauthorized, account (deleted or reset), hub or error.

Testing

  • dotnet test tests/Notesnook.API.Tests -c Release: 111/111 pass.

After deploy, watch

  • Fetch latency, slowest 5% and 1%. Each chunk adds a find by _id, and full syncs read each document twice (measured at about 1.2× storage reads under cache pressure).
  • Gen2 GC rate and LOH size. They should drop from today's baseline: gen2 about every 20s per replica, heap 200–400MB.
  • sync_v2_fetch_failures_fetches_total by reason. Failures are 0.13% of fetches today, with no breakdown.
  • sync_v2_connection_rejections by reason. Expect about 1/s of unauthorized, from clients with unconfirmed emails.

…mid-fetch resets

Rejections are logged at Information, which production filters out, so a
counter by stage is what shows them. Also drops a catch around the no-op
Hub.OnDisconnectedAsync.
@thecodrr
thecodrr merged commit af728d9 into master Oct 30, 2026
1 check passed
@thecodrr
thecodrr deleted the sync/memory-improvements branch October 30, 2026 07:43
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