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 and Workflows:

  • For Virtual Objects, the state is isolated per Virtual Object and lives forever (across invocations for that object).
  • For Workflows, you can think of it as if every workflow execution is a new object. So the state is isolated to a single workflow execution. The state can only be mutated by the run handler of the workflow. The other handlers can only read the state.
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 return any payload that can be serialized with bytes(json.dumps(obj), "utf-8") and deserialized with json.loads(buf). If not, you need to specify a custom serializer.

Listing state keys

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

state_keys = ctx.state_keys()

Retrieving state

Use ctx.get to retrieve the state for a key:

my_string = await ctx.get("my-string-key") or "default-key"
my_number = await ctx.get("my-number-key") or 123

The return value is None if no value was stored.

Setting state

Use ctx.set to set a new value for a key:

ctx.set("my-key", "my-new-value")

Clearing state

Use ctx.clear to delete the value of a key:

ctx.clear("my-key")

Clearing all state

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

ctx.clear_all()