Rebasely Beta: data may be purged

exampleslive-notes Public

Save a snapshot of each note to R2 every 100 versions #2

  1. Justin Torre opened this

    A room keeps one row with the current text and the last 200 ops, which is all the sequencer needs. Anything older is gone, so when someone deletes half a note by accident and notices an hour later, there is nothing to go back to. This saves a copy of every note to R2 every 100 versions and lets anyone read them.

    What changed

    • New R2 bucket live-notes-snapshots, bound as SNAPSHOTS. Keys are snapshots/<object id>/<version>.txt, with the version zero-padded so R2 lists them in order. The object id is idFromName(noteId), which the room knows as ctx.id and the Worker can compute from the URL, so the room never has to be told its name.
    • When the room saves. Room.receive() sets saveSnapshot on the outcome once the note is 100 versions (SNAPSHOT_EVERY) past the last saved copy. NoteRoom sends the ack and the broadcast first, then writes to R2, then records the version in a new snapshot table in its SQLite. Editors never wait on R2.
    • Failures. A failed put is logged and nothing is recorded, so the next edit is due again and retries. Only one save runs at a time per room.
    • Reading. GET /notes/:id/snapshots?after= lists a note's snapshots oldest first, 100 at a time, and GET /notes/:id/snapshots/:version returns one as text/plain with a year of cache-control. The Worker reads both straight from R2, so looking at history never wakes the room.
    • Architecture: a new snapshots component with edges from note-room (writes) and api (reads), snapshots added to the editing view, a new step at the end of the "An edit reaching other editors" flow, and a new flow, "Reading a note's history". The Visual tab shows the new component and both flows.
    • Knowledge base: a new snapshots concept, decision 0003 on why history lives in R2 and not in the room, and updates to the note room, conflict strategy and local development notes and the README.

    Rollout

    npx wrangler r2 bucket create live-notes-snapshots, then deploy. Existing notes create the snapshot table on their next wake and save their first copy 100 versions after that.

    Testing

    • test/snapshots.test.ts (9 tests): a save is due every 100 versions with the note as it is at that version, not again until 100 after a recorded save, again on the next edit after a failed one, never for a no-op or refused edit, and the recorded version survives a wake from hibernation. Against an in-memory bucket: key format, save and read back, and paging the list.
    • test/routes.test.ts covers the two new routes and refuses /snapshots/0, padded and oversized versions.
    • Ran it under wrangler dev with a script sending 205 edits over the socket: /notes/groceries/snapshots listed versions 100 and 200, and /snapshots/100 returned the 100 character text.
    • npm test (49 tests) and npm run typecheck pass. kb check and arch check pass.
  2. src/room.ts line 87
    87    if (note.version - this.#store.lastSnapshot() >= SNAPSHOT_EVERY) out.saveSnapshot = note;
    1. Justin Torre

      Thinking through an R2 outage: once a note is due, every edit tries a put until one works. The #saving flag in NoteRoom keeps that to one request at a time per room, so it is bounded by how fast people type. I am fine with that. If we ever see a long outage in the logs, a backoff on the recorded failure time would be a small change here.

    1. Justin Torre

      Before this ships to the public notes, I will add a lifecycle rule on snapshots/ that expires objects after 90 days. Decision 0003 already says the bucket is where retention belongs, so no code change for that.

Open

Ready to merge

  • Successduragit/ci/testsucceededDetails
  • No conflictsmain can fast-forward to the head

Sign in to comment.