Optimistic updates should be free
PhaseLock is an open-source backend for live apps that still work offline. You declare your types and write your business logic once, then PhaseLock's sync engine powers your web app, your mobile app, and even your backend workers.
Imagine this nightmarish UX: a user updates a text field, then hits enter. The text immediately returns to the old value. Then a half second later, it snaps back to the new value. This flicker happens after every single user action.
That's exactly what happens when an app built on a sync engine doesn't solve the "optimistic updates" problem.
A sync engine is supposed to cache server state locally and render from that cache. As server updates arrive, the cache updates and so does the UI. But if the app only renders from the cache, then the client can't update the UI without a network round trip, causing the update flicker.
"Optimistic updates" means that what renders in the UI is a combination of known server state plus the predicted effects of any in-flight client mutations.
Additionally, if multiple clients submit conflicting sequences of mutations, then one of them will lose and have to roll back the effect of its failed mutations in its UI. And if it had multiple mutations in flight, it might need to know how to rebase its subsequent mutations against the new server state.
This situation is surprisingly similar to a bad git merge conflict, both in mechanism and in how bad it hurts your brain to think about it:
- How will my server resolve these conflicts?
- How will my client accurately mirror what the server does?
- How will my UI roll back the failed changes?
- How can I handle all those cases and get them right?
In PhaseLock, all of these situations are resolved automatically using business logic you already had to write.
The predictor function
Every mutation's effect is decided by a server-side decider function.
Optimistic updates require a new function, a client-side predictor function. The predictor's job is to predict what the decider will do. Its predictions are based on the client's state, so the predicted effects may differ from what the server eventually produces.
"How can you claim optimistic updates are resolved automatically if I need to write a new function?" you ask. Great question. It turns out the extra function usually requires only a refactor of existing business logic.
Think of it this way. Processing a mutation requires answering two questions:
- authorization: is the sender allowed to send this mutation?
- effect: how should state be updated in response to this mutation?
For any given mutation, the predictor only answers the effect, while the decider must answer both. So usually the decider is just an auth check followed by a call to the predictor function; no new logic is needed.
Two paths for every update
In practice, each UI update takes both a fast optimistic path and a slow durable path:
The optimistic path completes locally with zero latency. A PhaseLock app's UI shows the predicted effect of a user action immediately. Even offline mode works, because offline mode is just optimistic updates in front of a pathologically slow network.
So the implementation is roughly:
- Mutations are created from user actions and sent to the server.
- The predictor makes predicted edits to an overlay. The UI reflects optimistic state.
- On the server, the decider makes authoritative edits, which are saved to the edit log.
- On the client, now-persisted edits arrive and are applied to the client store.
- The old overlay is discarded. The UI now reflects durable state.
If multiple mutations were in flight and only the first is resolved, or if unrelated edits arrive from the server, then the old overlay is discarded and the predictor re-predicts for the remaining in-flight mutations against the updated client store. A new overlay is built and the UI reflects an updated optimistic state.
Conclusion
Now we have enough detail to answer every difficult question from before:
- How will my server resolve these conflicts? It runs the decider against incoming mutations one at a time, applying or rejecting each mutation in order.
- How will my client accurately mirror what the server does? It runs the predictor, which is typically the same effect logic inside the decider, against the client's latest-known server state.
- How will my UI roll back the failed changes? The UI never needs to "roll back" anything; it reflects the current state of the client store plus the overlay, and the overlay can be cheaply discarded and rebuilt.
- How can I handle all those cases and get them right? You never need to!
All you define is how to update state in response to a mutation, and PhaseLock does the rest.