Skip to content

Size sync requests by bytes, pace by CPU time, and sync off the main thread - #8

Merged
tetujin merged 1 commit into
mainfrom
feature/sync-throughput
Nov 19, 2026
Merged

tetujin merged 1 commit into
mainfrom
feature/sync-throughput

Conversation

@tetujin

@tetujin tetujin commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

A sync used to send 1,000 rows per request, wait a fixed 0.1 s between requests, and prepare requests on the main thread. This PR changes that:

  • Size-based requests. A request holds as many rows as fit in maxBatchBytes, measured in the form the server receives. The default is 4 MB for AWARE Micro (Google App Engine accepts up to 32 MB) and 900 KB for other servers (below the nginx default of 1 MB and PHP's post_max_size). maxBatchRows (20,000) caps the row count, and batchSize is now the database read chunk.
  • Server limits. On a 413, the limit for that host is halved and saved in UserDefaults. On a timeout, it is halved for the running app and grows back after 20 successful requests. On a 429 or 503, the sync waits for Retry-After. In all three cases the same rows are sent again, so nothing is lost.
  • Pacing. The fixed wait is replaced by a rest proportional to the CPU time of the last batch:
    • cpuRestRatio is 1 in the foreground and backgroundCpuRestRatio is 3 in the background.
    • The rest is doubled in Low Power Mode, at least 5 s when the thermal state is serious, at least 30 s when it is critical, and at most 120 s.
    • batchInterval sets a minimum rest; set it to 0.1 for the 1.6 behaviour.
  • Off the main thread. Sync work runs on a serial queue per table, with .background QoS while the app is in the background. Completion handlers are still called on the main thread.
  • Faster request building. SQLiteEngine writes each row as JSON straight from the SQLite values (Engine.fetchUploadJSON), without making a dictionary per row first.
    • The values are the same as before: NULL columns are left out and REALs keep full precision.
    • SQLite's json_object() is not used because it keeps only 15 significant digits.
    • A row that JSON cannot hold, such as a BLOB or a non-finite number, goes through the old builder.
    • A subclass that overrides getUploadCandidates must also override getUploadCandidateJSON; returning nil is enough.

Results

Load test: 360,000 accelerometer rows in the iOS Simulator, sent to a local AWARE Micro-like server.

1.6.5 This PR
Time to upload the backlog 55.4 s 15.8 s
Requests 360 23
Mean CPU during sync 24.6% 30.7%
UI response, max during sync 36.9 ms 12.0 ms
Peak phys_footprint during sync 49 MB 56 MB
Rows received 360,000 360,000
Server rejecting bodies over 1 MB (60,000 rows) all received, 0 rejections all received, 2 rejections (4 MB → 2 MB), then every request fits

Tests

All 105 tests pass. This PR adds:

  • DbSyncBatchingTests: size limits for JSON and form bodies, compact format and body handler, maxBatchRows, an oversized row, the 413 and timeout limits, pacing, and config defaults.
  • UploadJSONTests: on a real SQLite database, checks that the JSON rows carry the same values as fetch, including NULL columns, string escapes, Unicode, and doubles compared bit for bit, and that a BLOB row is marked. A full sync sends every row once with exact numbers.

🤖 Generated with Claude Code

…thread

- Requests hold as many rows as fit in maxBatchBytes (default 4 MB for AWARE
  Micro, 900 KB for other servers), measured in the form the server receives.
  maxBatchRows caps the row count; batchSize is now the DB read chunk.
- 413 halves the per-host limit and remembers it; a timeout halves it for the
  run and grows back; 429/503 wait for Retry-After. The same rows are resent.
- The fixed 0.1 s wait between requests is replaced by a rest proportional to
  the CPU time of the last batch (cpuRestRatio, backgroundCpuRestRatio), longer
  in Low Power Mode and when the device is hot.
- Sync work runs on a serial queue per table, never on the main thread, with a
  lower QoS in the background.
- SQLiteEngine writes each upload row as JSON straight from the SQLite values
  (Engine.fetchUploadJSON), without a dictionary per row. Values are the same
  as before and REALs keep full precision (json_object() keeps only 15 digits).

Load test (360,000 accelerometer rows, AWARE Micro-like server): sync time
55.6 s -> 15.8 s, requests 360 -> 23, UI response max during sync 36.9 ms ->
12.0 ms, peak phys_footprint 49 -> 56 MB, no rows lost; a server rejecting
bodies over 1 MB costs two 413s, then every request fits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tetujin
tetujin merged commit dbef80c into main Nov 19, 2026
2 checks passed
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