# Building Local-First Edge Apps with Turso Sync and Embedded Replicas

> Design patterns for microsecond local reads, offline-capable writes, and conflict-aware cloud sync using TursoDB and libSQL.
> Published: 2026-11-15
> Canonical URL: https://tursodb.cn/blog/local-first-turso-sync-guide/
> Source: TursoDB.cn Independent Developer Community

Users notice latency the moment an interaction crosses 100 milliseconds. When your app sits in Frankfurt or Tokyo while your primary database lives in Virginia, even a simple three-query page render burns half a second in pure network RTT.

## Pattern 1: Embedded Replicas for Web Servers

For SSR frameworks (Astro, Next.js, SvelteKit) running on long-lived containers or VPS nodes, **Embedded Replicas** place a synchronized SQLite file on the local NVMe drive:

```ts
import { createClient } from '@libsql/client';

const db = createClient({
  url: 'file:/var/lib/app/replica.db',
  syncUrl: process.env.TURSO_DATABASE_URL!,
  authToken: process.env.TURSO_AUTH_TOKEN!,
  syncInterval: 30,
});
```

- **Reads** hit the local disk in `< 1ms` (zero network hops).
- **Writes** route transparently to the primary and immediately reflect locally after completion.

## Pattern 2: True Local-First with Turso Sync

Desktop tools, mobile apps, and local coding agents cannot assume an always-on network connection. **Turso Sync** enables:

- **Offline local reads and writes** directly against the local SQLite engine.
- **Explicit or background push/pull** to synchronize frame deltas with Turso Cloud once connectivity returns.

## Key Engineering Tips

1. **Idempotent Migrations**: Keep schema migrations additive (`CREATE TABLE IF NOT EXISTS`, nullable new columns) so clients running slightly older app versions sync safely.
2. **Smart Sync Triggers**: Pair periodic intervals with explicit `sync()` calls on app foregrounding or after critical mutations.
3. **Separate Hot vs. Cold Data**: Store ephemeral caches in un-synced local tables and keep synced tables focused on durable user state.
