Redis

Detailed information on the Redis state store component

Component format

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

Limitations

Before using Redis and the Transactions API, make sure you’re familiar with Redis limitations regarding transactions.

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. spec:
  6. type: state.redis
  7. version: v1
  8. metadata:
  9. - name: redisHost
  10. value: <HOST>
  11. - name: redisPassword
  12. value: <PASSWORD>
  13. - name: enableTLS
  14. value: <bool> # Optional. Allowed: true, false.
  15. - name: maxRetries
  16. value: # Optional
  17. - name: maxRetryBackoff
  18. value: # Optional
  19. - name: failover
  20. value: <bool> # Optional. Allowed: true, false.
  21. - name: sentinelMasterName
  22. value: <string> # Optional
  23. - name: redeliverInterval
  24. value: # Optional
  25. - name: processingTimeout
  26. value: # Optional
  27. - name: redisType
  28. value: # Optional
  29. - name: redisDB
  30. value: # Optional
  31. - name: redisMaxRetries
  32. value: # Optional
  33. - name: redisMinRetryInterval
  34. value: # Optional
  35. - name: redisMaxRetryInterval
  36. value: # Optional
  37. - name: dialTimeout
  38. value: # Optional
  39. - name: readTimeout
  40. value: # Optional
  41. - name: writeTimeout
  42. value: # Optional
  43. - name: poolSize
  44. value: # Optional
  45. - name: poolTimeout
  46. value: # Optional
  47. - name: maxConnAge
  48. value: # Optional
  49. - name: minIdleConns
  50. value: # Optional
  51. - name: idleCheckFrequency
  52. value: # Optional
  53. - name: idleTimeout
  54. value: # Optional
  55. - name: ttlInSeconds
  56. value: <int> # Optional
  57. - name: queryIndexes
  58. value: <string> # Optional

Warning

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

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

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

Spec metadata fields

FieldRequiredDetailsExample
redisHostYConnection-string for the redis hostlocalhost:6379, redis-master.default.svc.cluster.local:6379
redisPasswordYPassword for Redis host. No Default. Can be secretKeyRef to use a secret reference“”, “KeFg23!”
redisUsernameNUsername for Redis host. Defaults to empty. Make sure your redis server version is 6 or above, and have created acl rule correctly.“”, “default”
enableTLSNIf the Redis instance supports TLS with public certificates, can be configured to be enabled or disabled. Defaults to “false”“true”, “false”
maxRetriesNMaximum number of retries before giving up. Defaults to 35, 10
maxRetryBackoffNMaximum backoff between each retry. Defaults to 2 seconds; “-1” disables backoff.3000000000
failoverNProperty to enabled failover configuration. Needs sentinelMasterName to be set. The redisHost should be the sentinel host address. See Redis Sentinel Documentation. Defaults to “false”“true”, “false”
sentinelMasterNameNThe sentinel master name. See Redis Sentinel Documentation“”, “127.0.0.1:6379”
redeliverIntervalNThe interval between checking for pending messages to redelivery. Defaults to “60s”. “0” disables redelivery.“30s”
processingTimeoutNThe amount time a message must be pending before attempting to redeliver it. Defaults to “15s”. “0” disables redelivery.“30s”
redisTypeNThe type of redis. There are two valid values, one is “node” for single node mode, the other is “cluster” for redis cluster mode. Defaults to “node”.“cluster”
redisDBNDatabase selected after connecting to redis. If “redisType” is “cluster” this option is ignored. Defaults to “0”.“0”
redisMaxRetriesNAlias for maxRetries. If both values are set maxRetries is ignored.“5”
redisMinRetryIntervalNMinimum backoff for redis commands between each retry. Default is “8ms”; “-1” disables backoff.“8ms”
redisMaxRetryIntervalNAlias for maxRetryBackoff. If both values are set maxRetryBackoff is ignored.“5s”
dialTimeoutNDial timeout for establishing new connections. Defaults to “5s”.“5s”
readTimeoutNTimeout for socket reads. If reached, redis commands will fail with a timeout instead of blocking. Defaults to “3s”, “-1” for no timeout.“3s”
writeTimeoutNTimeout for socket writes. If reached, redis commands will fail with a timeout instead of blocking. Defaults is readTimeout.“3s”
poolSizeNMaximum number of socket connections. Default is 10 connections per every CPU as reported by runtime.NumCPU.“20”
poolTimeoutNAmount of time client waits for a connection if all connections are busy before returning an error. Default is readTimeout + 1 second.“5s”
maxConnAgeNConnection age at which the client retires (closes) the connection. Default is to not close aged connections.“30m”
minIdleConnsNMinimum number of idle connections to keep open in order to avoid the performance degradation associated with creating new connections. Defaults to “0”.“2”
idleCheckFrequencyNFrequency of idle checks made by idle connections reaper. Default is “1m”. “-1” disables idle connections reaper.“-1”
idleTimeoutNAmount of time after which the client closes idle connections. Should be less than server’s timeout. Default is “5m”. “-1” disables idle timeout check.“10m”
actorStateStoreNConsider this state store for actors. Defaults to “false”“true”, “false”
ttlInSecondsNAllows specifying a default Time-to-live (TTL) in seconds that will be applied to every state store request unless TTL is explicitly defined via the request metadata.600
queryIndexesNIndexing schemas for querying JSON objectssee Querying JSON objects

