Skip to main content

Delta sync cursor

The mobile app keeps a local mirror of the library current through the delta feeds under /api/sync/ (apps/backend/api/views/sync.py): photos, people, albums of each kind, tags and the sharing users. The desktop app does not use them; it mirrors no server rows. Each feed pages its rows in last_modified order, with the primary key as the tie-break, and logs hard deletes and lost visibility as tombstones in DeletionLog.

Passes and cursors​

Not released yet (comes after 1.7.0) A pass is the run of requests from the stored cursor until a page says it is the last one. Every page carries:

FieldMeaning
itemsup to page_size rows after the cursor
tombstonesthe ids deleted or no longer visible since the previous pass; only on the first page of a pass, never on a seed
next_cursorthe cursor of the next page of this pass; null once a page is shorter than page_size, which ends the pass
resume_cursorwhat the client stores after applying the page, in the same batch as its rows, and sends as cursor next time
totalthe rows in the feed, only on a seed (no cursor)
server_timethe database time of the request, which the cursor also carries
vthe envelope version: 2 since 1.8.0, 1 before

The cursor is opaque to clients. It carries the anchor of its pass, the database time of the pass's first page (Django's Now(): STATEMENT_TIMESTAMP() on PostgreSQL, STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW') on SQLite, to the millisecond, so every pod issues the same anchor; a request fails rather than fall back to the app clock when the database answers anything but a time with a zone), and a position:

  • On a full page, resume_cursor equals next_cursor: the last row's (last_modified, id) and the pass's anchor. A client killed mid-pass resumes the same pass from it.
  • On the last page, resume_cursor starts the next pass. Its position is the last row's key, or, when that is newer, CURSOR_OVERLAP (10 minutes) before the anchor; the next pass then serves every row with last_modified at or after that floor, and the tombstones logged since the anchor minus CURSOR_OVERLAP.

Why the overlap​

Writers stamp last_modified (and DeletionLog.deleted_at) in Python before their transaction commits: auto_now, or .update(last_modified=timezone.now()). A row stamped earlier can become visible after a row stamped later was already served, through a lock wait, a slow transaction or clock skew between pods. Up to 1.7.0 the cursor was the last row's last_modified and the next request asked for rows strictly after it, so such a row, or such a deletion, was never served. On SQLite this happens with the autocommit .update() writers, whose stamp is taken before they wait up to 300 seconds for the write lock; on PostgreSQL with any concurrent writers.

Starting each pass again CURSOR_OVERLAP before the previous one began serves every change that committed within CURSOR_OVERLAP of its stamp, on both databases and whatever the skew between pods within that window. The client applies a row it already has as an upsert by id and a tombstone as a delete, so a row or tombstone served twice changes nothing. A pass on a busy library re-sends the rows changed in the last 10 minutes; a quiet one re-sends nothing, since its floor is the last row. The window assumes two things:

  • Stamp at write time. A writer takes last_modified right before it writes the row, never on an object it saves later. auto_now, save(update_fields=[...]), and a queryset update() or bulk_update() through OwnedQuerySet that names a synced column stamp at the write by themselves; bulk_update() also replaces a last_modified its field list names with a fresh stamp. A hand-set photo.last_modified = timezone.now() or setattr(photo, "last_modified", ...) on a row written after a long loop does not, and StampAtWriteTimeTest (api/tests/infra/test_sync_feed_writes.py) fails on both in api/, nextcloud/, librephotos/ and chunked_upload/. A queryset update(last_modified=now) is not checked: its value goes into the statement that writes it, and every such call computes now right before it. Not released yet (comes after 1.7.0) Up to 1.7.0 Classify Media and resetting a category to Auto over a selection stamped every photo when they classified it and wrote it in batches, up to a whole library later, so the phone could miss it.
  • Clocks in step. On every web and worker host, the database clock minus the host's clock, plus the time from stamp to commit, stays under 10 minutes. A host whose clock runs behind the database stamps rows that look older than they are and can lose them for good; one that runs ahead only makes the next pass send its rows again. Run NTP on every host and on the database server.

The model is SyncFeed in verification/metadata.

Expired cursors​

DeletionLog rows older than 90 days are pruned, so a cursor whose tombstones would start before that horizon is answered with 410 cursor_expired, and the client clears the feed and seeds it again.

Not released yet (comes after 1.7.0) A cursor whose anchor lies more than CURSOR_OVERLAP in the future is answered with 410 too, since its tombstones cannot be trusted; a cursor that cannot be read (a time without a zone, a key of the wrong type, a date at the edge of the calendar) is a 400 invalid_cursor.

Not released yet (comes after 1.7.0) The horizon is compared with the cursor's anchor, the time it was issued. Up to 1.7.0 it was compared with the last row's last_modified, so the second page of a seed of a library left untouched for 90 days, and the next pass of a quiet feed, were refused, and the app reseeded until its job budget ran out. The app now allows one reseed per pass: the remote_delta job carries reseeded to the next page and passes allowReseed: false once it is set, so a second 410 fails the job and the next sync trigger starts a new pass. The model is DeltaHorizon in verification/mobile-sync.

A cursor from a server up to 1.7.0 still works: it is read as the start of a pass, with tombstones since its row's last_modified minus CURSOR_OVERLAP, and answered with 410 when that is older than the horizon. The app reads resume_cursor when the server sends it and falls back to next_cursor on older servers.

Clients written for version 1​

Not released yet (comes after 1.7.0) The envelope's v is 2. A client that checks v strictly (the mobile app's builds before 1.8.0 did) rejects every page. One that does not, and stores the last non-null next_cursor and loops until a page comes back empty, ends every pass and loses no change, but next_cursor is now null on any page shorter than page_size, so it never stores the cursor of a short last page and starts each pass again from an older position. Such a client should check v, store resume_cursor after every page and stop at the first null next_cursor. Keeping next_cursor filled on the last page instead was not an option: the next request starts a new pass that serves the last 10 minutes of changes again, so a version 1 client would never see an empty page while the library is busy.