Cloudflare Data Products
Introduction
Section titled “Introduction”Cloudflare offers a suite of data storage products designed to work seamlessly with Cloudflare Workers at the edge. These products enable developers to build full-stack applications without managing traditional infrastructure.
D1: SQL Database at the Edge
Section titled “D1: SQL Database at the Edge”D1 is Cloudflare’s native serverless database built on SQLite. It allows you to run SQL queries at the edge with low latency.
Key Features
Section titled “Key Features”- SQLite Compatible: Use standard SQL syntax.
- Global Distribution: Data is replicated across Cloudflare’s network.
- Serverless: No servers to manage, automatic scaling.
- Low Latency: Queries execute close to your users.
Use Cases
Section titled “Use Cases”- User authentication and session management.
- E-commerce product catalogs.
- Content management systems.
Example
Section titled “Example”// Query D1 from a Cloudflare Workerexport default { async fetch(request, env) { const { results } = await env.DB.prepare( "SELECT * FROM users WHERE id = ?" ).bind(1).all(); return Response.json(results); }};Learn More
Section titled “Learn More”R2: Object Storage
Section titled “R2: Object Storage”R2 is Cloudflare’s object storage service, compatible with the S3 API. It offers zero egress fees, making it cost-effective for serving large files.
Key Features
Section titled “Key Features”- S3 Compatible: Use existing S3 SDKs and tools.
- Zero Egress Fees: No charges for data transfer out.
- Global Access: Low-latency access from anywhere.
- Automatic Replication: Data is replicated for durability.
Use Cases
Section titled “Use Cases”- Storing images, videos, and static assets.
- Backups and archives.
- Data lakes for analytics.
Example
Section titled “Example”// Upload a file to R2 from a Cloudflare Workerexport default { async fetch(request, env) { await env.MY_BUCKET.put("file.txt", "Hello, World!"); return new Response("File uploaded!"); }};Learn More
Section titled “Learn More”KV: Key-Value Store
Section titled “KV: Key-Value Store”Workers KV is a global, low-latency key-value data store. It’s optimized for high-read, low-write workloads.
Key Features
Section titled “Key Features”- Eventually Consistent: Optimized for reads, writes propagate globally.
- Low Latency: Data is cached at the edge.
- Simple API: Get, put, delete operations.
- Expiration: Automatic key expiration (TTL).
Use Cases
Section titled “Use Cases”- Caching API responses.
- Feature flags and configuration.
- User preferences and settings.
Example
Section titled “Example”// Read from KV in a Cloudflare Workerexport default { async fetch(request, env) { const value = await env.MY_KV.get("myKey"); return new Response(value); }};Learn More
Section titled “Learn More”Comparison Table
Section titled “Comparison Table”| Feature | D1 | R2 | KV |
|---|---|---|---|
| Type | SQL Database | Object Storage | Key-Value Store |
| Best For | Relational data, complex queries | Large files, media | High-read, low-write data |
| Consistency | Strong | Strong | Eventual |
| Query Language | SQL | S3 API | Simple Get/Put |
| Latency | Low | Low | Very Low |
- D1 is for SQL workloads at the edge.
- R2 is for object storage with zero egress fees.
- KV is for fast, globally distributed key-value data.