Setup Redis

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

You can use Helm 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 redis.yaml file. 例如:

    1. metadata:
    2. - name: redisHost
    3. value: redis-master:6379
  4. Next, 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 creates 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. 例如:

    1. metadata:
    2. - name: redisPassword
    3. value: lhDOkwTlp0

Note: this approach requires having an Azure Subscription.

  1. 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 resource’s “Overview” and copy “Host name”.
    • For your access key: navigate to “Settings” > “Access Keys” to copy and save your key.
  5. Add your key and your host to a redis.yaml file that Dapr can apply to your cluster.

    • If you’re running a sample, add the host and key to the provided redis.yaml.
    • If you’re creating a project from the ground up, 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 saved earlier.

    Note: In a production-grade application, follow secret management instructions to securely manage your secrets.

NOTE: Dapr pub/sub uses Redis Streams that was introduced by Redis 5.0, which isn’t currently available on Azure Managed Redis Cache. Consequently, you can use Azure Managed Redis Cache only for state persistence.

AWS Redis

GCP Cloud MemoryStore

Querying JSON objects (optional)

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 redisjson 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. {
  3. "name": "<indexing name>",
  4. "indexes": [
  5. {
  6. "key": "<JSONPath-like syntax for selected element inside documents>",
  7. "type": "<value type (supported types: TEXT, NUMERIC)>",
  8. },
  9. ...
  10. ]
  11. },
  12. ...
  13. ]
  1. When calling state management API, add the following metadata to the API calls:
  • Save State, Get 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
  • Query State:
    • add metadata.contentType=application/json&metadata.queryIndexName=<indexing name> URL query parameters to HTTP API request
    • add "contentType" : "application/json" 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. }
  11. }

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

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.redis
  7. version: v1
  8. initTimeout: 1m
  9. metadata:
  10. - name: redisHost
  11. value: "localhost:6379"
  12. - name: redisPassword
  13. value: ""
  14. - name: queryIndexes
  15. value: |
  16. [
  17. {
  18. "name": "orgIndx",
  19. "indexes": [
  20. {
  21. "key": "person.org",
  22. "type": "TEXT"
  23. },
  24. {
  25. "key": "person.id",
  26. "type": "NUMERIC"
  27. },
  28. {
  29. "key": "state",
  30. "type": "TEXT"
  31. },
  32. {
  33. "key": "city",
  34. "type": "TEXT"
  35. }
  36. ]
  37. }
  38. ]

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.

Note: redislabs/rejson has support only for amd64 architecture.

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

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

Follow instructions for Redis deployment in Kubernetes with one extra detail.

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

  1. helm install redis bitnami/redis --set image.tag=6.2 -f values.yaml

where values.yaml looks like:

  1. image:
  2. repository: redislabs/rejson
  3. tag: 2.0.6
  4. master:
  5. extraFlags:
  6. - --loadmodule
  7. - /usr/lib/redis/modules/rejson.so
  8. - --loadmodule
  9. - /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 Redis deployment in AWS.

Note

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

Note

  1. Memory Store does not support modules and cannot be used with query.

Redis Enterprise Cloud

Alibaba 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 --resources-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 query:

  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. curl -s -X POST -H "Content-Type: application/json" -d @query-api-examples/query1.json \
  2. 'http://localhost:3500/v1.0-alpha1/state/querystatestore/query?metadata.contentType=application/json&metadata.queryIndexName=orgIndx'

The result will be:

  1. {
  2. "results": [
  3. {
  4. "key": "3",
  5. "data": {
  6. "person": {
  7. "org": "Finance",
  8. "id": 1071
  9. },
  10. "city": "Sacramento",
  11. "state": "CA"
  12. },
  13. "etag": "1"
  14. },
  15. {
  16. "key": "7",
  17. "data": {
  18. "person": {
  19. "org": "Dev Ops",
  20. "id": 1015
  21. },
  22. "city": "San Francisco",
  23. "state": "CA"
  24. },
  25. "etag": "1"
  26. },
  27. {
  28. "key": "5",
  29. "data": {
  30. "person": {
  31. "org": "Hardware",
  32. "id": 1007
  33. },
  34. "city": "Los Angeles",
  35. "state": "CA"
  36. },
  37. "etag": "1"
  38. },
  39. {
  40. "key": "9",
  41. "data": {
  42. "person": {
  43. "org": "Finance",
  44. "id": 1002
  45. },
  46. "city": "San Diego",
  47. "state": "CA"
  48. },
  49. "etag": "1"
  50. }
  51. ]
  52. }

The query syntax and documentation is available here