Skip to main content

State

Restate offers durable storage of application state, with support for key-value state that can be retrieved, set, and cleared via its key. Using this feature requires no additional setup.

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.

Listing state keys​

To list all the keys that have entries in the state store for that invocation (and its keyed service instance), do:

const stateKeys = ctx.stateKeys();

Retrieving state​

To retrieve state in Restate, you can do the following:

const myStringValue = await ctx.get<string>("my-key") ?? "my-default";

Replace my-key with the key you want to retrieve. This will either return the value that was stored or it will return null if no value was stored.

In this code snippet, the value stored is a string, but you can store any type that can be serialized as a buffer with Buffer.from(JSON.stringify(yourObject)).

For example, to retrieve a number type, you could use the following code:

const myNbValue = await ctx.get<number>("my-key") ?? 0;

This would retrieve the value of the my-key key. If the value is null, then it will return 0.

Setting state​

To set a key-value pair in Restate, do:

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

The value can be of any type that can be serialized as a Buffer.

Replace my-key with the key that you are setting. And replace my-new-value with the value that you want to set.

Clearing state​

To delete the value of a key in Restate, do:

ctx.clear("my-key");

Replace my-key with the key that you are setting.

To delete all K/V state entries for the invocation (and its keyed service instance), do:

ctx.clearAll();