Blog

How a document saves itself without losing your last sentence

Save on every keystroke and a busy document hammers the database. Save too rarely and a crash takes your last sentence. Here are the four timers between your keyboard and the database in Nemi Docs, an interactive figure that runs them on your typing, and the guard that refuses to overwrite a document with a blank page.

Engineering7 min read

Fig. 03 s longest the save timers hold your typing

There's no Save button in Nemi Docs. You type, and your document is stored. That simplicity hides a real trade-off: save too eagerly and a busy document hammers the database with a write per keystroke. Save too lazily and a crash, redeploy, or closed laptop takes your last sentence with it.

This post walks through the timers that decide when your words reach the database, why there are four of them instead of one, and the guard that refuses to save a document that suddenly went blank. Type into the figure below to watch them run on your own keystrokes.

Where does a keystroke go?

A Nemi document is a live, shared object. Each open browser tab keeps its own copy, and the server keeps one more, called the room, that every editor connects to. Your keystroke updates your local copy first, so typing never waits on the network. From there, it makes two more hops: from your browser to the room so others see it, and from the room to the database so it survives.

Each hop has its own quiet timer. Your browser batches edits and sends them once you've paused for 250 ms, turning a burst of typing into one request instead of twenty. The room then waits for 900 ms of quiet before writing to the database, so a paragraph becomes one write instead of one per word. Big pastes skip the first wait: anything over 48 KB leaves at once and asks to be saved right away.

Your keystrokes, on their way to the database

You typeSent to the roomIn the databasenow12 s ago
Saved because you pausedSaved because 3 s checkpointSaved because 4 s max-wait
Waiting for the database
nothing
Longest any keystroke waited
0.00 s
Saves so far
0
Each mark is computed from your own keystrokes by the same four timers the editor and server run. The network and database are treated as instant, so on a real connection each mark lands one round trip later. Try typing in bursts, then try Type steadily for me, which never pauses long enough for a quiet save.

What if you never stop typing?

Quiet timers have an easy-to-miss flaw: they only fire when you stop. Someone transcribing an interview might not leave a 900 ms gap for minutes. With quiet timers alone, nothing they typed would reach the database until they paused, and a crash in that window would lose all of it.

Two more timers close that gap, one on each side. In the browser, the first unconfirmed edit starts a 3 second checkpoint. When it fires, the browser sends everything it holds and asks the room to write to the database right away and confirm it.

In the room, the first edit of a burst starts a 4 second maximum wait. That forces a save no matter how busy the document is.

How long a keystroke can wait, by typing speed

Longest wait to reach the database

Longest wait, seconds

0123
1005009001,5002,500

Milliseconds between keystrokes

The longest any keystroke waited to be stored, for steady typing with the gap shown between keystrokes, computed by the same model. Below about 900 ms between keys the room never sees a quiet moment, and the 3 second checkpoint is what saves you.

The result is a hard upper bound that doesn't depend on how you type. Pause, and your words are stored 1.15 seconds after your last keystroke. Type without stopping, and the timers never hold anything longer than 3 seconds. On a real connection, add one round trip to both. While you're offline, the checkpoint waits for the network to return, then sends everything at once.

In the model, the room's own maximum wait never fires first because the browser's checkpoint always beats it. It's there as a backstop for edits that reach the room without a checkpoint behind them, such as when a browser's checkpoint request never arrived.

250 ms

of quiet before your edits leave the browser

900 ms

of quiet before the room writes

3 s

the longest the timers hold a keystroke, typing without a break, online

What happens when you close the tab?

Leaving is when edits are most at risk, so it gets its own path. When a tab is hidden or closed, the browser hands any pending edits to a delivery mechanism built to finish sending after the page is gone, and asks the room to store them immediately. That mechanism has its own size limit, which is the other reason large pastes are sent right away: it keeps what remains at closing time small.

If anything is still unconfirmed when you try to leave, the browser shows its standard “leave this page?” warning. The chip in the editor reads Saved only once the server confirms your words are on disk, not when they were merely sent, so the warning and the chip always match what's actually stored.

Why would a save be refused?

One kind of save is refused on purpose. When a document opens, the editor starts empty and loads the stored content a moment later. If something goes wrong in that moment and the editor is still blank when the first save fires, it would overwrite the real document with an empty one. One refresh, and everything would look gone.

The blank document guard

