Each JMAP sync pass reads every row of the account: _refresh calls source_flags per changes page #171

Closed
opened 2026-11-03 21:29:36 +00:00 by svc-agent-writer · 1 comment

Summary

On a large JMAP mirror, every sync pass reads every row of the account from disk, even when Email/changes reports nothing. _refresh calls Store.source_flags(account) once per changes page, and that query reads three columns that sit after body_text in the row. On a store of about 249,000 messages this costs about 1 GB of disk reads per pass, and the pass interval grows from about 50 s to about 5 minutes once the page cache is smaller than the table.

Where (v0.11.0; the same code in 0.10.6)

  • jmap.py:650 _changes calls _refresh(created + updated, …) once per Email/changes page.
  • jmap.py:664 _refresh calls self.store.source_flags(account) before it looks at ids, so an empty page still pays the full read.
  • store.py:1951 source_flags: SELECT source_id, flags, mailboxes FROM messages WHERE account = ? AND source_id IS NOT NULL, under self._lock, materialised into a dict of every row.
  • jmap.py:698 _reconcile reads it a second time on a mailbox change.

In the messages table, source_id (column 26) and mailboxes (column 27) come after body_text (column 21). SQLite must walk past the body, including its overflow pages, to reach them. So the query reads close to the whole table, not three short columns.

Evidence

One deployment, image 0.10.6@sha256:beace95e…a74, schema v15. One JMAP account with about 249,000 messages and a 2.7 GB database. One small IMAP account in the same store. sync container memory limit 768Mi. Counts and timings only.

Measure Value
Disk read by sync since pod start (about 13.4 h) 1.05 TB, about 22 MB/s sustained
JMAP pass interval, p50 over 3 h 324 s (min 208 s, max 439 s)
Same mirror on 0.10.4 47–60 s
sync anonymous memory 350 MB (the source_flags dict)
sync page cache about 450 MB: the rest of the 768Mi limit
Disk read per row, after-body columns vs before-body columns 3.7–6.4 KiB vs 0.1–0.3 KiB

The small IMAP account's pass interval also grows (p50 373 s, max 1,687 s) because it waits on the same store lock.

The interval change between 0.10.4 and 0.10.5 has no matching code change in the sync path. The likeliest cause is that the table outgrew the page cache the container limit allows, so each pass now goes to disk. That is an inference, not a measurement.

Proposed fixes

In order of size:

  1. In _refresh, return early when ids is empty.
  2. Look up only the ids on the page: SELECT source_id, flags, mailboxes FROM messages WHERE account = ? AND source_id IN (…), chunked to SQLite's variable limit. The existing partial unique index messages_source (account, source_id) serves the lookup, so it reads one row per id.
  3. For _backfill and _reconcile, which do need every id: extend messages_source into a covering index on (account, source_id, flags, mailboxes) with the same WHERE source_id IS NOT NULL, so the scan reads the index, not the table.
  4. Longer term: move body_text to its own table, so a row scan never walks the body.

Workaround in use

The deployment raises the sync memory limit so more of the table stays in page cache. This hides the cost; it does not remove it, and it does not scale with the mirror.

