Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Durable Object Storage API: SQLite Backend, Key-Value, and Recovery

How attached storage works on Durable Objects: SQLite vs KV, ctx.storage SQL API, persistence limits, billing, and common mistakes.

This page summarizes Cloudflare documentation on the Durable Object Storage API—the transactional, strongly consistent storage attached to each Durable Object instance. Each object has private storage reached through ctx.storage on DurableObjectState. New classes should use the SQLite storage backend; legacy key-value–backed namespaces remain for existing deployments.

Durable Objects are a powerful compute API that provides a compute with storage building block. Each Durable Object has its own private, transactional, and strongly consistent storage. Durable Objects Storage API provides access to a Durable Object’s attached storage.

— Cloudflare, Access Durable Objects Storage

Storage API: why persist instead of only in-memory state

Section titled “Storage API: why persist instead of only in-memory state”

In-memory fields on a Durable Object are fast but are not a durable source of truth. The runtime can evict idle objects, restart them after deploys, or terminate an instance after an uncaught exception.

A Durable Object’s in-memory state is preserved as long as the Durable Object is not evicted from memory. Inactive Durable Objects with no incoming request traffic can be evicted. There are normal operations like code deployments that trigger Durable Objects to restart and lose their in-memory state. For these reasons, you should use Storage API to persist state durably on disk that needs to survive eviction or restart of Durable Objects.

— Cloudflare, Access Durable Objects Storage

Layer Speed Survives eviction? Typical use
In-memory (this.* properties) Fastest No Caches, connection handles, hot paths
ctx.storage (SQLite / KV) Fast, colocated Yes Primary application state
External bindings (R2, D1, KV namespace) Variable Yes Large blobs, cross-object shared data

Storage also has a built-in read cache: repeated get() calls can be served from memory without hitting disk, as described in In-memory state in a Durable Object.

SQLite-backed vs key-value storage backend: which API you get

Section titled “SQLite-backed vs key-value storage backend: which API you get”

Cloudflare recommends SQLite for all new Durable Object namespaces. SQLite-backed objects can still use the key-value API; they also get the SQL API and Point In Time Recovery (PITR).

Cloudflare recommends all new Durable Object namespaces use the SQLite storage backend. These Durable Objects can continue to use storage key-value API.

Additionally, SQLite-backed Durable Objects allow you to store more types of data (such as tables), and offer Point In Time Recovery API which can restore a Durable Object’s embedded SQLite database contents (both SQL data and key-value data) to any point in the past 30 days.

— Cloudflare, SQLite-backed Durable Object Storage

Capability SQLite-backed class KV-backed class (legacy)
SQL API (ctx.storage.sql) Yes No
PITR Yes No
Async KV (get / put / list) Yes Yes
Sync KV via ctx.storage.kv Yes No

Creating new KV-only namespaces is no longer supported for accounts without an existing KV-backed namespace; migration to SQLite is planned. On the Workers Free plan, only SQLite-backed Durable Objects are available (Durable Objects pricing).

Configure SQLite in Wrangler with a migration such as new_sqlite_classes, as shown in Rules of Durable Objects and Durable Object in-memory state example.

ctx.storage: access pattern and initialization

Section titled “ctx.storage: access pattern and initialization”

Storage methods live on the DurableObjectStorage interface, exposed as this.ctx.storage in the constructor. That includes SQL, KV, PITR, and alarms (Access Durable Objects Storage).

Storage API methods are available on ctx.storage parameter passed to the Durable Object constructor. Storage API has several methods, including SQL, point-in-time recovery (PITR), key-value (KV), and alarm APIs.

— Cloudflare, Access Durable Objects Storage

Only Durable Object classes with a SQLite storage backend can access SQL API.

— Cloudflare, Access Durable Objects Storage

Initialize instance variables from storage without races

Section titled “Initialize instance variables from storage without races”

A common pattern loads durable state once, then serves reads from instance fields on the same global object instance:

A common pattern is to initialize a Durable Object from persistent storage and set instance variables the first time it is accessed. Since future accesses are routed to the same Durable Object, it is then possible to return any initialized values without making further calls to persistent storage.

— Cloudflare, Access Durable Objects Storage

Use ctx.blockConcurrencyWhile() during constructor initialization so no requests run before tables or counters are ready—see the Counter example on Access Durable Objects Storage and Rules of Durable Objects. Reserve blockConcurrencyWhile() for startup, not per-request work, because it blocks all concurrency on that object.

SQL API: sql.exec, cursors, and TypeScript types

Section titled “SQL API: sql.exec, cursors, and TypeScript types”

On SQLite-backed classes, ctx.storage.sql is a SqlStorage handle. exec(query, ...bindings) returns a synchronous cursor you can iterate with for...of, .next(), .toArray(), .one(), or .raw().

The SqlStorage interface encapsulates methods that modify the SQLite database embedded within a Durable Object. The SqlStorage interface is accessible via the sql property of DurableObjectStorage class.

— Cloudflare, SQLite-backed Durable Object Storage

Consume cursors before await. Holding a cursor across an await does not give snapshot isolation; rows may change or reflect uncommitted work.

For predictable behavior, fully consume cursors synchronously before the next await, for example with .toArray() or Array.from(cursor).

— Cloudflare, SQLite-backed Durable Object Storage