Live copynot open yetStored document

1/4The stored document has text

Three paragraphs, saved earlier.

The check looks for content, not just characters: a document with only an image or a table isn't blank, and it saves normally. Spreadsheets and canvases use the same guard, counted in cells and elements.

The guard has a cost, and we chose to pay it. If you select everything in a document and delete it, leaving it truly empty, that emptiness isn't saved until your next real edit. The stored copy still holds the old text, so it can reappear next time you open the document.

It's a strange moment, but the alternative is a rule that can wipe out a document no one meant to delete. Between an empty page that comes back and a full one that vanishes, the first is the one you can live with.

Between an empty page that comes back and a full one that vanishes, there is only one safe default.

What happens when everyone leaves?

The room doesn't disappear the moment the last person disconnects. Connections drop and return all the time behind a proxy, on a train, or when a laptop briefly sleeps. Tearing the room down on every blip would mean rebuilding it from the database on every reconnect. So an empty room waits 10 seconds first, with its save timers still running.

When it does close, it saves before letting go and waits for that save to finish. Until then, the room stays registered, so anyone who reopens the document gets the live room instead of an older copy from the database.

If the save keeps failing, the room isn't discarded. It holds the edits in memory and keeps retrying, because a room that gives up quietly is a document that loses its last minutes quietly. A failed save retries after 1.5 seconds, up to 6 times in a row, and a later edit or reconnect starts the attempts again.

None of this is visible when it works, which is the point. You can read what the editor shows you in Writing a document, find older versions in Version history, see what to do when the chip won't turn to Saved in saving and sync problems, and explore the rest of the editor on the Docs page.

What happens after the write is a story of its own. The content is encrypted before it reaches the database, and whether you can change a shared document is decided by your role in the workspace, checked on every write.

Questions people ask

How does autosave work?

An autosaving editor saves your changes without a Save button, usually when you pause typing, backed by a maximum wait so continuous typing gets saved too. In Nemi Docs, edits leave your browser after 250 ms of quiet, the server writes them after 900 ms of quiet, and a 3 second checkpoint covers typing that never stops.

How often does Nemi Docs save?

If you pause, your words are stored about 1.15 seconds after your last keystroke. If you type without stopping, the timers never hold edits longer than 3 seconds. On a real connection, add one network round trip to both.

What is the difference between a debounce and a maximum wait?

A debounce waits for a quiet moment before acting, turning a burst of typing into one save instead of one per key. A maximum wait forces a save after a fixed time no matter how busy you are, because a debounce alone never fires while you keep typing. Nemi uses both, on each side of the connection.

Can I lose my work if I close the tab?

Nemi takes three steps to prevent that. When a tab is hidden or closed, the editor sends any pending edits through a browser mechanism that finishes after the page is gone. If anything is still unconfirmed when you try to leave, the browser asks if you're sure. The Saved chip only appears once the server confirms your words are stored.

What does Saved mean in Nemi Docs?

It means the server has confirmed your edits were written to the database, not just sent. The editor tracks edits made, edits received, and edits confirmed, and only the last one turns the chip to Saved. A request the server refused never counts as stored.

Why did text I deleted come back in my document?

If you select everything and delete it, Nemi doesn't save the completely empty document until your next real edit, so the old text can reappear next time you open it. That's deliberate: it stops a page that was briefly blank while loading from overwriting the stored copy. Type anything in the empty document and it saves normally.

Where the numbers come from

  • Browser side timers and the durable checkpointlib/collab/client.ts (FLUSH_DEBOUNCE_MS, EAGER_FLUSH_BYTES, DURABLE_CHECKPOINT_MS)
  • Room save timers, retries and the dispose gracelib/collab/rooms.ts (SAVE_DEBOUNCE_MS, SAVE_MAX_WAIT_MS, SAVE_RETRY_MS, MAX_SAVE_FAILURES, DISPOSE_GRACE_MS)
  • The blank document guardlib/collab/sync.ts (persistDoc), lib/collab/shared.ts (docHasContent)
  • The timing model behind the figuresapp/(marketing)/blog/_posts/collab-autosave/pipeline.ts

Use the thing we write about.

Files, docs, sheets, mail, photos, calendar and meetings in one account, built the way this post describes. Nemi is opening in groups: put your name down and we tell you when yours is up.

15 GB free when you get in. No card needed.