Skip to content

Commit a70d1d7

Browse files
committed
wip
1 parent eff5523 commit a70d1d7

File tree

2 files changed

+57
-88
lines changed

2 files changed

+57
-88
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Read multiple keys from Workers KV with bulk reads
3+
description: You can now retrieve up to 100 keys in a single bulk read request to Workers KV.
4+
products:
5+
- kv
6+
date: 2025-01-28T14:00:00Z
7+
---
8+
9+
You can now retrieve up to 100 keys in a single bulk read request made to Workers KV.
10+
11+
This makes it easier to
12+
13+
Workers KV namespace limits were increased from 200 to 1000 for all accounts. Higher limits for Workers KV namespaces enable better organization of key-value data, such as by category, tenant, or environment.
14+
15+
Consult the [Workers KV limits documentation](/kv/platform/limits/) for the rest of the limits. This increased limit is available for both the Free and Paid [Workers plans](/workers/platform/pricing/).

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

Lines changed: 42 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,71 @@ sidebar:
88
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:
99

1010
```js
11+
// Read individual key
1112
env.NAMESPACE.get(key);
13+
14+
// Read multiple keys
15+
env.NAMESPACE.get(keys);
1216
```
1317

1418
The `get()` method returns a promise you can `await` on to get the value.
1519

1620
If you request a single key as a string, you will get a single response in the promise. If the key is not found, the promise will resolve with the literal value `null`.
1721

18-
Instead of a single key, you can also request an array of keys. In this case, the response is a map with the key-value pairs.
19-
20-
#### Example
21-
22-
An example of reading a key from within a Worker:
22+
You can also request an array of keys. The return value with be a `Map` of the key-value pairs found,
23+
with keys not found having `null` values.
2324

2425
```js
2526
export default {
2627
async fetch(request, env, ctx) {
2728
try {
29+
// Read single key, returns value or null
2830
const value = await env.NAMESPACE.get("first-key");
29-
test;
3031

31-
if (value === null) {
32-
return new Response("Value not found", { status: 404 });
33-
}
34-
return new Response(value);
35-
} catch (e) {
36-
return new Response(e.message, { status: 500 });
37-
}
38-
},
39-
};
40-
```
32+
// Read multiple keys, returns Map of values
33+
const values = await env.NAMESPACE.get(["first-key", "second-key"]);
4134

42-
Or if you request multiple keys:
35+
// Read single key with metadata, returns value or null
36+
const valueWithMetadata = await env.NAMESPACE.getWithMetadata("first-key");
4337

44-
```js
45-
export default {
46-
async fetch(request, env, ctx) {
47-
try {
48-
const value = await env.NAMESPACE.get(["first-key", "second-key"]);
38+
// Read multiple keys with metadata, returns Map of values
39+
const valuesWithMetadata = await env.NAMESPACE.getWithMetadata(["first-key", "second-key"]);
4940

50-
return new Response(
51-
"values: " + value.get("second-key") + " " + value.get("second-key"),
52-
);
41+
return new Response({
42+
value: value,
43+
values: Object.fromEntries(values),
44+
valueWithMetadata: valueWithMetadata,
45+
valuesWithMetadata: Object.fromEntries(valuesWithMetadata)
46+
});
5347
} catch (e) {
5448
return new Response(e.message, { status: 500 });
5549
}
5650
},
5751
};
5852
```
5953

54+
:::note
55+
56+
`get()` and `getWithMetadata()` methods 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.
57+
58+
:::
59+
60+
6061
## Reference
6162

6263
The following methods are provided to read from KV:
6364

