Memory Store (KV)


Memory Store (KV)

Supercharge your agents with lightning-fast global memory store. Cloudflare KV is an eventually consistent key-value store that provides sub-10ms read latency across 330+ cities worldwide. Perfect for caching API responses, user preferences, and frequently accessed data to dramatically improve agent response times.

Key Features

  • Global Replication: Data is automatically replicated to edge locations worldwide
  • Eventually Consistent: Writes propagate globally within 60 seconds
  • Low Latency: Sub-10ms reads from the nearest edge location
  • High Availability: 99.9% uptime SLA with automatic failover
  • Cost Effective: Pay only for operations, not storage time
  • Expiration Support: Automatic cleanup of cached data with TTL

Getting Started

Wrangler Configuration

First, add KV namespaces in your wrangler.json:

json

Agent Example

typescript

MCP Tool Integration

Integrate KV caching capabilities with MCP tools:

typescript

TypeScript API Reference

You can access the KV API via the env of the Agent or MCP Tool

typescript

Basic Operations

CACHE.get(key: string): Promise<string | null>
Retrieve a value from the cache by key. Returns null if the key doesn't exist.

CACHE.put(key: string, value: string, options?: KVPutOptions): Promise<void>
Store a value in the cache with optional expiration and metadata.

CACHE.delete(key: string): Promise<void>
Remove a key-value pair from the cache.

Advanced Operations

CACHE.list(options?: KVListOptions): Promise<KVListResult>
List keys in the cache with optional filtering and pagination.

CACHE.getWithMetadata(key: string): Promise<KVValueWithMetadata>
Retrieve a value along with its metadata and expiration information.

Batch Operations

CACHE.getMultiple(keys: string[]): Promise<Record<string, string | null>>
Retrieve multiple values in a single operation for better performance.

CACHE.putMultiple(entries: Array<{key: string, value: string, options?: KVPutOptions}>): Promise<void>
Store multiple key-value pairs in a single operation.

KV Options

typescript

Examples

Learn how to integrate KV caching into your agents and MCP tools for optimal performance through practical implementations.

Session State Management

Use KV to maintain session state across multiple agent interactions with automatic cleanup.

typescript

Configuration and Feature Flags

Cache configuration values and feature flags for instant access without database queries.

typescript

Best Practices

Essential patterns and techniques for optimal KV cache performance and reliability.

Cache Key Strategy

Use hierarchical, predictable keys for better organization and batch operations.

typescript

Error Handling and Fallbacks

Always provide fallbacks when cache operations fail to ensure reliability.

typescript

Batch Operations for Performance

Use batch operations to reduce latency when working with multiple keys.

typescript

TTL Strategy

Choose appropriate expiration times based on data characteristics and update frequency.

typescript

Limitations & Considerations

Important constraints and guidelines when using KV for caching at scale.

  • Value Size: Maximum 25 MB per value (use compression for large data)
  • Key Size: Maximum 512 bytes per key
  • List Operations: Maximum 1,000 keys per list operation
  • Eventual Consistency: Writes may take up to 60 seconds to propagate globally
  • Rate Limits: 1,000 operations per second per key
  • No Transactions: KV doesn't support atomic multi-key operations
  • Analytics Storage - Track cache hit rates and performance metrics
  • Cache - HTTP response caching at the edge
  • Databases - Primary data storage for complex queries
  • Workflows - Orchestrate cache warming and cleanup

Official Documentation