Skip to content

fix(sync): keep each account's encrypted activity within 1 MiB - #52

Merged
D3SOX merged 1 commit into
mainfrom
limit-sync-event-storage
Nov 29, 2026
Merged

D3SOX merged 1 commit into
mainfrom
limit-sync-event-storage

Conversation

@D3SOX

@D3SOX D3SOX commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Problem

sync_event took about 60% of the public instance's database (~915 MiB). Clients pad every activity event to one 64 KiB block, so each record of a few hundred bytes is stored as 87,610 bytes, and an account keeps up to 100 of them for 30 days. Activity is a broadcast that every device reads with its own cursor, so nothing deletes it on delivery.

Change

  • sync_event::append inserts a broadcast, then keeps the account's newest broadcasts, ordered by created_at, while they fit in 1 MiB and number at most 100. Released clients keep about their 11 newest padded events; clients that pad events in 1 KiB blocks (fix(sync): pad activity and device requests in 1 KiB blocks instead of 64 KiB OpenTubeX#3740) keep all 100 in about 160 KB.
  • Sizes come from octet_length(payload), which reads the length from the record header instead of loading the payload from its overflow pages, so this adds no payload reads to the write transaction. SQLite gained that optimization in 3.43, so the server now refuses to start with an older SQLite and says why.
  • Device requests are only counted, as before. Their per-recipient trim could never delete anything, because the account-wide limit of 100 pending requests already applies, so it is gone.
  • README and privacy policy describe the new limit and the smaller event padding.

The server can't read payloads, so end-to-end encryption is unchanged, and released clients keep working. On the public instance, this alone shrinks today's 11,570 events to 3,197 (about −72% of the table). Once clients pad in 1 KiB blocks, the table would hold about 18 MiB for today's event count. Freed pages are reused; the file itself only shrinks after a VACUUM.

Proof: extended events_are_atomic_bounded_expiring_and_scoped_to_account_and_recipient (padded events are trimmed to the newest 11, small ones all kept, a delayed older upload does not evict newer events — this assertion fails when the trim treats the incoming event as newest); writes_and_polls_do_not_read_stored_event_payloads fails with length() and passes with octet_length(); the startup check exits with its error when the probed function is missing. cargo test --workspace, clippy (SQLite and Postgres) and rustfmt pass.

Reviewed with sol-loop: 3 rounds with gpt-6.2-sol on high.
Sol verdict: approved

Made with Claude Opus 5.5 in Claude Code, running in T3 Code.

View guided diff Turn on auto-fix

@sourcery-ai

sourcery-ai Bot commented Nov 29, 2026

Copy link
Copy Markdown

Sorry @D3SOX, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 5 hours and 1 minute by commenting @sourcery-ai review. Upgrade to get a review now.

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The server now retains broadcast activity events within a 1 MiB payload budget and a 100-event limit. Startup checks for SQLite’s octet_length function. Tests and documentation cover retention behavior, payload padding, and device-request limits.

Changes: Activity event retention

Layer / File(s) Summary
SQLite payload-length support
src/database/sync_event.rs, src/main.rs
The database module declares SQLite’s octet_length function. Startup checks that the function is available and exits with a SQLite 3.43 minimum-version error if the check fails.
Broadcast retention and documented limits
src/database/sync_event.rs, PRIVACY.md, README.md
Broadcast events are pruned to the newest 100 events within a cumulative 1 MiB payload limit. Tests cover padded broadcasts, device-targeted requests, delayed events, and payload reads. The documentation describes 1 KiB payload padding, activity retention, and device-request limits.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

  • OpenTubeX/sync-server#37: This PR also uses SQLite’s octet_length function to measure payload size.
  • OpenTubeX/sync-server#47: This PR changes the same sync_event read and write path and includes a test that the current PR also references.
Merge Risk: Minimal · up to 5faa0

The change limits each account's stored activity to 1 MiB and updates the documentation to match. No concrete merge-blocking problem was found. Accounts already over the limit are trimmed on their next broadcast or when their records expire.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: activity events per account are now kept within a 1 MiB limit. The changeset adds a 1 MiB retention budget for broadcast activity events in sync_event.rs, which matche…
Description check Passed The description explains the problem (sync_event using about 60% of the database), the change to append trimming by 1 MiB and 100 events, the use of octet_length to avoid payload reads, the SQLite 3.4…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: activity events per account are now kept within a 1 MiB limit. The changeset adds a 1 MiB retention budget for broadcast activity events in sync_event.rs, which matches the title. The title is specific and uses the conventional fix(sync) prefix.

Full details: Description check

Explanation

The description explains the problem (sync_event using about 60% of the database), the change to append trimming by 1 MiB and 100 events, the use of octet_length to avoid payload reads, the SQLite 3.43 startup check, the removal of the device-request per-recipient trim, and the README and privacy updates. It matches the changeset closely.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c6fe9187-3816-4462-8e7e-d0a31d8f64ce
Commits

Reviewing files that changed from the base of the PR and between aacc6a1 and 5faa06a.

Files selected for processing: 1

  • PRIVACY.md

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Released clients pad every activity event to 64 KiB, which they upload
as 87,610 bytes, so a month of activity took most of the database. Keep
the newest events that fit in 1 MiB, which reads their stored length
without loading payloads from overflow pages, and refuse to start with
SQLite older than 3.43, which cannot.
@D3SOX
D3SOX force-pushed the limit-sync-event-storage branch from aacc6a1 to 5faa06a Compare November 29, 2026 10:59
@D3SOX

D3SOX commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai rate limit

🤖 Posted automatically by coderabbit-review-queue

@coderabbitai

coderabbitai Bot commented Nov 29, 2026

Copy link
Copy Markdown

Your plan includes PR reviews subject to rate limits. Reviews are available now.

@D3SOX

D3SOX commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

🤖 Posted automatically by coderabbit-review-queue

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@D3SOX
D3SOX merged commit ab91f81 into main Nov 29, 2026
19 checks passed
@D3SOX
D3SOX deleted the limit-sync-event-storage branch November 29, 2026 12:00
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