Data stores for Fastly services
Fastly services typically access content by fetching it from your backend servers, and your backend servers will also handle updates to that data. However, in many cases you may want to read and write data directly at the edge, interacting with data stores that are provided by Fastly.
The data in our data stores is globally available, durable, eventually consistent, and is read/write both at the edge and via the Fastly API. Read latency is typically under a millisecond but may be up to 300ms for data that has recently changed or is not yet replicated to the local POP.
HINT: If you don't need to write to your data at the edge, see Dynamic configuration instead, which offers predictably fast reads. If your use case involves counting things, consider using rate limiting.
The following data stores are currently available:
Product | Platforms | Shareable | Typical use cases |
---|---|---|---|
KV stores | Compute only | Yes | Redirects, files, user profile data |
KV stores
KV stores are service-linked resources that provide durable storage of key/value data that is readable and writable at the edge and synchronized globally. Stores can be created and managed via multiple methods:
Create store | Add entry | Delete entry | List entries | |
---|---|---|---|---|
Using the fastly CLI | fastly kv-store create | fastly kv-store-entry create | fastly kv-store-entry delete | fastly kv-store-entry list |
Using the API | Create KV store endpoint | Create entry endpoint | Delete entry endpoint | List entries endpoint |
KV stores must be attached to a service using the Resources API.
For example, the following curl commands create a KV store and add it to a service:
# Create a KV store$ curl -i -X POST "https://api.fastly.com/resources/stores/kv" -H "Fastly-Key: YOUR_FASTLY_TOKEN" -H "Content-Type: application/json" -H "Accept: application/json" -d '{"name":"example-store"}'
# Link the store to a service$ curl -i -X POST "https://api.fastly.com/service/YOUR_FASTLY_SERVICE_ID/version/YOUR_FASTLY_SERVICE_VERSION/resource" -H "Fastly-Key: YOUR_FASTLY_TOKEN" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "name=example-store-service-a&resource_id=YOUR_KV_STORE_ID"
KV stores are exposed to Compute services via dedicated interfaces in each SDK:
- Rust
- JavaScript
- Go
use fastly::{Error, KVStore, Request, Response};
#[fastly::main]fn main(_request: Request) -> Result<Response, Error> {
// Define a KV store instance using the resource link name let store = KVStore::open("example-store-service-a")?.unwrap();
// Insert an item into the KV store store.insert("hello.txt", "world")?;
// Get the value back from the KV store (as a string), // and return it, with HTTP response code 200 let mut value = store.lookup("hello.txt")?; Ok(Response::from_status(200).with_body(value.take_body()))}
See the Compute SDKs reference for more information about individual methods in each SDK.
Read requests from the edge are fulfilled by the Fastly cache, so offer a read latency of under a millisecond if the value is in cache, or up to a few hundred milliseconds if the entry needs to be fetched from outside the POP.
Limitations and constraints
Read about the limitations and constraints of KV Stores on the Compute resource limits page.
Read about the limitations and constraints of the links between services and data stores on the Resources core concepts page.