Skip to main content

State

You can store key-value state in Restate. Restate makes sure the state is consistent with the processing of the code execution.

This feature is only available for Virtual Objects:

  • For Virtual Objects, the state is isolated per Virtual Object and lives forever (across invocations for that object).
Command-line introspection

You can inspect and edit the K/V state stored in Restate via psql and the CLI. Have a look at the introspection docs for more information.

Serializing state

You can store any type of value that can be serialized. By default, serialization is done with JSONCodec which uses encoding/json.

Listing state keys

For a single Virtual Object, you can list all the state keys that have entries in the state store via:


stateKeys, err := restate.Keys(ctx)
if err != nil {
return err
}

Retrieving state

Use restate.Get to retrieve the state for a key:


myString := "my-default"
if s, err := restate.Get[*string](ctx, "my-string-key"); err != nil {
return err
} else if s != nil {
myString = *s
}
myNumber, err := restate.Get[int](ctx, "my-number-key")
if err != nil {
return err
}

The zero value will be returned if a state value does not exist for the provided key.

Setting state

Use restate.Set to set a new value for a key:


restate.Set(ctx, "my-key", "my-new-value")

This function will only return errors in the case of failed serialization.

Clearing state

Use restate.Clear to delete the value of a key:


restate.Clear(ctx, "my-key")

Clearing all state

Delete all the state stored in Restate for a Virtual Object via:


restate.ClearAll(ctx)