Write key-value pairs
To create a new key-value pair, or to update the value for a particular key, call the put() method on your KV instance.
To create a new key-value pair, or to update the value for a particular key,
call the put() method on your KV instance:
Example
An example of writing a key-value pair from within a Function:
Reference
put() method
To create a new key-value pair, or to update the value for a particular key,
call the put() method on your KV instance:
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to associate with the value. Keys have a maximum length of 512 bytes. |
value | string | object | ArrayBuffer | ReadableStream | The value to store. The type is inferred. The maximum size of a value is 25 MB. |
options | object | Optional. An object containing expiration, expirationTtl, and metadata attributes. |
Options
| Option | Type | Description |
|---|---|---|
expiration | number | The number representing when to expire the key-value pair in seconds since epoch. |
expirationTtl | number | The number representing when to expire the key-value pair in seconds from now. The minimum value is 60. |
metadata | object | An object that must serialize to JSON. The maximum size of the serialized JSON representation is 1024 bytes. |
Response
| Response | Type | Description |
|---|---|---|
response | Promise<void> | A Promise that resolves if the update is successful. |
The put() method returns a Promise that you should await to verify a
successful update.
Guidance
Writing different value types
You can store different types of values:
Concurrent writes to the same key
Due to the eventually consistent nature of KV Store, concurrent writes to the same key can end up overwriting one another. 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.
Expiring keys
KV Store offers the ability to create keys that automatically expire. You can
configure expiration to occur either at a particular point in time (using the
expiration option), or after a certain amount of time has passed since the
key was last modified (using the expirationTtl option).
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.
Create expiring keys
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:
Metadata
To associate metadata with a key-value pair, set metadata in the put()
options to an object (serializable to JSON):
You can later retrieve this metadata using the getWithMetadata() method.
Rate limits
KV Store has a maximum of 1 write to the same key per second. Writes made to the same key within 1 second may cause rate limiting errors.
You should not write more than once per second to the same key. Consider consolidating your writes to a key within a Function invocation to a single write, or wait at least 1 second between writes.
Storing large data with streams
For large payloads, use ReadableStream to stream data efficiently: