SQLite in WebAssembly: Running Databases in the Browser with OPFS and Web Workers
Persistent, High Performance Databases Directly Inside the Browser
For many years, running a real database inside a web browser sounded unrealistic. Browsers were designed for stateless interactions, temporary storage, and simple key value stores. That has changed.
With WebAssembly, modern browser storage APIs, and multi threaded execution through Web Workers, SQLite can now run entirely inside the browser with performance and durability that rivals native applications.
In this blog, we explore how SQLite works in WebAssembly, how Origin Private File System (OPFS) enables persistent storage, and how Web Workers allow safe multi threaded database operations.
Why Run SQLite in the Browser
Browser based SQLite unlocks entirely new application architectures.
Common use cases include:
Offline first web applications
Data intensive dashboards
Client side analytics
Secure local storage without servers
Progressive web apps with complex state
Instead of pushing all data to a backend, applications can process and store data locally while remaining fully functional offline.
This approach aligns naturally with offline first strategies discussed in
Building Offline-First Applications with SQLite.
Understanding SQLite WebAssembly Builds
SQLite provides official WebAssembly builds that compile the core database engine into WASM. This allows SQLite to execute inside the browser sandbox with near native performance.
Key characteristics of SQLite WASM:
Runs entirely client side
No server required
Uses browser provided storage
Secure by default
The database engine behaves exactly like native SQLite, except that file access is virtualized.
Introducing OPFS for Persistent Storage
OPFS, or Origin Private File System, is a browser storage API that provides a private, persistent file system per origin.
Why OPFS matters:
Data persists across page reloads
Storage is isolated per website
Files behave like real files
Performance is significantly better than IndexedDB
SQLite can store its database file directly inside OPFS, making browser databases reliable and durable.
Creating a SQLite Database Using OPFS
Conceptual example using SQLite WASM:
const db = new SQLite3.oo1.DB({
filename: "mydb.sqlite",
vfs: "opfs"
});
db.exec(`
CREATE TABLE notes (
id INTEGER PRIMARY KEY,
content TEXT,
created_at TEXT
);
`);This database now lives entirely in the browser and persists across sessions.
If you are familiar with SQLite file based storage from native environments, the mental model remains the same.
Querying and Updating Data in the Browser
Once initialized, SQLite behaves like a normal SQL database.
db.exec({
sql: "INSERT INTO notes (content, created_at) VALUES (?, ?)",
bind: ["First note", new Date().toISOString()]
});Querying:
db.exec({
sql: "SELECT * FROM notes",
callback: (row) => {
console.log(row);
}
});This enables rich client side data interactions without network latency.
Using Web Workers for Multi Threaded Access
Browsers are single threaded by default, but Web Workers allow background threads.
SQLite in the browser typically runs inside a dedicated Worker to avoid blocking the UI.
Benefits:
Non blocking queries
Safe concurrency
Responsive interfaces
Example Worker setup:
const worker = new Worker("sqlite-worker.js");
worker.postMessage({
action: "query",
sql: "SELECT COUNT(*) FROM notes"
});Inside the worker, SQLite processes queries independently of the UI thread.
This mirrors concurrency strategies used in native applications and complements concepts from Optimizing SQLite for Multi User Applications.
Handling Concurrency Safely
Even in the browser, SQLite enforces locking rules.
Best practices:
Single writer pattern
Serialized access via worker
Avoid sharing database instances across threads
WAL mode is often enabled internally to allow concurrent reads.
PRAGMA journal_mode = WAL;This allows reads to continue while writes occur, even in browser environments.
Client Side Analytics and Aggregations
SQLite in WASM excels at local analytics.
Example:
SELECT DATE(created_at), COUNT(*)
FROM notes
GROUP BY DATE(created_at);This enables dashboards and reports without sending raw data to servers.
If you are interested in analytics focused designs,
Real-Time Analytics with SQLite provides deeper insight into aggregation patterns.
Security and Privacy Benefits
Browser based SQLite offers strong privacy guarantees.
Advantages:
Data never leaves the device
No backend breaches
Origin isolated storage
Works with encryption extensions
This is particularly useful for sensitive applications like journaling, finance, or healthcare tools.
Limitations to Consider
Despite its power, SQLite WASM has limits.
Things to watch for:
Browser storage quotas
Memory limits
No direct file system access
Worker communication overhead
These constraints are acceptable for most client side applications but should be considered early.
Real World Example: Offline Knowledge Base
A documentation platform uses SQLite WASM to store thousands of articles locally.
Features:
Instant full text search
Offline reading
Client side filtering
Background sync
Users get native app like performance without installing anything.
When SQLite in WebAssembly Makes Sense
SQLite in the browser is ideal when:
Offline functionality is critical
Data processing happens locally
Network access is unreliable
Privacy is a priority
It may not be suitable for:
Massive multi user collaboration
Server authoritative systems
High frequency real time updates
Conclusion
SQLite in WebAssembly represents a major shift in how web applications can be built. By combining SQLite WASM builds, OPFS persistence, and Web Workers, developers can run fully featured databases directly in the browser.
This approach brings native application capabilities to the web, reduces backend complexity, and enables truly offline first experiences.
SQLite continues to prove that lightweight does not mean limited. Even inside the browser, it remains a powerful foundation for modern applications.
Subscribe Now
Stay ahead with the latest SQLite tutorials, advanced examples, and real-world projects. Subscribe to SQLite Forum for hands-on guides, coding tips, and expert advice, straight to your inbox. Join our growing community of developers exploring SQLite’s full potential!


