Write key-value pairs
To create a new key-value pair, or to update the value for a particular key, call the put()
method of the KV binding on any KV namespace you have bound to your Worker code:
An example of writing a key-value pair from within a Worker:
The following method is provided to write to KV:
To create a new key-value pair, or to update the value for a particular key, call the put()
method on any KV namespace you have bound to your Worker code:
-
key
:string
- The key to associate with the value. A key cannot be empty or be exactly equal to
.
or..
. All other keys are valid. Keys have a maximum length of 512 bytes.
- The key to associate with the value. A key cannot be empty or be exactly equal to
-
value
:string
|ReadableStream
|ArrayBuffer
- The value to store. The type is inferred. The maximum size of a value is 25 MiB.
-
options
:{ expiration?: number, expirationTtl?: number, metadata?: object }
- Optional. An object containing the
expiration
(optional),expirationTtl
(optional), andmetadata
(optional) attributes.expiration
is the number that represents when to expire the key-value pair in seconds since epoch.expirationTtl
is the number that represents when to expire the key-value pair in seconds from now. The minimum value is 60.metadata
is an object that must serialize to JSON. The maximum size of the serialized JSON representation of the metadata object is 1024 bytes.
- Optional. An object containing the
response
:Promise<void>
- A
Promise
that resolves if the update is successful.
- A
The put() method returns a Promise that you should await
on to verify a successful update.
Due to the eventually consistent nature of KV, concurrent writes to the same key can end up overwriting one another. It is a common pattern to write data from a single process with Wrangler or the API. This avoids competing concurrent writes because of the single stream. All data is still readily available within all Workers bound to the namespace.
If concurrent writes are made to the same key, the last write will take precedence.
Writes are immediately visible to other requests in the same global network location, but can take up to 60 seconds (or the value of the cacheTtl
parameter of the get()
or getWithMetadata()
methods) to be visible in other parts of the world.
Refer to How KV works for more information on this topic.
Write more than one key-value pair at a time with Wrangler or via the API.
The bulk API can accept up to 10,000 KV pairs at once.
A key
and a value
are required for each KV pair. The entire request size must be less than 100 megabytes. Bulk writes are not supported using the KV binding.
KV offers the ability to create keys that automatically expire. You may configure expiration to occur either at a particular point in time, or after a certain amount of time has passed since the key was last modified.
Once the expiration time of an expiring key is reached, it will be deleted from the system. After its deletion, attempts to read the key will behave as if the key does not exist. The deleted key will not count against the KV namespace’s storage usage for billing purposes.
There are two ways to specify when a key should expire:
-
Set a key’s expiration using an absolute time specified in a number of seconds since the UNIX epoch ↗. For example, if you wanted a key to expire at 12:00AM UTC on April 1, 2019, you would set the key’s expiration to
1554076800
. -
Set a key’s expiration time to live (TTL) using a relative number of seconds from the current time. For example, if you wanted a key to expire 10 minutes after creating it, you would set its expiration TTL to
600
.
Expiration targets that are less than 60 seconds into the future are not supported. This is true for both expiration methods.
To create expiring keys, set expiration
in the put()
options to a number representing the seconds since epoch, or set expirationTtl
in the put()
options to a number representing the seconds from now:
These assume that secondsSinceEpoch
and secondsFromNow
are variables defined elsewhere in your Worker code.
To associate metadata with a key-value pair, set metadata
in the put()
options to an object (serializable to JSON):
Workers KV has a maximum of 1 write to the same key per second. Writes made to the same key within 1 second will cause rate limiting (429
) errors to be thrown.
You should not write more than once per second to the same key. Consider consolidating your writes to a key within a Worker invocation to a single write, or wait at least 1 second between writes.
The following example serves as a demonstration of how multiple writes to the same key may return errors by forcing concurrent writes within a single Worker invocation. This is not a pattern that should be used in production.
To handle these errors, we recommend implementing a retry logic, with exponential backoff. Here is a simple approach to add retries to the above code.
You can also write key-value pairs from the command line with Wrangler and write data via the API.