---
title: "Cloudflare KV - Global Key-Value Database"
description: "Fast, globally distributed key-value database with infinite scale. <5ms hot read latencies, persistent storage, and ideal for read-heavy applications."
url: "https://www.cloudflare.com/products/kv"
---

# KV

> Workers KV is an eventually consistent, high-performance key-value data database built for Workers —  ideal for read-heavy, low-latency edge decision making.

## Key Features

- <5ms hot read latency
- Eventual consistency
- Unlimited storage
- Simple key-value API
- JSON value support
- TTL expiration
- Persistent storage

## Benefits

### Global Low-Latency Reads

Serve configuration or content to users worldwide with <5ms hot read latencies — thanks to Cloudflare's edge network.

### Infinite Scale, Simple API

One API to store and retrieve key-value pairs — unlimited storage, high scalability, no infrastructure to manage.

### More Than a Cache

Unlike a volatile cache, KV stores data persistently in central regions with exceptional availability and durability.

## Use Cases

### Build Distributed Configuration

Manage feature flags, A/B test variants, and redirect rules globally without managing deployments.

### Serve Static Assets

Serve small but critical files like scripts, icons, images, or JSON payloads directly from the edge.

### Personalize your app

Store user preferences or routing maps to customize experiences with near-zero latency.

### Authorization

Quickly look up API keys or authentication tokens to validate requests before they reach your origin.

### Manage Dynamic Data

Store and retrieve complex information as JSON documents, allowing your data structure to evolve easily without complex migrations.

## Code Examples

### Store and retrieve data globally

Write, read, list, and delete key-value pairs from any Worker using a simple API. Ideal for configuration, personalization, and low-latency lookups.

```typescript
export default {
  async fetch(request, env, ctx): Promise<Response> {

    await env.KV.put('KEY', 'VALUE');
    const value = await env.KV.get('KEY');
    const allKeys = await env.KV.list();
    await env.KV.delete('KEY');

    return new Response(
      JSON.stringify({
        value: value,
        allKeys: allKeys,
      }),
    );
  },
};
```

### Power A/B Tests from the Edge

Use Workers KV to store and serve configuration data, like A/B test variants. Fetch a JSON object containing your test parameters and dynamically alter your application's response with minimal latency.

```typescript
export default {
  async fetch(request, env) {
    // Get the entire A/B test configuration object from KV
    const config = await env.CONFIG_STORE.get("homepage-test", "json");

    // Assign user to a group (e.g., based on a cookie or URL)
    const group = request.headers.get("X-User-Group") || "control";

    // Return the specific configuration for the user's group
    const variantData = config[group] || config.control;
    return new Response(JSON.stringify(variantData), {
      headers: { "content-type": "application/json" },
    });
  },
} satisfies ExportedHandler<Env>;
```

### Verify API Keys Instantly

Secure your endpoints by validating API keys or authentication tokens at the edge. Before a request hits your origin, a Worker can check the key against a KV store, blocking unauthorized traffic with zero latency.

```typescript
export default {
  async fetch(request, env) {
    // Get Authorization key from request headers
    const apiKey = request.headers.get("Authorization")?.replace("Bearer ", "");

    if (!apiKey) {
      return new Response("Authorization header missing", { status: 401 });
    }

    // Check if the API key is valid and get associated metadata
    const keyData = await env.API_KEYS.get(apiKey, "json");

    if (keyData && keyData.enabled) {
      // Key is valid, add user info to the request and fetch the origin
      const newHeaders = new Headers(request.headers);
      newHeaders.set("X-User-ID", keyData.userId);
      return fetch(request, { headers: newHeaders });
    }

    return new Response("Invalid API Key", { status: 403 });
  },
} satisfies ExportedHandler<Env>;
```

### Route Requests with a Dynamic Reverse Proxy

Use Workers KV to maintain a dynamic routing table at the edge. Map incoming paths to different backend services or origins without redeploying your Worker. This allows you to seamlessly shift traffic, canary release new versions, or build a multi-service architecture behind a single domain.

```typescript
export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    const path = url.pathname; // e.g., "/api/users"

    // Look up the path prefix (e.g., "/api") to find the correct origin hostname
    const pathPrefix = "/" + path.split('/')[1];
    const originHostname = await env.ROUTING_RULES.get(pathPrefix);

    if (originHostname) {
      // Construct the new URL for the backend service
      const newUrl = new URL(url);
      newUrl.hostname = originHostname;

      // Act as a reverse proxy: fetch the content from the backend
      // and return it to the original client.
      return fetch(newUrl.toString(), request);
    }

    // If no route matches, return a 404 or forward to a default origin
    return new Response("Service not found for this path", { status: 404 });
  },
} satisfies ExportedHandler<Env>;
```

## Resources

- [Full Documentation](https://developers.cloudflare.com/kv): Complete technical documentation
- [Get Started](https://dash.cloudflare.com/sign-up): Sign up and start building
- [Pricing](/plans.md): See pricing details

## Related Products

- [Artifacts](/products/artifacts.md): Git-native versioned storage
- [Cache Reserve](/products/cache-reserve.md): Persistent caching for static content
- [D1](/products/d1.md): Serverless SQL
- [Data Platform](/products/data-platform.md): Ingest, Catalog & Query

---

*This is a markdown version of [https://www.cloudflare.com/products/kv](https://www.cloudflare.com/products/kv) for AI/LLM consumption.*
