Turso & libSQL - Edge-Ready SQLite

2024.12.30

What is Turso

Turso is an edge database service based on libSQL (a fork of SQLite). It combines SQLite’s lightweight nature with replication at edge locations to achieve ultra-low latency data access.

What is libSQL

libSQL is an open-source project that forked SQLite. While maintaining SQLite compatibility, it adds features like replication, WebSocket, and HTTP support.

Features

✓ SQLite compatible
✓ Edge replication
✓ Embedded Replicas (in-app SQLite)
✓ Low latency (edge proximity)
✓ Serverless optimized
✓ Generous free tier

Setup

Turso CLI

# Installation
curl -sSfL https://get.tur.so/install.sh | bash

# Login
turso auth login

# Create database
turso db create my-app

# Connect with shell
turso db shell my-app

JavaScript/TypeScript

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

const client = createClient({
  url: 'libsql://my-app-username.turso.io',
  authToken: process.env.TURSO_AUTH_TOKEN
});

// Execute query
const result = await client.execute('SELECT * FROM users');

// Query with parameters
const user = await client.execute({
  sql: 'SELECT * FROM users WHERE id = ?',
  args: [1]
});

// Transaction
const tx = await client.transaction('write');
await tx.execute('INSERT INTO users (name) VALUES (?)', ['Alice']);
await tx.execute('INSERT INTO users (name) VALUES (?)', ['Bob']);
await tx.commit();

Embedded Replicas

Have SQLite replicas within your application for ultra-fast reads.

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

const client = createClient({
  url: 'file:local.db',  // Local replica
  syncUrl: 'libsql://my-app-username.turso.io',  // Remote primary
  authToken: process.env.TURSO_AUTH_TOKEN,
  syncInterval: 60  // Sync every 60 seconds
});

// Read from local replica (ultra-fast)
const users = await client.execute('SELECT * FROM users');

// Explicitly execute sync
await client.sync();

Benefits of Embedded Replicas

Read Latency:
- Remote DB: 50-100ms
- Embedded Replica: 1-5ms

Use Cases:
- Cloudflare Workers
- Vercel Edge Functions
- Mobile apps
- Desktop apps

Drizzle ORM Integration

// schema.ts
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';

export const users = sqliteTable('users', {
  id: integer('id').primaryKey({ autoIncrement: true }),
  name: text('name').notNull(),
  email: text('email').notNull().unique()
});

// db.ts
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';
import * as schema from './schema';

const client = createClient({
  url: process.env.TURSO_DATABASE_URL!,
  authToken: process.env.TURSO_AUTH_TOKEN
});

export const db = drizzle(client, { schema });

// Usage
const allUsers = await db.select().from(users);

Edge Locations

Turso Edge Locations:
- North America: 10+
- Europe: 10+
- Asia: 5+
- Oceania: 2+

→ Responds from the location closest to the user

Group Configuration

# Create location group
turso group create my-group --location nrt  # Tokyo

# Add database to group
turso db create my-db --group my-group

# Add replicas
turso group locations add my-group sin  # Singapore
turso group locations add my-group lax  # Los Angeles

Branch Feature

# Create branch
turso db create my-db --from-db production-db

# Develop/test on branch
turso db shell my-db
# Insert test data...

# Merge to production (manual)

Usage with Cloudflare Workers

// wrangler.toml
// [vars]
// TURSO_URL = "libsql://my-app-username.turso.io"

// src/index.ts
import { createClient } from '@libsql/client/web';

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const client = createClient({
      url: env.TURSO_URL,
      authToken: env.TURSO_AUTH_TOKEN
    });

    const result = await client.execute('SELECT * FROM users LIMIT 10');
    return Response.json(result.rows);
  }
};

Pricing

Free Plan:
- 9GB storage
- 500 million rows read/month
- 5 million rows write/month
- 500 databases

Pro Plan ($29/month):
- 24GB storage
- Unlimited reads
- Unlimited databases

Summary

Turso is a database service that combines SQLite’s lightweight nature with edge computing benefits. Ultra-low latency reads with Embedded Replicas are ideal for edge-based applications.

← Back to list