Skip to content

Commit f9903c2

Browse files
Oxyjunthomasgauvinmarciocloudflare
authored
[Workers KV] Adding guidance on coalescing keys (#22496)
* Including coalescing keys guidance * Apply suggestions from code review Co-authored-by: Thomas Gauvin <[email protected]> * Updating internal link. * Apply suggestions from code review Co-authored-by: marciocloudflare <[email protected]> --------- Co-authored-by: Thomas Gauvin <[email protected]> Co-authored-by: marciocloudflare <[email protected]>
1 parent e25fe5d commit f9903c2

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,41 @@ The effective `cacheTtl` of an already cached item can be reduced by getting it
255255
256256
### Requesting more keys per Worker invocation with bulk requests
257257
258-
Workers are limited to 1000 operations to external services per invocation. This applies to Workers KV, as documented in [Workers KV limits](/kv/platform/limits/).
258+
Workers are limited to 1,000 operations to external services per invocation. This applies to Workers KV, as documented in [Workers KV limits](/kv/platform/limits/).
259+
260+
To read more than 1,000 keys per operation, you can use the bulk read operations to read multiple keys in a single operation. These count as a single operation against the 1,000 operation limit.
261+
262+
### Reducing cardinality by coalescing keys
263+
264+
If you have a set of related key-value pairs that have a mixed usage pattern (some hot keys and some cold keys), consider coalescing them. By coalescing cold keys with hot keys, cold keys will be cached alongside hot keys which can provide faster reads than if they were uncached as individual keys.
265+
266+
#### Merging into a "super" KV entry
267+
268+
One coalescing technique is to make all the keys and values part of a super key-value object. An example is shown below.
269+
270+
```
271+
key1: value1
272+
key2: value2
273+
key3: value3
274+
```
275+
276+
becomes
277+
278+
```
279+
coalesced: {
280+
key1: value1,
281+
key2: value2,
282+
key3: value3,
283+
}
284+
```
285+
286+
By coalescing the values, the cold keys benefit from being kept warm in the cache because of access patterns of the warmer keys.
287+
288+
This works best if you are not expecting the need to update the values independently of each other, which can pose race conditions.
289+
290+
- **Advantage**: Infrequently accessed keys are kept in the cache.
291+
- **Disadvantage**: Size of the resultant value can push your worker out of its memory limits. Safely updating the value requires a [locking mechanism](/kv/api/write-key-value-pairs/#concurrent-writes-to-the-same-key) of some kind.
259292
260-
To read more than 1000 keys per operation, you can use the bulk read operations to read multiple keys in a single operation. These count as a single operation against the 1000 operation limit.
261293
262294
## Other methods to access KV
263295

0 commit comments

Comments
 (0)