Skip to content

Commit d8dc28a

Browse files
committed
more offline
1 parent 6b22118 commit d8dc28a

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

contents/docs/offline.mdx

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,72 @@
11
---
2-
title: Offline Support
2+
title: Offline
33
---
4-
Zero supports offline *reads*. Any data cached locally continues to be accessible while offline.
4+
Zero supports offline reads, but not writes.
55

6-
Currently, Zero also continues to work offline for *writes*, but we do not plan to support this feature and it will be removed before beta. Our plan is that when disconnected, Zero will go into an *offline mode* where all mutations throw and Zero becomes read-only.
6+
<Note emoji="🤓" type="warning" heading="Well actually…">
7+
Offline writes *are* currently accepted by Zero, and sent when you come back online. But this is only because we haven't written the code to disable them yet, which we plan to before beta.
78

8-
Until then, we recommend that you detect when the user is offline using the `onOnlineChange` constructor parameter and disable write functionality in your app manually.
9+
It's a bug, not a feature.
10+
</Note>
911

10-
## More Information
12+
The lack of offline writes is often surprising to people familiar with sync engines, because offline is usually touted as something that comes for free with these tools.
1113

12-
Offline writes are a very complicated subject.
14+
This page explains why Zero doesn't support offline writes, why it is hard for *any* software system to automatically support offline writes, how we recommend you handle connectivity loss, and our future plans in this area.
1315

14-
While technically Zero can queue writes and replay them when reconnected (this happens by default in a sync engine), building apps that correctly support offline writes is very difficult.
16+
## Why Zero Doesn't Support Offline Writes
1517

16-
When offline for long periods, the actual *intent* of users can diverge resulting in complex conflicts that cannot automatically be resolved. Or, the server software and database schema may diverge from client such that offline mutations can no longer be processed. In either of these cases, simply replaying offline writes can result in arbitrarily large amount of user data loss.
18+
Offline writes are very complicated.
1719

18-
For these reasons, Zero is currently focused on the benefits that sync provides for the 99% of time most users and most apps are *online*. Specifically: instant user interactions and automatic reactivity. Providing a great *offline* experience is not currently a priority. We may revisit this in the future.
20+
While Zero can technically queue offline writes and replay them when reconnected (this happens by default in any sync engine, and is what Zero does today), that fact doesn't make supporting offline writes much easier. That's because a really hard part of offline writes is in handling conflicts, and no software tool can make that problem go away.
21+
22+
For example, imagine two users are editing an article about cats. One goes offline and does a bunch of work on the article, while the other decides that the article should actually be about dogs and rewrites it. When the offline user reconnects, there is no way that any software algorithm can automatically resolve their conflict. One or the other of them is going to be upset.
23+
24+
And while the above example may sound extreme, you can construct similar situations with the majority of common applications. Just take your own application and ask yourself what should really happen if one user takes their device offline for a week and makes arbitrarily complex changes while other users are working online.
25+
26+
People who work on sync engines and related tools often say that *offline is just extreme lag*, but that's only true at a technical level. At a human level, being "offline" for a few seconds is very different from being offline for a few hours. The difference is how much knowledge you have about what your collaborators are doing, and how much of your work can be lost.
27+
28+
## Offline Writes are a UX Problem…
29+
30+
The only way to support offline writes in general is to either:
31+
32+
1. Extremely carefully limit the data model so that all possible offline writes make sense when merged.
33+
2. Support custom UX to allow users to fork and merge conflicts when they occur.
34+
35+
Neither of these is *free*. Buiding a good offline UX is a lot of work, and most of that work is borne by application developers.
36+
37+
## … And a Schema Problem
38+
39+
But it's not just users that can diverge from each other. The server software and database schema can also diverge arbitrarily far from the client while the client is disconnected. When the client comes back online, the changes made may no longer be processable by the application, or may have a different effect than the user intended.
40+
41+
To support arbitrarily long offline periods, the server must also maintain backward compatibility with clients indefinitely.
42+
43+
Similarly, the server can never reject an offline write (i.e., due to validation) because that could lead to a user losing huge amounts of work.
44+
45+
## … And a Sync Engine Problem
46+
47+
Supporting offline writes also requires work in the sync engine.
48+
49+
In Zero, there are a few specific impacts:
50+
51+
1. The Zero client itself can get out of date while offline. On reconnect, the app might reload with a new version of the client. This new version must be able to read and process old data from arbitrarily long ago.
52+
2. An arbitrarily large number of pending mutations can be built up. These mutations must be replayed on reconnect, which can take a long time.
53+
3. When processing mutations on server we must consider what should happen if the database or application server are temporarily unavailable. We need to treat that kind of error differently from a validation error.
54+
55+
These problems are surmountable, but significant effort. Their solutions might also be in tension with other goals of the sync engine, like online performance and scalability. These tradeoffs will take time to work through.
56+
57+
## Zero's Position
58+
59+
For all of the above reasons, we plan to disable offline writes in Zero for beta.
60+
61+
When the Zero client loses connection to `zero-cache` for several minutes (or when `zero-cache` cannot reach the customer API server), it will enter a special *offline mode*. In this mode, all writes to Zero will throw.
62+
63+
While we recognize that offline writes would be useful for some applications, the reality is that for most of the apps we want to support, the user is online the vast majority of the time and the cost to support offline is extremely high. There is simply more value in making the online experience great first.
64+
65+
We would like to revisit this in the future and really think through how to design APIs and patterns that allow developers to make successful offline-enabled apps. But it's not our priority right now.
66+
67+
## Dealing with Offline Today
68+
69+
Until Zero disables offline writes automatically, we recomment using the `onOnlineChange` parameter to the `Zero` constructor to detect connection loss and disable writes manually.
1970

2071
## Even More Information
2172

lib/routes-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const ROUTES: EachRoute[] = [
3939
{ title: 'Authentication', href: '/auth' },
4040
{ title: 'Permissions', href: '/permissions' },
4141
{ title: 'ZQL on the Server', href: '/zql-on-the-server' },
42-
{ title: 'Offline Support', href: '/offline' },
42+
{ title: 'Offline', href: '/offline' },
4343
{ title: 'Deployment', href: '/deployment' },
4444
{ title: '`zero-cache` Config', href: '/zero-cache-config' },
4545
],

0 commit comments

Comments
 (0)