rowsRead and rowsWritten on the cursor feed SQLite storage metering (SQLite-backed Durable Object Storage). Index updates count as extra rows written—worth remembering for write-heavy schemas (SQLite-backed Durable Object Storage).

TypeScript generics on exec<User>() improve editor hints only; they do not validate row shape at runtime:

Providing a type parameter does not validate that the query result matches your type definition.

— Cloudflare, Access Durable Objects Storage

For multi-statement atomicity on SQLite, prefer synchronous transactionSync() with SQL inside the callback, or rely on implicit transactions: writes without an intervening await coalesce atomically (KV-backed Durable Object Storage (Legacy) documents gates and coalescing for both backends). Do not run BEGIN TRANSACTION via raw sql.exec(); use transaction() / transactionSync() (SQLite-backed Durable Object Storage).

The Agents stack exposes the same embedded SQLite through a this.sql template tag on top of ctx.storage.sql (Agent class internals).

Key-value API on SQLite: put, gates, and allowUnconfirmed

Section titled “Key-value API on SQLite: put, gates, and allowUnconfirmed”

Even on SQLite, get() / put() store data in a hidden __cf_kv table (SQLite-backed Durable Object Storage). KV operations are billed as SQL row reads and writes on the Paid plan (Durable Objects pricing).

The runtime uses input gates and output gates so storage reads and writes do not interleave unsafely with other events, and clients do not see side effects before writes persist (Rules of Durable Objects, glossary in Glossary).

Optional put() flags from SQLite-backed Durable Object Storage:

  • allowUnconfirmed: allow outbound network messages before the write is confirmed on disk (trade safety for latency).
  • noCache: drop the key from memory after flush when it will not be read again soon.

Interleaving await between KV reads and writes breaks write coalescing and can leave half-finished transfers; SQL exec() in a tight synchronous sequence is often safer for read-modify-write (Rules of Durable Objects).

Per-object SQLite storage limits (Durable Objects limits, FAQs):

Plan Storage per Durable Object Account storage (SQLite)
Workers Paid 10 GB Unlimited
Workers Free 1 GB 5 GB total

SQL shape limits (columns per table, 2 MB row/string, 100 KB statement, etc.) are listed under SQL storage limits on Durable Objects limits.

When an object hits its cap, writes fail with SQLITE_FULL; reads and deletes still work:

When a SQLite-backed Durable Object reaches its maximum storage limit (10 GB on Workers Paid, or 1 GB on the Free plan), write operations … will fail with the following error: database or disk is full: SQLITE_FULL

— Cloudflare, FAQs

Empty SQLite files still consume minimal space (~12 KB for an empty DB; empty tables a few kilobytes)—relevant for billing and Free-tier caps (FAQs, Pricing).

SQLite storage billing on Paid plans is charged by rows read, rows written, and stored GB-month, with Free-tier daily caps (Durable Objects pricing). Storage billing for SQLite-backed objects was scheduled to turn on with a target of January 7, 2026 (Billing for SQLite Storage). Compute (requests and duration) has been billed separately since the SQLite beta (Durable Objects pricing).

Inspect usage from the Durable Objects page in the dashboard (Billing for SQLite Storage).

Data Studio, security, and local development

Section titled “Data Studio, security, and local development”

Data Studio is a dashboard UI to read and write SQLite-backed object data via Storage API. It works only for SQLite-backed objects; SQL API data is read/write today, while KV data in the same object may become read-only in the UI later (Data Studio). Editor access on the implementing Worker is required (Durable Objects roles and permissions).

Data at rest uses LUKS with AES-256; Worker–object traffic uses TLS (Data security).

Locally, Durable Object state persists under .wrangler/state/; reset with rm -rf .wrangler/state or npx wrangler dev --persist-to="" (Agents runtime configuration). Unlike KV or D1, there is no CLI to seed Durable Objects—you must call your object’s methods or use test harness storage helpers (Adding local data, Prepare test state).

Common mistakes with Durable Object storage

Section titled “Common mistakes with Durable Object storage”
  1. Treating in-memory fields as durable — eviction and deploys clear them; persist to ctx.storage (In-memory state in a Durable Object).
  2. One global Durable Object for all traffic — storage and CPU become a bottleneck; shard by room, user, or document ID (Rules of Durable Objects).
  3. await fetch() between storage check and storage write — input gates do not cover external I/O; use version checks or keep critical sections synchronous (Rules of Durable Objects).
  4. Cursors across await — unpredictable results; call .toArray() first (SQLite-backed Durable Object Storage).
  5. Calling SQL API on a KV-backed class — errors at runtime; migrate with new_sqlite_classes (SQLite-backed Durable Object Storage).
  6. Ignoring overload — exceptions with .overloaded should not be retried; spread load across more object IDs (Error handling, Troubleshooting).
  7. Reusing a broken stub — create a new stub after certain failures (Error handling).
  1. Confirm the class uses a SQLite migration (new_sqlite_classes in Wrangler) and that production deploy succeeded (Getting started, SQLite-backed Durable Object Storage).

  2. Exercise a write path (SQL INSERT or put), then read back via your Worker or RPC; for dashboard inspection, open Data Studio for the target SQLite-backed instance.

  3. On Workers Paid, review SQLite row and storage usage on the Durable Objects dashboard page before storage billing applies (Billing for SQLite Storage).

  4. In tests, seed and assert storage with getDurableObjectStorage() as in Prepare test state or follow Testing Durable Objects.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.