Before Compressing Real-Time Sync to 1-RTT, We Rewrote What "Saved" Means
Redefining persistence boundaries around draft guards, transactional IndexedDB, and read-only search workers before cutting Mindloft sync latency to 1-RTT with incremental Loro projection caching, a 64-doc hot kernel LRU, and signed OSS uploads.
Before optimizing real-time sync latency on Mindloft (formerly Brainlet / 枝间), we had to fix what “saved” actually meant.
Getting the order backwards just writes corrupted state over the wire faster.
Closing the “pretend saved” holes first
On November 1, auditing baseline 41a7538 from a heavy-user perspective uncovered 50 data-autonomy issues, including four release blockers:
- Unblurred notes wiped mid-typing (PR01):
PropertyPanelreacted to unrelatedrevisionticks by overwriting local input state with the previous model snapshot, erasing active drafts whenever a remote update landed. - Exit allowed after Stage save failure (PR02): Under an injected
QuotaExceededError, the UI showed an error banner and still unmounted the view when clicking back to the mindmap. - Cut-and-paste stripped attachments and links (PR03):
ClipboardNodeonly serializedtitle,note, andchildren, silently discarding images, links, and relationship lines after deleting the source branch. - Deleted cloud maps left public Stage URLs live (PR04): Moving a source map to trash returned
200 OKon anonymousGETrequests whileDELETEreturned404.
We locked down all four before touching performance: dirty guards on notes, a shared exit-protection queue across Stage and mindmap, full subtree + asset metadata on internal clipboard commands with single-step undo, and persistent revocation lists tied to document deletion. We also moved AI persistence out of localStorage into transactional IndexedDB (ramilo-ai-work-v2), split single-map export from encrypted full backup v2 (64 MiB plaintext cap, empty-workspace restore only), and replaced synchronous main-thread indexing with a read-only Web Worker reading IndexedDB checkpoints (100 docs/page, max 50 hits/doc, 200 total; tested up to 1,002 documents, 5,000 deep, and 10,000 wide with a 38.6ms input-to-next-frame peak).
Cutting sync to 1-RTT: incremental projection and a 64-kernel LRU
With persistence boundaries solid, live multi-device editing still felt sticky. Clients re-walked the tree to build projections on keystrokes, the sync server cold-decoded Loro snapshots per update, and broadcasts blocked the document write queue.
Commits 6f76fa9b and f55c1fc3 stripped down four layers without relaxing a single ACL, private-node isolation, or durability rule.
1. Loro kernel layer (packages/document-core/src/loro/loro-document-kernel.ts)
#cachedPortable now keys off (epoch, opCount, changeCount): single-step edits apply in O(1) in place, and remote deltas apply optimistically with rejectPending guarding uncommitted state. Read paths switch to zero-copy toPortableReadonly(), and server validation kernels pass trackUndo: false to skip maintaining an UndoManager.
2. Hot kernel LRU and 1-RTT handshake (packages/sync/src/cloud/authorized-authority.ts)
AuthorizedDurableAuthority keeps a bounded pool of warm kernels:
const MAX_HOT_KERNELS = 64;
const MAX_HOT_KERNEL_BYTES = 64 * 1024 * 1024;
const HOT_KERNEL_IDLE_MS = 5 * 60_000;
Every cache hit verifies (sequence, batchId, payloadHash). During connection handshake, owner and editor clients include their known lineage and version vector in HELLO, so SYNC_STATE returns missingUpdates in the same round trip (1-RTT).
One boundary is enforced by regression tests in live-incremental-sync.test.ts: the server must never send missingUpdates to a viewer, ensuring raw CRDT frames containing hidden-node text never leak to read-only guests.
Decoupling broadcast, adaptive client flush, and direct OSS uploads
In services/sync/src/index.ts and services/api/src/server.ts, post-persist broadcasts move into per-document FIFO broadcastQueues so they no longer stall the next DURABLE_ACK. Subscriber checks collapse into one sessionAclSnapshot, BroadcastDeliveryCache shares one viewerMessage projection Promise per role:aclVersion, and presence cursor updates bypass projection entirely. We also bumped WebSocket perMessageDeflate from 256 to 1024 bytes and replaced decodeBase64Strict’s full re-encode check with direct trailing = zero-bit validation.
On the client and asset side:
- Adaptive Web flush (
apps/web/src/app.tsx,editor-session.ts,cloud-live.ts): Structural edits (add/move node) wait16ms, typing waits50ms, and continuous bursts cap atmaxWaitMs = 160ms.onlineandvisibilitychangeinvokereconnectNow()instead of waiting out exponential backoff. - Lightweight mobile bridge (
apps/mobile/native-lab):lightweightReplyanddeltaRecordsavoid round-tripping fullportableJSON over the WebView bridge, whileimmediateUi: trueupdates native views before coalescingAsyncStoragewrites. - Direct OSS uploads (
services/api/src/storage/oss-direct-upload.ts):DirectUploadCoordinatorverifies write permissions, signs an HMAC-SHA256 upload ticket bound to(doc, acc, aid, mime, size, key)(300sTTL +30sgrace), lets the client upload large images directly to Aliyun OSS, and verifies the digest before committing metadata and quota.
Fewer fake “saved” states and less work on the hot path—that order made every subsequent millisecond easy to trust.
Comments