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.
- name: actorStateStore
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.
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.
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install redis bitnami/redis
Run
kubectl get pods
to see the Redis containers now running in your cluster.Add
redis-master:6379
as theredisHost
in your file. For example:metadata:
- name: redisHost
value: redis-master:6379
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, runcertutil -decode encoded.b64 password.txt
, which will put your redis password in a text file calledpassword.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:metadata:
- name: redisPassword
value: lhDOkwTlp0
- Open to start the Azure Cache for Redis creation flow. Log in if necessary.
- Fill out necessary information and check the “Unblock port 6379” box, which will allow us to persist state without SSL.
- Click “Create” to kickoff deployment of your Redis instance.
- 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.
- 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 providedredis.yaml
. If you’re creating a project from the ground up, you’ll create aredis.yaml
file as specified in Configuration. Set theredisHost
key to[HOST NAME FROM PREVIOUS STEP]:6379
and theredisPassword
key to the key you copied in step 4. Note: In a production-grade application, follow instructions to securely manage your secrets.
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:
- 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. ``
- Specify
queryIndexes
entry in the metadata of the component config. The value of thequeryIndexes
is a JSON array of the following format:
{
"name": "<indexing name>",
"indexes": [
{
"key": "<JSONPath-like syntax for selected element inside documents>",
"type": "<value type (supported types: TEXT, NUMERIC)>",
},
...
]
},
...
]
- 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
- :
- 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
- add
Consider an example where you store documents like that:
{
"key": "1",
"value": {
"person": {
"org": "Dev Ops",
"id": 1036
},
"city": "Seattle",
"state": "WA"
}
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:
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.
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:
helm install redis bitnami/redis -f values.yaml
where values.yaml
looks like:
repository: redislabs/rejson
tag: 2.0.6
master:
extraFlags:
- --loadmodule
- /usr/lib/redis/modules/rejson.so
- --loadmodule
- /usr/lib/redis/modules/redisearch.so
Note
Azure Redis managed service does not support the RedisJson module and cannot be used with query.
Follow instructions for .
Note
For query support you need to enable RediSearch and RedisJson.
Note
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.
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.
curl -X POST -H "Content-Type: application/json" -d @query-api-examples/dataset.json \
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
curl http://localhost:3500/v1.0/state/querystatestore/1?metadata.contentType=application/json
The result will be:
{
"city": "Seattle",
"state": "WA",
"person": {
"org": "Dev Ops",
"id": 1036
}
}
Now, let’s find all employees in the state of California and sort them by their employee ID in descending order.
This is the :
{
"filter": {
"EQ": { "state": "CA" }
},
"sort": [
{
"key": "person.id",
"order": "DESC"
}
]
}
Execute the query with the following command:
'http://localhost:3500/v1.0-alpha1/state/querystatestore/query?metadata.contentType=application/json&metadata.queryIndexName=orgIndx'
The result will be:
- Basic schema for a Dapr component
- Read for instructions on configuring state store components
- State management building block