Two devices, one calendar: three sync traps
In short: I wanted my calendar to sync between my iPhone and my Linux computer. “Write the whole calendar to the cloud and let the newest copy win” looks good enough at first, but it silently deletes changes made offline. I had to make every event its own record, keep deletions as markers, and stop a new device from writing anything before the cloud had really answered. I tested all of it on my computer with two separate “devices”.
Context
My Calendar App is a single web app: it runs as a PWA installed on the iPhone home screen and as a desktop app on Linux with Electron. Data lives first in the device's own storage. To let the phone and the computer see the same calendar, I added Google's Firebase (the Firestore database and email/password sign-in). Firestore can queue writes while offline and send them once the connection is back; the real question was how two devices' changes should be merged.
users/{uid}/categories/{id}Security rule: each user can read and write only their own data.
Trap 1: Writing the whole calendar as one document
The app's first version stored the whole calendar as a single document with a “last updated” time; when two copies met, the newer one won. On one device that is fine. On two devices this happens: I add an event on the phone while it is offline, and meanwhile I add another event on the computer. When the phone reconnects, one copy counts as “newer” and overwrites the other entirely. An event disappears without any warning.
First version: the whole calendar as one document
- Phone offline: adds event A.
- Computer: adds event B and writes it to the cloud.
- The phone reconnects; one copy counts as “newer”.
✗ Lost:One copy overwrites the other entirely; A or B disappears without warning.
Now: every event its own document
- Phone offline: adds event A.
- Computer: adds event B and writes it to the cloud.
- The phone reconnects; A and B are separate documents.
✓ Kept:Both A and B stay. Only if the same event changes in two places does the latest change apply.
The fix was to make the unit of merging smaller. Every event and every event type is now its own document (users/{uid}/events/{id}), each carrying its own change time. The rule is “last change wins” per item: if two devices change different events, both are kept; if they change the same event, the latest change applies. Even the order of event types is stored as its own field so that it can sync too.
Trap 2: Deleted items come back
When you delete an event, deleting its cloud document as well seems natural. But if the other device is offline at that moment, when it returns it sees: “I have this event and the cloud doesn't; it must be new, let me upload it.” The deleted event comes back.
Deleting the document entirely
- The computer deletes event C; its cloud document is deleted too.
- The phone is offline at the time and still has C.
- The phone reconnects: “I have C, the cloud doesn't.”
✗ Comes back:The phone takes C for new and uploads it again.
Leaving a tombstone
- The computer deletes event C; only “deleted” and its time stay in the cloud.
- The phone is offline at the time and still has C.
- The phone reconnects and sees the tombstone.
✓ Deleted:The phone deletes C as well. If C was changed on the phone after the deletion, that change wins.
I solved this with two things. A deleted item's document stays, but only carries a “deleted” flag and a time (a tombstone). And every device keeps its own copy of the last cloud state it knew. That way it can tell whether an item that “is in the cloud but not here” is new, or was deleted by me.
Trap 3: An empty cache looks like an empty cloud
This was the sneakiest case I noticed while designing. When Firestore starts listening to a list, it first answers from its local cache; the server's answer comes later. On a new device that has never synced, the cache is empty. If I read that first answer as “there is nothing in the cloud”, the new device treats its default event types as “local changes” and writes them over the types in the account.
The rule became: a device that has never synced writes nothing until a real answer from the server has arrived. A device that has synced before keeps working when it opens offline, because it already knows the cloud state.
- 1. Listening startsA new device has signed in.
- 2. Cache answerEmpty. A device that never synced has nothing cached.
- 3. Server answerThe account's real events and types.
✗ Writing at 2:The empty answer looks like “the cloud is empty”; the device's default types are written over the account's.
✓ Waiting for 3:The account's data comes first; then only what really changed is written.
How did I test it?
I used Firebase's test server that runs on the computer (the Emulator Suite) and opened the app in two windows with completely separate storage: one “phone” and one “computer”. The cases I checked:
- Basic scenariosign-in, add, edit, delete, reopen✓11 / 11 passed
- Offline merge5 changes: 4 kept, the older one on the same event gave way to the newer✓5 / 5 as expected
- Security rulesunauthorised read and write attempts✓4 / 4 rejected
| Case | Result |
|---|---|
| Sign-up, sign-in, add, edit, complete, delete, two simultaneous adds, type change, reopening (11 checks) | 11 / 11 |
| Phone offline: a new event, completing an event, renaming the “Market” event. Meanwhile on the computer: a new event and renaming the same “Market” later | All merged; the computer's “Market” version won |
| Reading / writing another user's data, reading while signed out, writing to a list that is not allowed (4 attempts) | 4 / 4 rejected (403) |
The test checked every 250 milliseconds; changes appeared on the other “device” within that interval or right after it. That number belongs to a local test server; over the real internet the delay will be longer. One small lesson too: my first offline test was invalid, because Electron's “go offline” switch did not cut the connection to the local test server. Simulating the outage with Firestore's own “disable network” call made the test meaningful.
Known limits
- The unit is the item, not the field: if I change an event's title on the phone and its time on the computer, the last saved event wins as a whole and the other change is lost. There is no field-level merge.
- Clocks: “last change” is decided by each device's own clock. A device with a wrong clock can win unfairly.
- A late, older write: a device that stayed offline for a long time can, for a short while, put its queued older write over a newer version in the cloud. When the device holding the newer version opens, it uploads it again and both sides match once more.
- Tombstones pile up: markers for deleted items are not cleaned up for now. For a personal calendar that does not matter, but a growing system would need to clean them.
Three questions for your own app
- If two devices change different things while offline, is your unit of merging small enough to keep both?
- Can a deleted item be uploaded again by a device that was offline at the time?
- On a new device, does the first “empty” answer come from the cache or the server? Do you start writing without checking which?
Transparency
I write the code for this app with AI, using Claude Code. Which approach to take, what to test and what to publish are my decisions. All results here were taken on my computer, on Firebase's test server; they are not measurements over the real internet. The sync code is in the zaman-planlayici repository: src/sync.js, firestore.rules for the security rules, and the test cases with their output in docs/senkron-testleri.md (in Turkish).
References
- M. Kleppmann, A. Wiggins, P. van Hardenberg, M. McGranaghan. Local-first software: You own your data, in spite of the cloud, Onward! 2019.
- Firebase documentation. Access data offline (Cloud Firestore).
- Firebase documentation. Firebase Local Emulator Suite.
Turan Tech