You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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/).
Copy file name to clipboardExpand all lines: src/content/docs/kv/api/read-key-value-pairs.mdx
+42-88Lines changed: 42 additions & 88 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,70 +8,71 @@ sidebar:
8
8
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:
9
9
10
10
```js
11
+
// Read individual key
11
12
env.NAMESPACE.get(key);
13
+
14
+
// Read multiple keys
15
+
env.NAMESPACE.get(keys);
12
16
```
13
17
14
18
The `get()` method returns a promise you can `await` on to get the value.
15
19
16
20
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`.
17
21
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.
23
24
24
25
```js
25
26
exportdefault {
26
27
asyncfetch(request, env, ctx) {
27
28
try {
29
+
// Read single key, returns value or null
28
30
constvalue=awaitenv.NAMESPACE.get("first-key");
29
-
test;
30
31
31
-
if (value ===null) {
32
-
returnnewResponse("Value not found", { status:404 });
`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
+
60
61
## Reference
61
62
62
63
The following methods are provided to read from KV:
- `arrayBuffer`: An [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) instance.
103
104
- `stream`: A [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
104
105
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[])`
108
107
109
108
To get the values for multiple keys, call the `get()` method on any KV namespace you have bound to your Worker code with:
110
109
@@ -134,22 +133,21 @@ The `.get()` function to read multiple keys does not support `arrayBuffer` or `s
- 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:
139
138
- `text`: A `string` (default).
140
139
- `json`: An object decoded from a JSON string.
141
140
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 `413Error` error message.
147
142
148
143
### `getWithMetadata()` method
149
144
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:
151
146
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)`
153
151
154
152
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:
155
153
@@ -188,31 +186,7 @@ metadata: string | null
188
186
189
187
If there is no metadata associated with the requested key-value pair, `null` will be returned for metadata.
190
188
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
-
exportdefault {
199
-
asyncfetch(request, env, ctx) {
200
-
try {
201
-
const { value, metadata } =
202
-
awaitenv.NAMESPACE.getWithMetadata("first-key");
203
-
204
-
if (value ===null) {
205
-
returnnewResponse("Value not found", { status:404 });
206
-
}
207
-
returnnewResponse(value);
208
-
} catch (e) {
209
-
returnnewResponse(e.message, { status:500 });
210
-
}
211
-
},
212
-
};
213
-
```
214
-
215
-
#### Request multiple keys
189
+
#### Request multiple keys with `getWithMetadata(keys: string[])`
216
190
217
191
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:
0 commit comments