System Design

The Era of Edge Databases: Building Global Apps with Turso and Cloudflare D1

Master Edge Databases with Turso and Cloudflare D1. Learn physically distributed SQLite architecture, replica placement, and how to build globally fast full-stack apps.

Sachin Sharma
Sachin SharmaCreator
Feb 5, 2026
6 min read
The Era of Edge Databases: Building Global Apps with Turso and Cloudflare D1
Featured Resource
Quick Overview

Master Edge Databases with Turso and Cloudflare D1. Learn physically distributed SQLite architecture, replica placement, and how to build globally fast full-stack apps.

The Era of Edge Databases: Building Global Apps with Turso and Cloudflare D1

For 20 years, the architecture of the web was simple:

  1. 2.
    User is in India.
  2. 4.
    Server is in US-East (Virginia).
  3. 6.
    Database is in US-East (Virginia).

Every click the user makes has to travel halfway across the world and back. The speed of light is fast, but it’s not infinite. A round-trip from Delhi to Virginia takes 250ms. Add processing time, and your app feels sluggish.

We solved the "Static Content" problem with CDNs (Content Delivery Networks). Images and HTML are cached in Mumbai. We solved the "Compute" problem with Edge Functions (Cloudflare Workers, Vercel Edge). The code runs in Mumbai.

But the Database remained the bottleneck. Until now.

Welcome to the era of Edge Databases.

In this guide, we are looking at two technologies that are revolutionizing data access: Turso (based on LibSQL) and Cloudflare D1.


Part 1: Why SQLite? (The Resurrection)

People used to laugh at SQLite. "It's a toy DB for phones." They were wrong.

SQLite is the most widely deployed database engine in the world. It is robust, ACID-compliant, and incredibly fast. It failed on the server because it is a file-based database. It doesn't handle concurrent writes from multiple servers well.

The Innovation: Companies like ChiselStrike (Turso) and Cloudflare realized: "If we fork SQLite and add a networking layer, we can replicate the file globally."

Instead of one centralized Postgres server, you have 500 tiny SQLite replicas running in 500 cities.


Part 2: Turso (LibSQL) Architecture

Turso is built on LibSQL, a fork of SQLite that supports replication over HTTP.

The Primary-Replica Model

  1. 2.
    Primary: One location (e.g., Virginia). Handles all Writes.
  2. 4.
    Replicas: Hundreds of locations (e.g., Mumbai, Tokyo, London). Handle Reads.

When a user in Mumbai reads data:

  • The request hits a Mumbai Edge Worker.
  • The Worker queries the local Mumbai Replica.
  • Latency: 5ms. (Local IO).

When a user in Mumbai writes data:

  • The Worker sends the write to the Primary in Virginia.
  • The Primary writes and broadcasts the change to all replicas.
  • Latency: 250ms.

This is "Read-Heavy Optimization". Most apps are 95% reads (viewing profiles, reading blogs) and 5% writes (updating profile).

Code Example: Connecting to Turso

typescript
import { createClient } from "@libsql/client"; const client = createClient({ url: "libsql://my-db-mumbai.turso.io", authToken: process.env.TURSO_TOKEN, }); async function getProfile(id: string) { // This query runs in Mumbai! const rs = await client.execute({ sql: "SELECT * FROM users WHERE id = ?", args: [id], }); return rs.rows[0]; }

Part 3: Cloudflare D1 (The Native Approach)

D1 is Cloudflare's answer. It is built directly into the Workers platform.

The "Magic" of D1: It automatically manages read replication. You don't verify where the replica is. Cloudflare's "Smart Placement" moves the data closer to where the traffic is coming from.

Time Travel: D1 has built-in point-in-time recovery. It’s essentially a git repo for your data.

typescript
// worker.ts export interface Env { DB: D1Database; } export default { async fetch(request, env) { const { results } = await env.DB.prepare( "SELECT * FROM products WHERE category = ?" ) .bind("electronics") .all(); return Response.json(results); }, };

Part 4: Benchmarking Latency (Postgres vs Edge SQLite)

I built a simple API to test this. Setup:

  • User Location: Bangalore, India.
  • Postgres Database: AWS RDS (us-east-1).
  • Turso Database: Replica in Bangalore (bom).

Test 1: Fetch User Profile (SELECT * FROM users WHERE id = X)

  • AWS RDS: 280ms (Network RTT + Query).
  • Turso Edge: 12ms (Local Network + Query).

Improvement: 23x Faster.

This is not an optimization. This is a transformation. At 12ms, the data feels instant. It feels local.


Part 5: The "Write" Problem & Consistency

The catch is Eventual Consistency.

If I update my profile in Mumbai, and my friend in New York loads my profile 10ms later, he might see the old name. The replication takes time (usually < 1s).

For most features (Likes, Comments, Bio updates), this is acceptable. For some features (Billing, Inventory transfers), this is dangerous.

Strict Consistency Mode: Turso allows you to force a "Remote Read".

typescript
const client = createClient({ url: "libsql://primary.turso.io", // Connect to Primary ONLY authToken: "...", });

Use this for the payment page. Use the replica for the dashboard.


Part 6: Multi-Tenant Architecture

SQLite is just a file. This is a superpower for SaaS. Instead of one giant "Users" table with WHERE tenant_id = 5, you can just create one database per customer.

  • Customer A -> db_customer_a.sqlite
  • Customer B -> db_customer_b.sqlite

Turso supports creating 100,000+ databases on a single plan. This gives you physical data isolation. If Customer A's query is slow, Customer B is unaffected.

typescript
// Create a new DB for a new startup signing up await turso.databases.create("startup-xyz-db");

Part 7: Developer Experience (Migration)

Moving from Postgres to SQLite requires some changes.

  1. 2.
    No Enums: SQLite stores check constraints or text.
  2. 4.
    No Arrays: You store JSON strings.
  3. 6.
    Strict Typing: SQLite is "flexibly typed" by default, but D1 and Turso enforce stricter modes.

Using Drizzle ORM: Drizzle is the best ORM for Edge. It is lightweight and supports both D1 and LibSQL drivers natively.

typescript
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core"; export const users = sqliteTable("users", { id: integer("id").primaryKey(), name: text("name"), email: text("email").unique(), });

Conclusion: The Location-Aware Stack

We are moving away from "The Cloud" (a vague computer in Virginia) to "The Edge" (the computer down the street).

Edge Databases are the missing link. When you pair:

  1. 2.
    Next.js (Rendering on Vercel Edge).
  2. 4.
    Turso (Data on Edge Replicas).
  3. 6.
    R2/S3 (Assets on CDN).

You get an application that defies the speed of light limitations for 95% of interactions. It is complex? Slightly. Is it worth it? If you care about global users, absolutely.

Resources


About the Author: Sachin Sharma is a Systems Architect who specializes in distributed systems. He has migrated massive datasets to the Edge and improved global p99 latency by over 400%.

Sachin Sharma

Sachin Sharma

Software Developer & Mobile Engineer

Building digital experiences at the intersection of design and code. Sharing weekly insights on engineering, productivity, and the future of tech.