Simon Willison’s Weblog: Zero-latency SQLite storage in every Durable Object

Source URL: https://simonwillison.net/2024/Oct/13/zero-latency-sqlite-storage-in-every-durable-object/#atom-everything
Source: Simon Willison’s Weblog
Title: Zero-latency SQLite storage in every Durable Object

Feedly Summary: Zero-latency SQLite storage in every Durable Object
Kenton Varda introduces the next iteration of Cloudflare’s Durable Object platform, which recently upgraded from a key/value store to a full relational system based on SQLite.
This is a fascinating piece of distributed system design, advocating for a really interesting way to architect a large scale application.
The key idea behind Durable Objects is to colocate application logic with the data it operates on. A Durable Object comprises code that executes on the same physical host as the SQLite database that it uses, resulting in blazingly fast read and write performance.
How could this work at scale?

A single object is inherently limited in throughput since it runs on a single thread of a single machine. To handle more traffic, you create more objects. This is easiest when different objects can handle different logical units of state (like different documents, different users, or different “shards" of a database), where each unit of state has low enough traffic to be handled by a single object

Kenton presents the example of a flight booking system, where each flight can map to a dedicated Durable Object with its own SQLite database – thousands of fresh databases per airline per day.
Each DO has a unique name, and Cloudflare’s network then handles routing requests to that object wherever it might live on their global network.
The technical details are fascinating. Inspired by Litestream, each DO constantly streams a sequence of WAL entries to object storage – batched every 16MB or every ten seconds. This also enables point-in-time recovery for up to 30 days through replaying those logged transactions.
To ensure durability beyond that ten second window, writes are also forwarded to five replicas in separate nearby data centers as soon as they commit, and the write is only acknowledged once three of them have confirmed it.
The JavaScript API design is interesting too: it’s blocking rather than async, because the whole point of the design is to provide fast single threaded persistence operations:
let docs = sql.exec(`
SELECT title, authorId FROM documents
ORDER BY lastModified DESC
LIMIT 100
`).toArray();

for (let doc of docs) {
doc.authorName = sql.exec(
"SELECT name FROM users WHERE id = ?",
doc.authorId).one().name;
}
This one of their examples deliberately exhibits the N+1 query pattern, because that’s something SQLite is uniquely well suited to handling.
Via lobste.rs
Tags: software-architecture, sqlite, cloudflare, litestream

AI Summary and Description: Yes

**Summary:** The text discusses the innovative upgrades to Cloudflare’s Durable Object platform, emphasizing the integration of zero-latency SQLite storage. This approach enhances performance through colocating application logic with its data, offering significant scalability and durability improvements relevant for cloud computing architectures.

**Detailed Description:**
The content elaborates on the advancements in Cloudflare’s Durable Objects, which transition from key/value storage to a more sophisticated relational database system utilizing SQLite. This shift facilitates rapid data processing and retrieval, making it particularly significant for applications requiring swift, reliable database interactions. The approach showcases a new paradigm in the architectural design of large-scale cloud applications.

Key points include:

– **Colocation of Application Logic and Data:**
– Durable Objects are designed to execute code on the same physical host as the SQLite database, resulting in high-speed read and write operations.

– **Scalability Challenges:**
– A single Durable Object has throughput limitations as it operates on a single thread. To manage increased traffic, developers can create multiple Durable Objects.
– This is efficiently implemented when different objects handle disparate logical units of state, such as individual documents or user data.

– **Real-world Application Example:**
– The flight booking system illustrates the concept, with each flight assigned to a separate Durable Object and its own SQLite database, allowing for thousands of databases to be generated daily for various airlines.

– **Technical Innovations:**
– Durable Objects leverage a system inspired by Litestream for streaming write-ahead logging (WAL) entries to object storage, ensuring durability and enabling point-in-time recovery for logged transactions up to 30 days.
– Write acknowledgments are robust; they require confirmation from three of five replicas located in separate data centers after each commit.

– **JavaScript API Design:**
– The API operates synchronously to enhance single-threaded persistence operations, allowing easy querying with a focus on utilizing SQLite’s strengths, even exhibiting patterns like the N+1 query for demonstration purposes.

This architectural shift is significant for security and compliance professionals as it suggests an approach to enhance data persistence while maximizing performance in cloud environments, which could influence compliance with data retention and recovery standards.