惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
Vercel News
Vercel News
D
Docker
博客园 - 聂微东
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
D
DataBreaches.Net
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
N
Netflix TechBlog - Medium
G
Google Developers Blog
腾讯CDC

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
CRDTs for Offline-First Mobile Sync
SoftwareDevs · 2026-05-22 · via DEV Community

SoftwareDevs mvpfactory.io

---
title: "CRDTs for Mobile Sync: Automerge vs Yjs vs cr-sqlite"
published: true
description: "Compare Automerge, Yjs, and cr-sqlite for offline-first mobile sync. Benchmarks, merge semantics, and architecture patterns that eliminate conflict dialogs."
tags: mobile, architecture, kotlin, performance
canonical_url: https://blog.mvpfactory.co/crdts-for-mobile-sync-automerge-vs-yjs-vs-cr-sqlite
---

## What We're Building

Today we're walking through a practical comparison of three CRDT libraries — Automerge, Yjs, and cr-sqlite — so you can pick the right one for offline-first mobile sync. By the end, you'll understand how each one handles concurrent edits, what their sync payloads actually look like on the wire, and how they perform on real mobile hardware. No more conflict resolution dialogs. No more "which version do you want to keep?"

## Prerequisites

- Familiarity with offline-first architecture concepts
- Basic understanding of SQLite and document-based data models
- A Kotlin, Swift, or TypeScript project where you want to add local-first sync

## Step 1: Understand Why CRDTs Eliminate Conflict Dialogs

CRDTs (Conflict-free Replicated Data Types) guarantee that any two replicas receiving the same set of operations converge to the same state, regardless of operation order. Two families matter: state-based (CvRDTs) that ship full snapshots, and operation-based (CmRDTs) that ship individual ops. Modern libraries like Automerge and Yjs use hybrid approaches — encoding operations into compact binary formats and reconstructing state locally.

Here is the minimal setup to get this working: you need a way to order edits causally across devices. All three libraries rely on Hybrid Logical Clocks.

Enter fullscreen mode Exit fullscreen mode


kotlin
data class HLC(
val wallTime: Long, // millis since epoch
val counter: Int, // logical ticks
val nodeId: String // unique device identifier
) : Comparable {
override fun compareTo(other: HLC): Int =
compareValuesBy(this, other, { it.wallTime }, { it.counter }, { it.nodeId })
}


When two devices edit the same field concurrently, the HLC timestamps are incomparable. The CRDT's merge function kicks in with a deterministic tiebreaker (like highest `nodeId` wins). Users never see a dialog because the merge is automatic and identical across all replicas.

## Step 2: Compare the Three Libraries

Let me show you a pattern I use in every project — matching the library to the data shape.

| Dimension | Automerge 2.x | Yjs | cr-sqlite |
|---|---|---|---|
| Data model | Rich JSON documents (maps, lists, text) | Shared types (YMap, YArray, YText) | SQLite tables with CRDT columns |
| Merge granularity | Per-character / per-field | Per-character / per-field | Per-row / per-column |
| Language support | Rust core, WASM, JS, Swift, Kotlin (via FFI) | JS-native, Rust port (y-crdt) | C core, bindings for most platforms |
| Best fit | Collaborative documents, nested structures | Real-time text, lightweight state | Apps already on SQLite, relational data |

Automerge has the richest document model. Yjs has the lowest memory footprint. cr-sqlite fits teams already invested in SQLite.

## Step 3: Measure What Matters on Mobile

The docs don't mention this, but most teams benchmark CRDT size at rest and ignore sync cost. The sync architecture matters more than you'd think.

- **Yjs** uses a state vector (`{clientId: clock}` pairs). Typical sync payload for a 10KB document with 50 pending ops: ~2–4 KB.
- **Automerge** uses Bloom filters to identify missing changes. Slightly higher overhead per round-trip but fewer rounds for large change sets.
- **cr-sqlite** piggybacks on SQLite's replication patterns, shipping row-level diffs via a per-row version clock.

Here is the gotcha that will save you hours — memory on device:

| Scenario (10K ops) | Automerge 2.x | Yjs | cr-sqlite |
|---|---|---|---|
| Heap memory | ~8–12 MB | ~3–5 MB | ~2–4 MB |
| Load time (cold) | ~120–200 ms | ~40–80 ms | ~20–50 ms |
| GC pressure | Medium | Low | Minimal |

cr-sqlite wins on memory because SQLite manages its own page cache outside the Kotlin/JS heap. For Android Go phones with 2 GB RAM, cr-sqlite or Yjs are safer choices. Automerge is the better pick when your data model is deeply nested and document-shaped.

## Gotchas

- **Operation history grows unbounded.** CRDTs trade storage for conflict freedom. Without compaction, memory on low-end devices degrades over weeks. Use Automerge's `clone()` to strip history, Yjs's `encodeStateAsUpdate` for snapshots, or cr-sqlite's version pruning.
- **Don't force relational data into a document CRDT** (or vice versa). Foreign keys and joins point to cr-sqlite. Nested maps and lists point to Automerge. Lightweight collaborative state points to Yjs.
- **Prototype your sync loop first.** Stand up a minimal WebSocket relay, simulate 48 hours of offline edits on two devices, and measure actual payload sizes on your real data model. The benchmarks above are directional — your mileage depends on edit patterns.
- **Automerge FFI overhead on native mobile adds latency.** The Rust/WASM core avoids GC on web, but crossing the FFI boundary on Android or iOS has measurable cost on cold loads.

## Wrapping Up

Local-first has matured. With CRDT libraries available across Kotlin, Swift, and TypeScript, the engineering cost of eliminating conflict dialogs has dropped below the cost of building a custom server-authoritative sync layer. Match the library to your data shape, budget for history compaction, and prototype before you commit. I'd make the switch on any new project that handles user-generated content offline.

Enter fullscreen mode Exit fullscreen mode