Skip to content

Commit a12356d

Browse files
thomasgauvinhyperlint-ai[bot]Oxyjun
authored
[KV] adjust kv docs for tiered cache and analytics for hot and cold reads (cloudflare#17039)
* thomasgauvin: adjust kv docs for tiered cache and analytics for hot and cold reads * thomasgauvin: replicating update * Update src/content/docs/kv/concepts/how-kv-works.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * thomasgauvin: nit * Update src/content/docs/kv/api/read-key-value-pairs.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/kv/api/read-key-value-pairs.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/kv/api/read-key-value-pairs.mdx --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> Co-authored-by: Jun Lee <[email protected]>
1 parent 05ea0fd commit a12356d

File tree

2 files changed

+65
-68
lines changed

2 files changed

+65
-68
lines changed

src/content/docs/kv/api/read-key-value-pairs.mdx

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pcx_content_type: concept
33
title: Read key-value pairs
44
sidebar:
55
order: 4
6-
76
---
87

98
To get the value for a given key, call the `get()` method of the [KV binding](/kv/concepts/kv-bindings/) on any [KV namespace](/kv/concepts/kv-namespaces/) you have bound to your Worker code:
@@ -14,30 +13,31 @@ env.NAMESPACE.get(key);
1413

1514
The `get()` method returns a promise you can `await` on to get the value. If the key is not found, the promise will resolve with the literal value `null`.
1615

17-
#### Example
16+
#### Example
1817

1918
An example of reading a key from within a Worker:
2019

2120
```js
2221
export default {
23-
async fetch(request, env, ctx) {
24-
try {
25-
const value = await env.NAMESPACE.get("first-key");
26-
27-
if (value === null) {
28-
return new Response("Value not found", {status: 404});
29-
}
30-
return new Response(value);
31-
} catch (e) {
32-
return new Response(e.message, {status: 500});
33-
}
34-
},
22+
async fetch(request, env, ctx) {
23+
try {
24+
const value = await env.NAMESPACE.get("first-key");
25+
26+
if (value === null) {
27+
return new Response("Value not found", { status: 404 });
28+
}
29+
return new Response(value);
30+
} catch (e) {
31+
return new Response(e.message, { status: 500 });
32+
}
33+
},
3534
};
3635
```
3736

3837
## Reference
3938

4039
The following methods are provided to read from KV:
40+
4141
- [get()](#get-method)
4242
- [getWithMetadata()](#getwithmetadata-method)
4343

@@ -53,26 +53,26 @@ The `get()` method returns a promise you can `await` on to get the value. If the
5353
5454
#### Parameters
5555
56-
* `key`: `string`
57-
* The key of the KV pair.
58-
* `type`: `"text" | "json" | "arrayBuffer" | "stream"`
59-
* Optional. The type of the value to be returned. `text` is the default.
60-
* `options`: `{
56+
- `key`: `string`
57+
- The key of the KV pair.
58+
- `type`: `"text" | "json" | "arrayBuffer" | "stream"`
59+
- Optional. The type of the value to be returned. `text` is the default.
60+
- `options`: `{
6161
cacheTtl: number,
6262
type: "text" | "json" | "arrayBuffer" | "stream"
6363
}`
64-
* Optional. Object containing the `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
64+
- Optional. Object containing the `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
6565
6666
#### Response
6767
68-
* `response`: `Promise<string | Object | ArrayBuffer | ReadableStream | null>`
69-
* The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
70-
* `text`: A `string` (default).
71-
* `json`: An object decoded from a JSON string.
72-
* `arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
73-
* `stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
68+
- `response`: `Promise<string | Object | ArrayBuffer | ReadableStream | null>`
69+
- The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
70+
- `text`: A `string` (default).
71+
- `json`: An object decoded from a JSON string.
72+
- `arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
73+
- `stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
7474
75-
The `get()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
75+
The `get()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
7676
7777
### `getWithMetadata()` method
7878
@@ -82,59 +82,56 @@ To get the value for a given key along with its metadata, call the `getWithMetad
8282
env.NAMESPACE.getWithMetadata(key, type?, options?);
8383
```
8484
85-
Metadata is a serializable value you append to each KV entry.
86-
85+
Metadata is a serializable value you append to each KV entry.
8786
8887
#### Parameters
8988
90-
* `key`: `string`
91-
* The key of the KV pair.
92-
* `type`: `"text" | "json" | "arrayBuffer" | "stream"`
93-
* Optional. The type of the value to be returned. `string` is the default.
94-
* `options`: `{
89+
- `key`: `string`
90+
- The key of the KV pair.
91+
- `type`: `"text" | "json" | "arrayBuffer" | "stream"`
92+
- Optional. The type of the value to be returned. `text` is the default.
93+
- `options`: `{
9594
cacheTtl: number,
9695
type: "text" | "json" | "arrayBuffer" | "stream"
9796
}`
98-
* Optional. Object containing the `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
97+
- Optional. Object containing the `cacheTtl` and `type` properties. The `cacheTtl` property defines the length of time in seconds that a KV result is cached in the global network location it is accessed from (minimum: 60). The `type` property defines the type of the value to be returned.
9998
10099
#### Response
101100
102-
* `response`: `Promise<{
103-
value: string | Object | ArrayBuffer | ReadableStream | null,
104-
metadata: string | null
105-
}>`
101+
- `response`: `Promise<{
102+
value: string | Object | ArrayBuffer | ReadableStream | null,
103+
metadata: string | null
104+
}>`
106105
107-
* An object containing the value and the metadata for the requested KV pair. The type of the value attribute will depend on the `type` parameter provided for the `getWithMetadata()` command as follows:
108-
* `text`: A `string` (default).
109-
* `json`: An object decoded from a JSON string.
110-
* `arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
111-
* `stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
106+
- An object containing the value and the metadata for the requested KV pair. The type of the value attribute will depend on the `type` parameter provided for the `getWithMetadata()` command as follows:
107+
- `text`: A `string` (default).
108+
- `json`: An object decoded from a JSON string.
109+
- `arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
110+
- `stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
112111
113112
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
114113
115-
The `getWithMetadata()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
116-
114+
The `getWithMetadata()` method may return stale values. If a given key has recently been read in a given location, writes or updates to the key made in other locations may take up to 60 seconds (or the duration of the `cacheTtl`) to display.
117115
118-
#### Example
116+
#### Example
119117
120118
An example of reading a key with metadata from within a Worker:
121119
122120
```js
123121
export default {
124-
async fetch(request, env, ctx) {
125-
try {
126-
const { value, metadata } = await env.NAMESPACE.getWithMetadata("first-key");
127-
128-
if (value === null) {
129-
return new Response("Value not found", {status: 404});
130-
}
131-
return new Response(value);
132-
}
133-
catch (e)
134-
{
135-
return new Response(e.message, {status: 500});
136-
}
137-
},
122+
async fetch(request, env, ctx) {
123+
try {
124+
const { value, metadata } =
125+
await env.NAMESPACE.getWithMetadata("first-key");
126+
127+
if (value === null) {
128+
return new Response("Value not found", { status: 404 });
129+
}
130+
return new Response(value);
131+
} catch (e) {
132+
return new Response(e.message, { status: 500 });
133+
}
134+
},
138135
};
139136
```
140137
@@ -148,22 +145,22 @@ For large values, the choice of `type` can have a noticeable effect on latency a
148145
149146
### CacheTtl parameter
150147
151-
`cacheTtl` is a parameter that defines the length of time in seconds that a KV result is cached in the global network location it is accessed from.
148+
`cacheTtl` is a parameter that defines the length of time in seconds that a KV result is cached in the global network location it is accessed from.
152149
153-
Defining the length of time in seconds is useful for reducing cold read latency on keys that are read relatively infrequently. `cacheTtl` is useful if your data is write-once or write-rarely.
150+
Defining the length of time in seconds is useful for reducing cold read latency on keys that are read relatively infrequently. `cacheTtl` is useful if your data is write-once or write-rarely.
154151
155152
:::note[Hot and cold read]
156153
157-
A hot read means that the data is cached on Cloudflare's edge network using the [CDN](/cache/). A cold read means that the data is not cached, therefore you have to fetch the data from the storage provider. Both existing key-value pairs and non-existent key-value pairs (also known as negative lookups) are cached at the edge.
158-
154+
A hot read means that the data is cached on Cloudflare's edge network using the [CDN](https://developers.cloudflare.com/cache/), whether it is in a local cache or a regional cache. A cold read means that the data is not cached, so the data must be fetched from the central stores. Both existing key-value pairs and non-existent key-value pairs (also known as negative lookups) are cached at the edge.
159155
:::
160156
161157
`cacheTtl` is not recommended if your data is updated often and you need to see updates shortly after they are written, because writes that happen from other global network locations will not be visible until the cached value expires.
162158
163-
The `cacheTtl` parameter must be an integer greater than or equal to `60`, which is the default.
159+
The `cacheTtl` parameter must be an integer greater than or equal to `60`, which is the default.
164160
165161
The effective `cacheTtl` of an already cached item can be reduced by getting it again with a lower `cacheTtl`. For example, if you did `NAMESPACE.get(key, {cacheTtl: 86400})` but later realized that caching for 24 hours was too long, you could `NAMESPACE.get(key, {cacheTtl: 300})` or even `NAMESPACE.get(key)` and it would check for newer data to respect the provided `cacheTtl`, which defaults to 60 seconds.
166162
167163
## Other methods to access KV
168164
169165
You can [read key-value pairs from the command line with Wrangler](/kv/reference/kv-commands/#get) and [from the API](/api/operations/workers-kv-namespace-read-key-value-pair).
166+

src/content/docs/kv/concepts/how-kv-works.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Initial reads from a location do not have a cached value. Data must be read from
2121

2222
:::note[Hot and cold read]
2323

24-
A hot read means that the data is cached on Cloudflare's edge network using the [CDN](https://developers.cloudflare.com/cache/). A cold read means that the data is not cached, therefore you have to fetch the data from the storage provider.
24+
A hot read means that the data is cached on Cloudflare's edge network using the [CDN](https://developers.cloudflare.com/cache/), whether it is in a local cache or a regional cache. A cold read means that the data is not cached, so the data must be fetched from the central stores.
2525
:::
2626

2727
![Initial reads will miss the cache and go to the nearest central data store first.](~/assets/images/kv/kv-slow-read.svg)
@@ -55,7 +55,7 @@ Visibility of changes takes longer in locations which have recently read a previ
5555
:::note
5656

5757
KV is not ideal for applications where you need support for atomic operations or where values must be read and written in a single transaction.
58-
If you need stronger consistency guarantees, consider using [Durable Objects](/durable-objects/).
58+
If you need stronger consistency guarantees, consider using [Durable Objects](/durable-objects/).
5959
:::
6060

6161
An approach to achieve write-after-write consistency is to send all of your writes for a given KV key through a corresponding instance of a Durable Object, and then read that value from KV in other Workers. This is useful if you need more control over writes, but are satisfied with KV's read characteristics described above.

0 commit comments

Comments
 (0)