| [ Web Proxy ] |
| Viewing: https://developers.cloudflare.com/workers/platform/storage-options/ | [Back] [Original] |
This guide describes the storage & database products available as part of Cloudflare Workers, including recommended use-cases and best practices.
The following table maps our storage & database products to common industry terms as well as recommended use-cases:
| Use-case | Product | Ideal for |
|---|---|---|
| Key-value storage | Workers KV | Configuration data, service routing metadata, personalization (A/B testing) |
| Object storage / blob storage | R2 | User-facing web assets, images, machine learning and training datasets, analytics datasets, log and event data. |
| Accelerate a Postgres or MySQL database | Hyperdrive | Connecting to an existing database in a cloud or on-premise using your existing database drivers & ORMs. |
| Global coordination & stateful serverless | Durable Objects | Building collaborative applications; global coordination across clients; real-time WebSocket applications; strongly consistent, transactional storage. |
| Lightweight SQL database | D1 | Relational data, including user profiles, product listings and orders, and/or customer data. |
| Task processing, batching and messaging | Queues | Background job processing (emails, notifications, APIs), message queuing, and deferred tasks. |
| Vector search & embeddings queries | Vectorize | Storing embeddings from AI models for semantic search and classification tasks. |
| Streaming ingestion | Pipelines | Streaming data ingestion and processing, including clickstream analytics, telemetry/log data, and structured data for querying |
| Time-series metrics | Analytics Engine | Write and query high-cardinality time-series data, usage metrics, and service-level telemetry using Workers and/or SQL. |
Applications can build on multiple storage & database products: for example, using Workers KV for session data; R2 for large file storage, media assets and user-uploaded files; and Hyperdrive to connect to a hosted Postgres or MySQL database.
Pages Functions
Storage options can also be used by your front-end application built with Cloudflare Pages. For more information on available storage options for Pages applications, refer to the Pages Functions bindings documentation.
There are three options for SQL-based databases available when building applications with Workers.
We recommend using Workers KV for storing session data, credentials (API keys), and/or configuration data. These are typically read at high rates (thousands of RPS or more), are not typically modified (within KV's 1 write RPS per unique key limit), and do not need to be immediately consistent.
Frequently read keys benefit from KV's internal cache, and repeated reads to these "hot" keys will typically see latencies in the 500s to 10ms range.
Authentication frameworks like OpenAuth use Workers KV as session storage when deployed to Cloudflare, and Cloudflare Access uses KV to securely store and distribute user credentials so that they can be validated as close to the user as possible and reduce overall latency.
Workers KV is an eventually consistent key-value data store that caches on the Cloudflare global network.
It is ideal for projects that require:
To get started with KV:
R2 is S3-compatible blob storage that allows developers to store large amounts of unstructured data without egress fees associated with typical cloud storage services.
It is ideal for projects that require:
To get started with R2:
Durable Objects provide low-latency coordination and consistent storage for the Workers platform through global uniqueness and a transactional storage API.
Global Uniqueness guarantees that there will be a single instance of a Durable Object class with a given ID running at once, across the world. Requests for a Durable Object ID are routed by the Workers runtime to the Cloudflare data center that owns the Durable Object.
The transactional storage API provides strongly consistent key-value storage to the Durable Object. Each Object can only read and modify keys associated with that Object. Execution of a Durable Object is single-threaded, but multiple request events may still be processed out-of-order from how they arrived at the Object.
It is ideal for projects that require:
To get started with Durable Objects:
D1 is Cloudflares native serverless database. With D1, you can create a database by importing data or defining your tables and writing your queries within a Worker or through the API.
D1 is ideal for:
To get started with D1:
Note
If your working data size exceeds 10 GB (the maximum size for a D1 database), consider splitting the database into multiple, smaller D1 databases.
Cloudflare Queues allows developers to send and receive messages with guaranteed delivery. It integrates with Cloudflare Workers and offers at-least once delivery, message batching, and does not charge for egress bandwidth.
Queues is ideal for:
To get started with Queues:
Hyperdrive is a service that accelerates queries you make to MySQL and Postgres databases, making it faster to access your data from across the globe, irrespective of your users location.
Hyperdrive allows you to:
To get started with Hyperdrive:
Pipelines is a streaming ingestion service that allows you to ingest high volumes of real time data, without managing any infrastructure.
Pipelines allows you to:
To get started with Pipelines:
Analytics Engine is Cloudflare's time-series and metrics database that allows you to write unlimited-cardinality analytics at scale using a built-in API to write data points from Workers and query that data using SQL directly.
Analytics Engine allows you to:
Cloudflare uses Analytics Engine internally to store and product per-product metrics for products like D1 and R2 at scale.
To get started with Analytics Engine:
Vectorize is a globally distributed vector database that enables you to build full-stack, AI-powered applications with Cloudflare Workers and Workers AI.
Vectorize allows you to:
To get started with Vectorize:
Cloudflare Workers offers a SQLite-backed serverless database product - D1. How should you compare SQLite in Durable Objects and D1?
D1 is a managed database product.
D1 fits into a familiar architecture for developers, where application servers communicate with a database over the network. Application servers are typically Workers; however, D1 also supports external, non-Worker access via an HTTP API , which helps unlock third-party tooling support for D1.
D1 aims for a "batteries included" feature set, including the above HTTP API, database schema management, data import/export, and database query insights.
With D1, your application code and SQL database queries are not colocated which can impact application performance. If performance is a concern with D1, Workers has Smart Placement to dynamically run your Worker in the best location to reduce total Worker request latency, considering everything your Worker talks to, including D1.
SQLite in Durable Objects is a lower-level compute with storage building block for distributed systems.
By design, Durable Objects are accessed with Workers-only.
Durable Objects require a bit more effort, but in return, give you more flexibility and control. With Durable Objects, you must implement two pieces of code that run in different places: a front-end Worker which routes incoming requests from the Internet to a unique Durable Object, and the Durable Object itself, which runs on the same machine as the SQLite database. You get to choose what runs where, and it may be that your application benefits from running some application business logic right next to the database.
With SQLite in Durable Objects, you may also need to build some of your own database tooling that comes out-of-the-box with D1.
SQL query pricing and limits are intended to be identical between D1 (pricing, limits) and SQLite in Durable Objects (pricing, limits).
| Web Proxy Viewer | New URL | Original Page |