| 1 | # KV |
| 2 | |
| 3 | KV is a key-value store that an application accesses through a binding. A |
| 4 | Worker reads a value with `env.NAMESPACE.get()`, and it writes one with |
| 5 | `env.NAMESPACE.put()`. An application uses KV for data that many requests read |
| 6 | and few requests change, such as a feature flag, a redirect table, or a |
| 7 | configuration document. Each namespace lives in one celld cell, and a value |
| 8 | above 1 MiB lives in the fleet bucket. Read the |
| 9 | [Cloudflare KV documentation](https://developers.cloudflare.com/kv/api/) for the |
| 10 | standard API behavior. |
| 11 | |
| 12 | ## Example |
| 13 | |
| 14 | The [KV example](../../examples/kv) reads, writes, and deletes values in a KV |
| 15 | namespace. |
| 16 | |
| 17 | <!-- celld-example: kv --> |
| 18 | |
| 19 | ## Namespaces, keys, and values |
| 20 | |
| 21 | A project declares a namespace with a `kv_namespaces` entry that gives a |
| 22 | `binding` name and an `id`. celld uses the `id` as the namespace identity, |
| 23 | whatever string it holds, so a hexadecimal id from Cloudflare and a plain name |
| 24 | such as `sessions` both work. A namespace belongs to the fleet and not to one |
| 25 | script, so two Workers that name one `id` reach one namespace. celld accepts |
| 26 | `preview_id` and ignores it, because celld does not run `wrangler dev`. |
| 27 | |
| 28 | `put()` accepts a string, an `ArrayBuffer`, a typed array, or a |
| 29 | `ReadableStream`, and celld copies the bytes at the call so a later change to |
| 30 | the buffer cannot alter the stored value. celld reads a stream to its end |
| 31 | before the write, therefore `put(key, request.body)` stores a request body. |
| 32 | `get()` returns the value as `"text"`, `"json"`, `"arrayBuffer"`, or |
| 33 | `"stream"`. `getWithMetadata()` also returns the metadata that the write |
| 34 | attached, and a missing key gives `null` in both methods. `list()` returns at |
| 35 | most 1000 key names in byte order, and its cursor resumes after the last |
| 36 | returned name, so a concurrent write cannot make the pagination 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, and serialized metadata of |
| 41 | at most 1024 bytes. One bulk `get()` takes at most 100 keys. celld refuses a |
| 42 | call that crosses a limit instead of truncating the data, so the application |
| 43 | learns about the problem at the call site. |
| 44 | |
| 45 | A write can set `expiration` as an absolute time in seconds, or `expirationTtl` |
| 46 | as a count of seconds from now. The minimum lifetime is 60 seconds. celld |
| 47 | filters an expired key on the read path, so the key becomes invisible at the |
| 48 | moment it expires, and a sweeper reclaims the storage afterwards. |
| 49 | |
| 50 | ## The read and write path |
| 51 | |
| 52 | A Cloudflare namespace is |
| 53 | [a database that replicates to the global network](https://developers.cloudflare.com/kv/concepts/kv-namespaces/), |
| 54 | and each location caches the values that it reads. celld has no such cache. A |
| 55 | celld namespace is one cell, which is the same single-threaded actor that runs a |
| 56 | Durable Object, so the namespace state lives in that cell's SQLite database. |
| 57 | Every call reaches the node that owns the cell, therefore a celld read returns |
| 58 | the committed value and is never stale. |
| 59 | |
| 60 |  |
| 61 | |
| 62 | One cell owns a namespace, and a cell has one writer. Two writes to one |
| 63 | namespace therefore run one after the other, even when they touch different |
| 64 | keys. More nodes do not divide that work, because the namespace has one owner. |
| 65 | An application that needs more write capacity splits its keys across more |
| 66 | namespaces, because KV has no operation that spans two keys. |
| 67 | |
| 68 | Reads and writes cost different amounts. A write commits to the cell's SQLite |
| 69 | database and replicates to the fleet, so it pays for durability every time. A |
| 70 | read only answers from the committed state. This asymmetry makes KV a good |
| 71 | choice for configuration data that a Worker reads on every request, and a poor |
| 72 | choice for a counter that many requests increment. Use a Durable Object for a |
| 73 | write-hot value, which is the same advice that |
| 74 | [Cloudflare gives](https://developers.cloudflare.com/kv/concepts/how-kv-works/). |
| 75 | |
| 76 | A read also has a cost in celld that it does not have on Cloudflare. Cloudflare |
| 77 | answers a hot key from a cache in the location that serves the request. celld |
| 78 | sends every `get()` to the owner node, so a read costs one cell dispatch. An |
| 79 | application that reads one key many times inside one request must hold the value |
| 80 | in a local variable. |
| 81 | |
| 82 | ## A value above the inline limit |
| 83 | |
| 84 | celld keeps a value of 1 MiB or less inside the cell, because such a value |
| 85 | replicates with the cell's other writes. A larger value goes to the fleet bucket |
| 86 | under a content address, and the cell row holds only the reference. This split |
| 87 | keeps one large `put()` from becoming the most expensive operation in the |
| 88 | runtime. Cloudflare made the same split for the same reason, and sends a large |
| 89 | value to object storage. |
| 90 | |
| 91 | A node therefore needs a fleet bucket to write a value above 1 MiB. Without one, |
| 92 | the write fails with the message `KV large values need a fleet bucket`. celld |
| 93 | writes the object before it commits the row, so a crash between the two steps |
| 94 | leaves an unreferenced object that a collector removes later. A committed row |
| 95 | never names an object that does not exist. |
| 96 | |
| 97 | ## Differences from Cloudflare |
| 98 | |
| 99 | - A celld read never returns a stale value, because it reaches the cell that |
| 100 | owns the namespace. Cloudflare KV is eventually consistent. |
| 101 | - celld has no edge cache. `cacheTtl` has no effect, and `cacheStatus` is |
| 102 | `null`. |
| 103 | - A value above 1 MiB requires a fleet bucket. |
| 104 | - A namespace has one writer. Use more namespaces to increase write capacity. |
| 105 | |
| 106 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the |
| 107 | runtime APIs and the unsupported services. |