storage Operation
Access Cloudflare storage: KV (key-value), D1 (SQL database), R2 (object storage), and Vectorize (vector database). Thestorage operation provides a unified interface to all Cloudflare data primitives with automatic binding resolution.
Configuration
KV (Key-Value Store)
Global key-value cache with eventual consistency. Perfect for configuration, sessions, and caching.GET Operation
PUT Operation
expirationTtl(number) - Seconds until expirationexpiration(number) - Unix timestamp for expirationmetadata(object) - Custom metadata (max 1KB)
DELETE Operation
LIST Operation
prefix(string) - Filter keys by prefixlimit(number) - Max results (default: 1000, max: 1000)cursor(string) - Pagination cursor
D1 (SQL Database)
Serverless SQLite database for relational data with full SQL support.Query Operation
Parameterized Queries
Always use parameterized queries to prevent SQL injection:INSERT with RETURNING
UPDATE Operation
DELETE Operation
Complex Queries
Transactions
For atomic operations across multiple queries:R2 (Object Storage)
S3-compatible object storage for files, images, backups, and large data.GET Object
PUT Object
httpMetadata(object) - Standard HTTP metadatacustomMetadata(object) - Custom key-value metadata (max 2KB)
DELETE Object
LIST Objects
prefix(string) - Filter by prefixlimit(number) - Max results (default: 1000)delimiter(string) - Directory delimitercursor(string) - Pagination cursorinclude(string[]) - Include metadata:['httpMetadata', 'customMetadata']
HEAD Object (Metadata Only)
Vectorize (Vector Database)
Vector database for semantic search, RAG, and similarity matching.Insert Vectors
Query Vectors
topK(number) - Number of results (max: 100)returnMetadata(boolean) - Include metadata (default: true)returnValues(boolean) - Include vectors (default: false)filter(object) - Metadata filter
Query with Metadata Filter
Get Vector by ID
Delete Vectors
Common Patterns
Cache-Aside Pattern
Check cache first, then database:Write-Through Cache
Update database and cache simultaneously:Pagination
Semantic Search (RAG)
File Upload Pipeline
Bindings Setup
Configure storage bindings inwrangler.toml:
Performance Tips
1. Use Batch Operations
Combine multiple D1 queries:2. Index Database Fields
Create indexes for frequently queried columns:3. Use KV for Hot Data
Cache frequently accessed data:4. Paginate Large Results
Never fetch all records at once:5. Use Prepared Statements
D1 automatically prepares parameterized queries for better performance.Error Handling
Testing
Best Practices
1. Choose the Right Storage- KV: 25MB per value
- D1: 500MB per database
- R2: Unlimited with paid plan
- Vectorize: 200,000 vectors (free), unlimited (paid)
Storage Limits
KV
- Max value size: 25 MB
- Max metadata size: 1024 bytes
- Max keys per account: 1 billion
- Operations per second: Unlimited
D1
- Max database size: 500 MB (free), 10 GB (paid)
- Max query time: 30 seconds
- Max batch size: 1000 statements
- Databases per account: 10 (free), 50,000 (paid)
R2
- Max object size: 5 TB
- Max multipart parts: 10,000
- Operations per second: Unlimited
- Storage: 10 GB/month free, then paid
Vectorize
- Max dimensions: 1536
- Max vectors: 200,000 (free), unlimited (paid)
- Queries per second: Varies by plan
- Max topK: 100

