PhaseLock

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:

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:

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 app UI turns user updates into mutations. Each mutation forks: one branch goes over the network to decide(), whose edits are appended to the edit log and sent back to the client store; the other branch is predict() on the client, whose edits land in an overlay. The client store shows through the overlay, and the app UI queries the overlay. Life of a PhaseLock UI Update User updates are sent to the server as mutations. CLIENT SERVER OPTIMISTIC PATH Predicted edits update the UI immediately. DURABLE PATH Persisted edits are sent back to the client. App UI Mutations decide() predict() Edit Log Client Store Overlay

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:

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:

All you define is how to update state in response to a mutation, and PhaseLock does the rest.