64-
- [get()](#get-method)
65-
- [getWithMetadata()](#getwithmetadata-method)
65+
- [get()](#request-a-single-key-with-getkey-string)
66+
- [getWithMetadata()](#request-multiple-keys-with-getkeys-string)
6667

6768
### `get()` method
6869

6970
Use the `get()` method to get a single value, or multiple values if given multiple keys:
7071

71-
- Read single keys with [get(key)](#request-a-single-key)
72-
- Read multiple keys with [get([keys])](#request-multiple-keys)
72+
- Read single keys with [get(key: string)](#request-a-single-key-with-getkey-string)
73+
- Read multiple keys with [get(keys: string[])](#request-multiple-keys-with-getkeys-string)
7374

74-
#### Request a single key
75+
#### Request a single key with `get(key: string)`
7576

7677
To get the value for a single key, call the `get()` method on any KV namespace you have bound to your Worker code with:
7778

@@ -102,9 +103,7 @@ env.NAMESPACE.get(key, options?);
102103
- `arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
103104
- `stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
104105
105-
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.
106-
107-
#### Request multiple keys
106+
#### Request multiple keys with `get(keys: string[])`
108107
109108
To get the values for multiple keys, call the `get()` method on any KV namespace you have bound to your Worker code with:
110109
@@ -134,22 +133,21 @@ The `.get()` function to read multiple keys does not support `arrayBuffer` or `s
134133
135134
##### Response
136135
137-
- `response`: `Promise<Map<string, string | Object | null>`
138-
- The value for the requested KV pair. The response type will depend on the `type` parameter provided for the `get()` command as follows:
136+
- `response`: `Promise<Map<string, string | Object | null>>`
137+
- The value for the requested KV pair. If no key is found, `null` is returned for the key. The response type will depend on the `type` parameter provided for the `get()` command as follows:
139138
- `text`: A `string` (default).
140139
- `json`: An object decoded from a JSON string.
141140
142-
When requesting multiple keys, the promise will never resolve to a null value. It may, however, resolve to a map that contains a key that corresponds to a null value, similar to what happens for a single get.
143-
144-
You cannot request over 25 MB of data. If you do, the request will fail with a `413` Error.
145-
146-
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.
141+
The limit of the response size is 25 MB. Responses above this size will fail with a `413 Error` error message.
147142
148143
### `getWithMetadata()` method
149144
150-
Use the `getWithMetadata()` method to get a single value, or multiple values if given multiple keys.
145+
Use the `getWithMetadata()` method to get a single value along with its metadata, or multiple values with their metadata:
151146
152-
#### Request a single key
147+
- Read single keys with [getWithMetadata(key: string)](#request-a-single-key-with-getwithmetadatakey-string)
148+
- Read multiple keys with [getWithMetadata(keys: string[])](#request-multiple-keys-with-getwithmetadatakeys-string)
149+
150+
#### Request a single key with `getWithMetadata(key: string)`
153151
154152
To get the value for a given key along with its metadata, call the `getWithMetadata()` method on any KV namespace you have bound to your Worker code:
155153
@@ -188,31 +186,7 @@ metadata: string | null
188186
189187
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
190188
191-
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.
192-
193-
##### Example
194-
195-
An example of reading a key with metadata from within a Worker:
196-
197-
```js
198-
export default {
199-
async fetch(request, env, ctx) {
200-
try {
201-
const { value, metadata } =
202-
await env.NAMESPACE.getWithMetadata("first-key");
203-
204-
if (value === null) {
205-
return new Response("Value not found", { status: 404 });
206-
}
207-
return new Response(value);
208-
} catch (e) {
209-
return new Response(e.message, { status: 500 });
210-
}
211-
},
212-
};
213-
```
214-
215-
#### Request multiple keys
189+
#### Request multiple keys with `getWithMetadata(keys: string[])`
216190
217191
To get the values for a given set of keys along with their metadata, call the `getWithMetadata()` method on any KV namespace you have bound to your Worker code with:
218192
@@ -252,27 +226,7 @@ metadata: string | Object | null
252226
253227
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
254228
255-
You cannot request over 25MB of data. If you do, the request will fail with a `413` Error .
256-
257-
##### Example
258-
259-
An example of reading a key with metadata from within a Worker:
260-
261-
```js
262-
export default {
263-
async fetch(request, env, ctx) {
264-
try {
265-
const response = await env.NAMESPACE.getWithMetadata(["first-key"]);
266-
const firstValue = response.get("first-key");
267-
return new Response(
268-
"value: " + firstValue?.value + " metadata: " + firstValue?.metadata,
269-
);
270-
} catch (e) {
271-
return new Response(e.message, { status: 500 });
272-
}
273-
},
274-
};
275-
```
229+
The limit of the response size is 25 MB. Responses above this size will fail with a `413 Error` error message.
276230
277231
## Guidance
278232

0 commit comments

Comments
 (0)