📝Use Distributed State Machines to handle centralized requests in CRDT
When working with CRDT or local-first databases, sometimes a centralized processing is needed (e.g., handling complex validations).
One way to handle that is with “Distributed State Machines.”
Instead of updating their user name directly, the client stores an updateUserName
request in the database.
{
"machine": "updateUserName",
"state": "requestedUpdate",
"request": {
"name": "Fred",
"id": 123,
"timestamp": 1694122496
},
"response": {}
}
// User 123
{
"name": "Bob",
"id": 123
}
Once it is synced to the server, the server processes it and either fulfills or rejects the request:
{
"machine": "updateUserName",
"state": "finished",
"request": {
"name": "Fred",
"id": 123,
"timestamp": 1694122496
}
"response": {
"error": false,
"timestamp": 1694122996
}
}
// User 123
{
"name": "Fred",
"id": 123
}
Pros
Backend can perform additional validations or call out into external system to process the request
All concurrent requests may be exposed to other clients to either show global progress or optimistic updates.
Backlinks
- 📝 CRDT