Loro Now Open Source: Reimagine State Management with CRDTs
Loro, our high-performance CRDTs library, is now open source
.
In this article, we share our vision for the local-first software development paradigm,
explain why we’re excited about it, and discuss the current status of Loro.
With better DevTools, documentation, and a friendly ecosystem, everyone can easily
build local-first software.
You can build collaborative apps with time travel features easily using Loro.
Play the example online (opens in a new tab).
Envisioning the Local-First Development Paradigm
Distributed states are commonly found in numerous scenarios, such as multiplayer
games, multi-device document synchronization, and edge networks. These scenarios
require synchronization to achieve consistency, usually entailing elaborate design
and coding. For instance, considerations for network issues or concurrent write operations
are necessary. However, for a wide range of applications CRDTs can simplify the code significantly:
- CRDTs can automatically merge concurrent writes without conflicts.
- Fewer abstractions. There’s no need to design specific backend database schemas,
manually execute expected conflict merges, or implement interfaces to memory and memory to
persistent structure conversions. - Offline supports are right out of the box
What are CRDTs
When you can’t use CRDTs
Since the data resides locally, client applications can directly access and
manipulate local data, offering both speed and availability. Additionally,
due to CRDTs’ nature, synchronization / real-time collaboration can be achieved without relying on
centralized servers (similar to Git, allowing migration to other platforms
without data loss). With performance improvements, CRDTs increasingly
replace traditional real-time collaboration solutions in various contexts.
This represents a new paradigm. Local-first not only empowers users with control over
their data, but also makes developers’ lives easier.
The annual growth rate of the “local-first” star count in GitHub has reached 40%+.
Integrating CRDTs with UI State Management
Loro’s rich text collaboration example
Since CRDTs enable conflict-free automatic merging, the challenge with using
CRDTs shifts to “how to express operations and states on CRDTs”.
Front-end state management libraries often necessitate defining the retrieval of
State
and the specification of Actions
, as illustrated by this example from
Vue’s state management tool, Pinia:
export const useCartStore = defineStore({
id: "cart",
state: () => ({
rawItems: [] as string[],
}),
getters: {
items: (state): Array<{ name: string; amount: number }> =>
state.rawItems.reduce((items, item) => {
const existingItem = items.find((it) => it.name === item);
if (!existingItem) {
items.push({ name: item, amount: 1 });
} else {
existingItem.amount++;
}
return items;
}, [] as Array<{ name: string; amount: number }>),
},
actions: {
addItem(name: string) {
this.rawItems.push(name);
},
removeItem(name: string) {
const i = this.rawItems.lastIndexOf(name);
if (i > -1) this.rawItems.splice(i, 1);
},
async purchaseItems() {
const user = useUserStore();
if (!user.name) return;
console.log("Purchasing", this.items);
const n = this.items.length;
this.rawItems = [];
return n;
},
},
});
This paradigm and CRDTs are easily compatible: The state in the state management
libraries corresponds to CRDT types, and Action corresponds to a set of
CRDT operations.
Thus, implementing UI state management through CRDTs does not require users to
change their habits. It also has many advanced features:
- Make states automatically synchronizable / support real-time
collaboration. - Like Git, maintain a complete distributed editing history.
- It can store an extensively large editing history with a low memory footprint
and a compact encoding size. Below is an example.
With this, you can effortlessly implement products with real-time / async collaboration
and time machine features.
Time travel a document with 360,000+ operations using Loro. To load the whole history and playback, it only takes 8.4MB in memory. And the entire history only takes 361KB in storage.
The editing trace is from
.
Introduction to Loro
Loro is our CRDTs library, now open-sourced under a permissive license. We believe a
cooperative and friendly open-source community is key to creating outstanding
developer experiences.
We aim to make Loro simple to use, extensible, and maintain high performance.
The following is the latest status of Loro.
CRDTs
We have explored extensively, supporting a range of CRDT algorithms that have
yet to be widely used.
OT-like CRDTs
Our CRDTs library is built on the brilliant concept
{span>{span>
Read More