Redis

To setup Redis state store create a component of type . See on how to create and apply a state store configuration.

Warning

The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described .

If you wish to use Redis as an actor store, append the following to the yaml.

  1. - name: actorStateStore
  2. value: "true"

Dapr can use any Redis instance - containerized, running on your local dev machine, or a managed cloud service.

A Redis instance is automatically created as a Docker container when you run dapr init

We can use to quickly create a Redis instance in our Kubernetes cluster. This approach requires Installing Helm.

  1. Install Redis into your cluster. Note that we’re explicitly setting an image tag to get a version greater than 5, which is what Dapr’ pub/sub functionality requires. If you’re intending on using Redis as just a state store (and not for pub/sub), you do not have to set the image version.

    1. helm repo add bitnami https://charts.bitnami.com/bitnami
    2. helm install redis bitnami/redis
  2. Run kubectl get pods to see the Redis containers now running in your cluster.

  3. Add redis-master:6379 as the redisHost in your file. For example:

    1. metadata:
    2. - name: redisHost
    3. value: redis-master:6379
  4. Next, we’ll get the Redis password, which is slightly different depending on the OS we’re using:

    • Windows: Run kubectl get secret --namespace default redis -o jsonpath="{.data.redis-password}" > encoded.b64, which will create a file with your encoded password. Next, run certutil -decode encoded.b64 password.txt, which will put your redis password in a text file called password.txt. Copy the password and delete the two files.

    • Linux/MacOS: Run kubectl get secret --namespace default redis -o jsonpath="{.data.redis-password}" | base64 --decode and copy the outputted password.

    Add this password as the redisPassword value in your redis.yaml file. For example:

    1. metadata:
    2. - name: redisPassword
    3. value: lhDOkwTlp0
  1. Open to start the Azure Cache for Redis creation flow. Log in if necessary.
  2. Fill out necessary information and check the “Unblock port 6379” box, which will allow us to persist state without SSL.
  3. Click “Create” to kickoff deployment of your Redis instance.
  4. Once your instance is created, you’ll need to grab the Host name (FQDN) and your access key.
    • for the Host name navigate to the resources “Overview” and copy “Host name”
    • for your access key navigate to “Access Keys” under “Settings” and copy your key.
  5. Finally, we need to add our key and our host to a redis.yaml file that Dapr can apply to our cluster. If you’re running a sample, you’ll add the host and key to the provided redis.yaml. If you’re creating a project from the ground up, you’ll create a redis.yaml file as specified in Configuration. Set the redisHost key to [HOST NAME FROM PREVIOUS STEP]:6379 and the redisPassword key to the key you copied in step 4. Note: In a production-grade application, follow instructions to securely manage your secrets.

AWS Redis

In addition to supporting storing and querying state data as key/value pairs, the Redis state store optionally supports querying of JSON objects to meet more complex querying or filtering requirements. To enable this feature, the following steps are required:

  1. The Redis store must support Redis modules and specifically both Redisearch and RedisJson. If you are deploying and running Redis then load redisearch and modules when deploying the Redis service. ``
  2. Specify queryIndexes entry in the metadata of the component config. The value of the queryIndexes is a JSON array of the following format:
  1. {
  2. "name": "<indexing name>",
  3. "indexes": [
  4. {
  5. "key": "<JSONPath-like syntax for selected element inside documents>",
  6. "type": "<value type (supported types: TEXT, NUMERIC)>",
  7. },
  8. ...
  9. ]
  10. },
  11. ...
  12. ]
  1. When calling state management API, add the following metadata to the API calls:
  • Save State, , Delete State:
    • add metadata.contentType=application/json URL query parameter to HTTP API request
    • add "contentType": "application/json" pair to the metadata of gRPC API request
  • :
    • add metadata.contentType=application/json&metadata.queryIndexName=<indexing name> URL query parameters to HTTP API request
    • add and "queryIndexName" : "<indexing name>" pairs to the metadata of gRPC API request

Consider an example where you store documents like that:

  1. {
  2. "key": "1",
  3. "value": {
  4. "person": {
  5. "org": "Dev Ops",
  6. "id": 1036
  7. },
  8. "city": "Seattle",
  9. "state": "WA"
  10. }

The component config file containing corresponding indexing schema looks like that:

Consecutively, you can now store, retrieve, and query these documents.

Consider the example from “How-To: Query state” guide. Let’s run it with Redis.

If you are using a self-hosted deployment of Dapr, a Redis instance without the JSON module is automatically created as a Docker container when you run dapr init.

Alternatively, you can create an instance of Redis by running the following command:

  1. docker run -p 6379:6379 --name redis --rm redis

The Redis container that gets created on dapr init or via the above command, cannot be used with state store query API alone. You can run redislabs/rejson docker image on a different port(than the already installed Redis is using) to work with they query API.

Use following command to create an instance of redis compatiable with query API.

  1. docker run -p 9445:9445 --name rejson --rm redislabs/rejson:2.0.6

When installing Redis Helm package, provide a configuration file that specifies container image and enables required modules:

  1. helm install redis bitnami/redis -f values.yaml

where values.yaml looks like:

  1. repository: redislabs/rejson
  2. tag: 2.0.6
  3. master:
  4. extraFlags:
  5. - --loadmodule
  6. - /usr/lib/redis/modules/rejson.so
  7. - --loadmodule
  8. - /usr/lib/redis/modules/redisearch.so

Note

  1. Azure Redis managed service does not support the RedisJson module and cannot be used with query.

Follow instructions for .

Note

  1. For query support you need to enable RediSearch and RedisJson.

Note

Redis Enterprise Cloud

Next is to start a Dapr application. Refer to this component configuration file, which contains query indexing schemas. Make sure to modify the redisHost to reflect the local forwarding port which redislabs/rejson uses.

  1. dapr run --app-id demo --dapr-http-port 3500 --components-path query-api-examples/components/redis

Now populate the state store with the employee dataset, so you can then query it later.

  1. curl -X POST -H "Content-Type: application/json" -d @query-api-examples/dataset.json \
  2. http://localhost:3500/v1.0/state/querystatestore?metadata.contentType=application/json

To make sure the data has been properly stored, you can retrieve a specific object

  1. curl http://localhost:3500/v1.0/state/querystatestore/1?metadata.contentType=application/json

The result will be:

  1. {
  2. "city": "Seattle",
  3. "state": "WA",
  4. "person": {
  5. "org": "Dev Ops",
  6. "id": 1036
  7. }
  8. }

Now, let’s find all employees in the state of California and sort them by their employee ID in descending order.

This is the :

  1. {
  2. "filter": {
  3. "EQ": { "state": "CA" }
  4. },
  5. "sort": [
  6. {
  7. "key": "person.id",
  8. "order": "DESC"
  9. }
  10. ]
  11. }

Execute the query with the following command:

  1. 'http://localhost:3500/v1.0-alpha1/state/querystatestore/query?metadata.contentType=application/json&metadata.queryIndexName=orgIndx'

The result will be: