Lesson 4 · about 30 minutes
Works with no signal
Your app starts remembering things: feeds, bookings, scores. Entries are saved on the phone first and sent when there's signal, so nothing is lost in a car park. Two phones can share the same log, and there are backups.
A Cloudflare D1 database behind the app, written only through your Worker. An offline queue on the phone that syncs when it's back online, and backups you can restore.
A D1 database that only your Worker can read and write, with every field checked. The phone saves entries to a queue first, with ids it makes itself, so a retry never saves anything twice. It syncs with a cursor, adds database columns before the code that needs them, and keeps backups with Time Travel plus a nightly copy in R2.
Every entry goes through the Worker, which checks it before saving. Nothing else can write to your data.
What you'll make
- A database for your app's entries
- Entries that save with no signal and sync later
- Two phones sharing one log
- Backups you can restore
You'll need: an app online from lesson 2 or 3.
Somewhere to keep entries
Until now your app only remembered things on one phone. Now it gets a database at Cloudflare, called D1, so entries are kept safely and every phone can see them. Think of it as one shared notebook that lives online, instead of a separate notepad on each phone.
Your agent runs this and adds the database to wrangler.jsonc. Free plan: up to 5 GB.
Every entry is checked
Your phone never talks to the database directly. It sends entries to your app's server code, the Worker, which checks each one first: is it the right kind of thing, the right size, a sensible date, and from someone allowed to add it?
One clean…() per record type: allow-list fields, check types, ranges and lengths, reject the rest with a plain 400 message. Never trust ids or roles from the browser.
Save on the phone first
Every entry is saved on the phone straight away, then sent to the database when there's a connection. The screen updates instantly either way.
Try it: switch the signal off, add a couple of entries, then switch it back on and watch them move across.
Try it with no signal
Now try the real thing on your phone: turn on Airplane mode, add an entry, turn Airplane mode off.
On a computer: Chrome DevTools → Network → set "Offline".
The entry appears straight away, a small "waiting to save" note shows while offline, and it disappears once it's sent.
My entry vanished after I reconnected
Two phones, one log
Open the app on a second phone or a second browser, signed in to the same log (lesson 5 covers sign-in). Add an entry on one; it shows on the other within seconds.
Sync with a cursor (updated_at), paged, with a "more" flag; soft deletes so deletions travel too; last write wins per row is fine for small groups.
Changing what you store later
Apps grow: one day you'll want to store something new. Your agent does this with small, numbered changes to the database, applied before the new version goes live, and only ever adding things while older versions are still on people's phones.
Database change first, then the code that uses it.
Backups
D1 keeps a rolling history you can restore to any minute: 7 days on the free plan, 30 days on Workers Paid (from Cloudflare's docs, November 2026). For longer, your agent can save a nightly copy to your own storage.
Lesson learned: test a restore once, on a copy, counting rows. A backup you've never restored is a hope, not a backup.
Did it work?
Tick these off. They're saved in this browser, so you can come back later.
- My app saves entries in a database
- I added something with no signal and it saved when I was back online
- Two phones (or two browsers) see the same entries
- I know how far back I can restore