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. State is isolated per Virtual 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 as a Buffer with Buffer.from(JSON.stringify(yourObject)) and deserialized with JSON.parse(result.toString()) as T.

Listing state keys

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


const stateKeys = ctx.stateKeys();

Retrieving state

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


const myString = await ctx.get<string>("my-string-key") ?? "my-default";
const myNumber = await ctx.get<number>("my-number-key") ?? 0;

The return value is null 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.clearAll();