## Summary On a large JMAP mirror, every sync pass reads every row of the account from disk, even when `Email/changes` reports nothing. `_refresh` calls `Store.source_flags(account)` once per changes page, and that query reads three columns that sit after `body_text` in the row. On a store of about 249,000 messages this costs about 1 GB of disk reads per pass, and the pass interval grows from about 50 s to about 5 minutes once the page cache is smaller than the table. ## Where (v0.11.0; the same code in 0.10.6) - `jmap.py:650` `_changes` calls `_refresh(created + updated, …)` once per `Email/changes` page. - `jmap.py:664` `_refresh` calls `self.store.source_flags(account)` before it looks at `ids`, so an empty page still pays the full read. - `store.py:1951` `source_flags`: `SELECT source_id, flags, mailboxes FROM messages WHERE account = ? AND source_id IS NOT NULL`, under `self._lock`, materialised into a dict of every row. - `jmap.py:698` `_reconcile` reads it a second time on a mailbox change. In the `messages` table, `source_id` (column 26) and `mailboxes` (column 27) come after `body_text` (column 21). SQLite must walk past the body, including its overflow pages, to reach them. So the query reads close to the whole table, not three short columns. ## Evidence One deployment, image `0.10.6@sha256:beace95e…a74`, schema v15. One JMAP account with about 249,000 messages and a 2.7 GB database. One small IMAP account in the same store. `sync` container memory limit 768Mi. Counts and timings only. | Measure | Value | | --- | --- | | Disk read by `sync` since pod start (about 13.4 h) | 1.05 TB, about 22 MB/s sustained | | JMAP pass interval, p50 over 3 h | 324 s (min 208 s, max 439 s) | | Same mirror on 0.10.4 | 47–60 s | | `sync` anonymous memory | 350 MB (the `source_flags` dict) | | `sync` page cache | about 450 MB: the rest of the 768Mi limit | | Disk read per row, after-body columns vs before-body columns | 3.7–6.4 KiB vs 0.1–0.3 KiB | The small IMAP account's pass interval also grows (p50 373 s, max 1,687 s) because it waits on the same store lock. The interval change between 0.10.4 and 0.10.5 has no matching code change in the sync path. The likeliest cause is that the table outgrew the page cache the container limit allows, so each pass now goes to disk. That is an inference, not a measurement. ## Proposed fixes In order of size: 1. In `_refresh`, return early when `ids` is empty. 2. Look up only the ids on the page: `SELECT source_id, flags, mailboxes FROM messages WHERE account = ? AND source_id IN (…)`, chunked to SQLite's variable limit. The existing partial unique index `messages_source (account, source_id)` serves the lookup, so it reads one row per id. 3. For `_backfill` and `_reconcile`, which do need every id: extend `messages_source` into a covering index on `(account, source_id, flags, mailboxes)` with the same `WHERE source_id IS NOT NULL`, so the scan reads the index, not the table. 4. Longer term: move `body_text` to its own table, so a row scan never walks the body. ## Workaround in use The deployment raises the `sync` memory limit so more of the table stays in page cache. This hides the cost; it does not remove it, and it does not scale with the mirror.
Author
Member

Workaround measured: same deployment on 0.11.0 (same source_flags code), sync memory limit raised from 768Mi to 2Gi. One hour, sampled every 10 minutes after the roll.

Measure 768Mi (0.10.6) 2Gi (0.11.0)
sync disk read rate about 22 MB/s sustained 13.0 MB/s in the first 10 min, then 4.4–6.5 MB/s
sync page cache about 450 MB 1.4–1.6 GB
sync anonymous memory 350 MB 307–347 MB
JMAP pass interval, p50 324 s 150 s (min 32 s, max 167 s)
IMAP pass interval, p50 373 s 154 s

More cache cuts the disk reads by about three quarters and halves the pass interval, but it does not remove the per-pass scan: the interval is still about three times the 47–60 s seen before the table outgrew the cache, and the cost grows with the mirror.

Workaround measured: same deployment on 0.11.0 (same `source_flags` code), `sync` memory limit raised from 768Mi to 2Gi. One hour, sampled every 10 minutes after the roll. | Measure | 768Mi (0.10.6) | 2Gi (0.11.0) | | --- | --- | --- | | `sync` disk read rate | about 22 MB/s sustained | 13.0 MB/s in the first 10 min, then 4.4–6.5 MB/s | | `sync` page cache | about 450 MB | 1.4–1.6 GB | | `sync` anonymous memory | 350 MB | 307–347 MB | | JMAP pass interval, p50 | 324 s | 150 s (min 32 s, max 167 s) | | IMAP pass interval, p50 | 373 s | 154 s | More cache cuts the disk reads by about three quarters and halves the pass interval, but it does not remove the per-pass scan: the interval is still about three times the 47–60 s seen before the table outgrew the cache, and the cost grows with the mirror.
piers referenced this issue from a commit 2026-11-04 01:23:25 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dev/mail-mind-mcp#171
No description provided.