Value Stores


A Value Store provides an agent with a store for state that is not publically addressable. If persistence is enabled and the application restarts, the state of the store will be reloaded from the underlying persistence engine. If persistence is not enabled, the state of the store will be reloaded from the in-memory store in between agent restarts but not application restarts. If you require addressable state, then a Value Lane will be suitable.

A Value Store meets the following requirements:

For instances where a map structure is required, a Map Store is available.

Example: using a Value Store to store the highest value received by a Value Lane:

use swimos::agent::{
    event_handler::{EventHandler, HandlerActionExt},
    lifecycle, projections,

pub struct ExampleAgent {
    lane: ValueLane<i32>,
    store: ValueStore<i32>,

pub struct ExampleLifecycle;

impl ExampleLifecycle {
    pub fn on_event(
        context: HandlerContext<ExampleAgent>,
        value: &i32,
    ) -> impl EventHandler<ExampleAgent> {
        let n = *value;
            .and_then(move |state| {
                let new_state = if n > state { n } else { state };
                context.set_value(ExampleAgent::STORE, new_state)

Use cases

Value Stores are useful in situtations where you need to persist data that is derived from lane events received but you do not wish for it to be available outside of the scope of the agent. While it is possible to keep state in the agent’s lifecycle, this is lost when the agent is stopped and must be rebuilt each time the agent is started. Using a Value Store, the state is retreived from the underlying persistence engine when the agent first starts.

Handler Context Operations

The HandlerContext provided as an argument to lifecycle event handlers provides access to two functions which are used to retreive or update the state of the store.

Further reading

It is recommended to understand server persistence to understand how stores are loaded and how state changes are persisted.

Try It Yourself

A standalone project that demonstrates Value Stores is available here.