exampleslive-notes Public
Save a snapshot of each note to R2 every 100 versions #2
- src/room.ts line 87
87 if (note.version - this.#store.lastSnapshot() >= SNAPSHOT_EVERY) out.saveSnapshot = note;-
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.
-
-
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.
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
live-notes-snapshots, bound asSNAPSHOTS. Keys aresnapshots/<object id>/<version>.txt, with the version zero-padded so R2 lists them in order. The object id isidFromName(noteId), which the room knows asctx.idand the Worker can compute from the URL, so the room never has to be told its name.Room.receive()setssaveSnapshoton the outcome once the note is 100 versions (SNAPSHOT_EVERY) past the last saved copy.NoteRoomsends the ack and the broadcast first, then writes to R2, then records the version in a newsnapshottable in its SQLite. Editors never wait on R2.GET /notes/:id/snapshots?after=lists a note's snapshots oldest first, 100 at a time, andGET /notes/:id/snapshots/:versionreturns one astext/plainwith a year ofcache-control. The Worker reads both straight from R2, so looking at history never wakes the room.snapshotscomponent with edges fromnote-room(writes) andapi(reads),snapshotsadded 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.snapshotsconcept, 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 thesnapshottable 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.tscovers the two new routes and refuses/snapshots/0, padded and oversized versions.wrangler devwith a script sending 205 edits over the socket:/notes/groceries/snapshotslisted versions 100 and 200, and/snapshots/100returned the 100 character text.npm test(49 tests) andnpm run typecheckpass.kb checkandarch checkpass.