3 ms·
Yes! I have long felt like there are CRDTs possible that maintain complex application invariants (that is, have transactions). But when I sat down to think abo
by aboodman 4y ago
Yes! I have long felt like there are CRDTs possible that maintain complex application invariants (that is, have transactions).
But when I sat down to think about it I realized that CRDTs make the problem far harder than it needs to be for today's software. CRDTs are designed for when there is no central authority, where all nodes are peers. But most software is not peer-to-peer, it's client/server and does in fact have a central authority -- the server.
This is how https://replicache.dev/ https://replicache.dev/ started. It's a CRDT inspired system, but takes advantage of the server to provide much stronger guarantees. Replicache mutations are transactional and enable enforcing arbitrary application invariants.
For example enforcing "only one meeting can be booked in each room at a time" is trivial in Replicache. You can just say:
async function createReservation(tx, reservationID, roomID, time) {
const existingReservation = await findReservationByTimeAndRoom(tx, time, roomID);
const res = {
success: existingReservation == null,
roomID,
time,
};
await tx.put(reservationID, res);
}
This function runs automatically on the client and the server. First it runs on the client optimistically. The UI updates immediately. In the background it runs again on the server, authoritatively. Results are synced to the client in the background and the client snaps to the server state.
You can also extend this easily to do fancier stuff like automatically reserve a similar room at the same time.
It's very difficult to do things like this in a CRDT.
I am still excited about the work that e.g., Automerge is doing and follow it closely. But if you are trying to add realtime collaboration and/or offline operation to a classic SaaS style application, I think a tool built for client/server will often be a better experience.
See https://doc.replicache.dev/how-it-works#conflict-resolution https://doc.replicache.dev/how-it-works#conflict-resolution