Diffinery

homeflare/celldpublic

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