Your application can use Dapr’s state management API to save, read, and query key/value pairs in the supported state stores. Using a state store component, you can build stateful, long running applications that save and retrieve their state (like a shopping cart or a game’s session state). For example, in the diagram below:
The following overview video and demo demonstrates how Dapr state management works.
With the state management API building block, your application can leverage features that are typically complicated and error-prone to build, including:
These are the features available as part of the state management API:
Dapr data stores are modeled as components, which can be swapped out without any changes to your service code. See supported state stores to see the list.
With Dapr, you can include additional metadata in a state operation request that describes how you expect the request to be handled. You can attach:
By default, your application should assume a data store is eventually consistent and uses a last-write-wins concurrency pattern.
Not all stores are created equal. To ensure your application’s portability, you can query the metadata capabilities of the store and make your code adaptive to different store capabilities.
Dapr supports Optimistic Concurrency Control (OCC) using ETags. When a state value is requested, Dapr always attaches an ETag property to the returned state. When the user code:
If-Match
header.The write
operation succeeds when the provided ETag matches the ETag in the state store.
Data update conflicts are rare in many applications, since clients are naturally partitioned by business contexts to operate on different data. However, if your application chooses to use ETags, mismatched ETags may cause a request rejection. It’s recommended you use a retry policy in your code to compensate for conflicts when using ETags.
If your application omits ETags in writing requests, Dapr skips ETag checks while handling the requests. This enables the last-write-wins pattern, compared to the first-write-wins pattern with ETags.
Read the API reference to learn how to set concurrency options.
Dapr supports both strong consistency and eventual consistency, with eventual consistency as the default behavior.
Read the API reference to learn how to set consistency options.
State store components may maintain and manipulate data differently, depending on the content type. Dapr supports passing content type in state management API as part of request metadata.
Setting the content type is optional, and the component decides whether to make use of it. Dapr only provides the means of passing this information to the component.
metadata.contentType
. For example, http://localhost:3500/v1.0/state/store?metadata.contentType=application/json
."contentType" : <content type>
to the request metadata.Dapr supports two types of multi-read or multi-write operations: bulk or transactional. Read the API reference to learn how use bulk and multi options.
You can group multiple read requests into a bulk (or batch) operation. In the bulk operation, Dapr submits the read requests as individual requests to the underlying data store, and returns them as a single result.
You can group write, update, and delete operations into a request, which are then handled as an atomic transaction. The request will succeed or fail as a transactional set of operations.
Transactional state stores can be used to store actor state. To specify which state store to use for actors, specify value of property actorStateStore
as true
in the state store component’s metadata section. Actors state is stored with a specific scheme in transactional state stores, allowing for consistent querying. Only a single state store component can be used as the state store for all actors. Read the state API reference and the actors API reference to learn more about state stores for actors.
You should always set the TTL metadata field (ttlInSeconds
), or the equivalent API call in your chosen SDK when saving actor state to ensure that state eventually removed. Read actors overview for more information.
Dapr supports automatic client encryption of application state with support for key rotations. This is supported on all Dapr state stores. For more info, read the How-To: Encrypt application state topic.
Different applications’ needs vary when it comes to sharing state. In one scenario, you may want to encapsulate all state within a given application and have Dapr manage the access for you. In another scenario, you may want two applications working on the same state to get and save the same keys.
Dapr enables states to be:
For more details read How-To: Share state between applications,
Dapr enables developers to use the outbox pattern for achieving a single transaction across a transactional state store and any message broker. For more information, read How to enable transactional outbox messaging
There are two ways to query the state:
Using the optional state management query API, you can query the key/value data saved in state stores, regardless of underlying database or storage technology. With the state management query API, you can filter, sort, and paginate the key/value data. For more details read How-To: Query state.
Dapr saves and retrieves state values without any transformation. You can query and aggregate state directly from the underlying state store. For example, to get all state keys associated with an application ID “myApp” in Redis, use:
KEYS "myApp*"
If the data store supports SQL queries, you can query an actor’s state using SQL queries. For example:
SELECT * FROM StateTable WHERE Id='<app-id>||<actor-type>||<actor-id>||<key>'
You can also avoid the common turn-based concurrency limitations of actor frameworks by performing aggregate queries across actor instances. For example, to calculate the average temperature of all thermometer actors, use:
SELECT AVG(value) FROM StateTable WHERE Id LIKE '<app-id>||<thermometer>||*||temperature'
Dapr enables per state set request time-to-live (TTL). This means that applications can set time-to-live per state stored, and these states cannot be retrieved after expiration.
The state management API can be found in the state management API reference, which describes how to retrieve, save, delete, and query state values by providing keys.
Want to put the Dapr state management API to the test? Walk through the following quickstart and tutorials to see state management in action:
Quickstart/tutorial | Description |
---|---|
State management quickstart | Create stateful applications using the state management API. |
Hello World | Recommended Demonstrates how to run Dapr locally. Highlights service invocation and state management. |
Hello World Kubernetes | Recommended Demonstrates how to run Dapr in Kubernetes. Highlights service invocation and state management. |
Want to skip the quickstarts? Not a problem. You can try out the state management building block directly in your application. After Dapr is installed, you can begin using the state management API starting with the state management how-to guide.