| 1 | # KV |
| 2 | |
| 3 | KV is a key-value store that a Worker reaches through a binding. Each namespace |
| 4 | is one celld cell, and a value above 1 MiB lives in the fleet bucket. Read the |
| 5 | [Cloudflare KV documentation](https://developers.cloudflare.com/kv/api/) for the |
| 6 | API. |
| 7 | |
| 8 | ## Example |
| 9 | |
| 10 | The [KV example](../../examples/kv) reads, writes, and deletes values in a KV |
| 11 | namespace. |
| 12 | |
| 13 | <!-- celld-example: kv --> |
| 14 | |
| 15 | ## API |
| 16 | |
| 17 | - `get(key, type)` returns the value as `"text"`, `"json"`, `"arrayBuffer"`, or |
| 18 | `"stream"`. A missing key gives `null`. |
| 19 | - `getWithMetadata(key, type)` also returns the metadata of the write. |
| 20 | - `put(key, value, options)` takes a string, an `ArrayBuffer`, a typed array, or |
| 21 | a `ReadableStream`. The options are `expiration` (an absolute time in |
| 22 | seconds), `expirationTtl` (seconds from now), and `metadata`. |
| 23 | - `delete(key)` removes a key. |
| 24 | - `list({ prefix, cursor })` returns a page of key names and a cursor for the |
| 25 | next page. |
| 26 | |
| 27 | ## Configuration and limits |
| 28 | |
| 29 | A `kv_namespaces` entry gives a `binding` and an `id`. The `id` is the namespace |
| 30 | identity, and it can be any string, such as a Cloudflare hexadecimal id or |
| 31 | `sessions`. Two Workers that name one `id` reach one namespace. celld ignores |
| 32 | `preview_id`. |
| 33 | |
| 34 | `put()` reads a `ReadableStream` to its end before the write, so |
| 35 | `put(key, request.body)` works. `list()` returns at most 1000 keys in byte |
| 36 | order, and a concurrent write cannot make the cursor skip a key. |
| 37 | |
| 38 | celld enforces the |
| 39 | [Cloudflare KV limits](https://developers.cloudflare.com/kv/platform/limits/): a |
| 40 | key of at most 512 bytes, a value of at most 25 MiB, metadata of at most 1024 |
| 41 | bytes, and at most 100 keys in one bulk `get()`. A call that crosses a limit |
| 42 | fails, and celld never truncates data. The minimum expiration is 60 seconds. An |
| 43 | expired key becomes invisible at the moment it expires. |
| 44 | |
| 45 | Every call goes to the node that owns the namespace cell, so a read costs one |
| 46 | cell dispatch. Hold a value in a local variable when one request reads it many |
| 47 | times. Writes to one namespace run one at a time, so use a Durable Object for a |
| 48 | write-hot value such as a counter. |
| 49 | |
| 50 | A write of a value above 1 MiB without a fleet bucket fails with |
| 51 | `KV large values need a fleet bucket`. |
| 52 | |
| 53 | ## Differences from Cloudflare |
| 54 | |
| 55 | - A celld read never returns a stale value, because it reaches the cell that |
| 56 | owns the namespace. Cloudflare KV is eventually consistent. |
| 57 | - celld has no edge cache. `cacheTtl` has no effect, and `cacheStatus` is |
| 58 | `null`. |
| 59 | - A value above 1 MiB requires a fleet bucket. |
| 60 | - A namespace has one writer. Use more namespaces to increase write capacity. |
| 61 | |
| 62 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the |
| 63 | runtime APIs and the unsupported services. |