Key Value Store
- Using Key Value Store From Applications
- Custom Key Value Stores
- Granting Key Value Store Permissions to Components
Spin provides an interface for you to persist data in a key value store managed by Spin. This key value store allows Spin developers to persist non-relational data across application invocations. To learn more about key value store use cases and how to enable your Spin application to use a key value store, check out our key value tutorial.
Why do I need a Spin interface? Why can't I just use my own external store?
You can absolutely still use your own external store either with the Redis or Postgres APIs, or outbound HTTP. However, if you’re interested in quick, non-relational local storage without any infrastructure set-up then Spin’s key value store is a great option.
Using Key Value Store From Applications
The Spin SDK surfaces the Spin key value store interface to your language. The following characteristics are true of keys and values:
- Keys as large as 256 bytes (UTF-8 encoded)
- Values as large as 1 megabyte
- Capacity for 1024 key value tuples
The set of operations is common across all SDKs:
Operation | Parameters | Returns | Behavior |
---|---|---|---|
open | name | store | Open the store with the specified name. If name is the string “default”, the default store is opened, provided that the component that was granted access in the component manifest from spin.toml . Otherwise, name must refer to a store defined and configured in a runtime configuration file supplied with the application. |
get | store, key | value | Get the value associated with the specified key from the specified store . |
set | store, key, value | - | Set the value associated with the specified key in the specified store , overwriting any existing value. |
delete | store, key | - | Delete the tuple with the specified key from the specified store . error::invalid-store will be raised if store is not a valid handle to an open store. No error is raised if a tuple did not previously exist for key . |
exists | store, key | boolean | Return whether a tuple exists for the specified key in the specified store . |
get-keys | store | list | Return a list of all the keys in the specified store . |
close | store | - | Close the specified store . |
The exact detail of calling these operations from your application depends on your language:
Key value functions are available in the spin_sdk::key_value
module. The function names match the operations above. For example:
use anyhow::Result;
use spin_sdk::{
http::{Request, Response},
http_component,
key_value::{Store},
};
#[http_component]
fn handle_request(_req: Request) -> Result<Response> {
let store = Store::open_default()?;
store.set("mykey", "myvalue")?;
let value = store.get("mykey")?;
Ok(http::Response::builder().status(200).body(Some(value.into()))?)
}
General Notes
set
Operation
- For set, the value argument can be of any type that implements AsRef<[u8]>
get
Operation
- For get, the return value is of type Vec
. If the key does not exist then Error::NoSuchKey is returned.
open
Operation
- The close operation is not surfaced; it is called automatically when the store is dropped.
set_json
and get_json
Operation
- Rust applications can store and retrieve serializable Rust types.
Custom Key Value Stores
Spin defines a key-value store named "default"
and provides automatic backing storage. If you need to customize Spin with additional stores, or to change the backing storage for the default store, you can do so via the --runtime-config-file
flag and the runtime-config.toml
file. See Key Value Store Runtime Configuration for details.
Granting Key Value Store Permissions to Components
By default, a given component of an app will not have access to any key value store. Access must be granted specifically to each component via the component manifest:
[component]
# Pass in 1 or more key value stores, based on how many you'd like your component to have access to
key_value_stores = ["<store 1>", "<store 2>"]
For example, a component could be given access to the default store using key_value_stores = ["default"]
.