Tutorial: Configure state store and pub/sub message broker

    To get up and running with the state and Pub/sub building blocks, you’ll need two components:

    • A state store component for persistence and restoration.
    • As pub/sub message broker component for async-style message delivery.

    A full list of supported components can be found here:

    For this tutorial, we describe how to get up and running with Redis.

    Dapr can use any Redis instance, either:

    • Containerized on your local dev machine, or
    • A managed cloud service.

    If you already have a Redis store, move on to the configuration section.

    Redis is automatically installed in self-hosted environments by the Dapr CLI as part of the initialization process. You are all set! Skip ahead to the next steps.

    You can use to create a Redis instance in our Kubernetes cluster. Before beginning, install Helm v3.

    Install Redis into your cluster:

    For Dapr’s Pub/sub functionality, you’ll need at least Redis version 5. For state store, you can use a lower version. Note that adding to the install command creates a single replica Redis setup, which can save memory and resources if you are working in a local environment.

    Run kubectl get pods to see the Redis containers now running in your cluster:

    1. $ kubectl get pods
    2. NAME READY STATUS RESTARTS AGE
    3. redis-master-0 1/1 Running 0 69s
    4. redis-replicas-0 1/1 Running 0 69s
    5. redis-replicas-1 1/1 Running 0 22s

    For Kubernetes:

    • The hostname is redis-master.default.svc.cluster.local:6379
    • The secret, redis, is created automatically.
    1. Open and log into the to start the Azure Redis Cache creation flow.
    2. Fill out the necessary information.
      • Dapr Pub/sub uses Redis streams introduced by Redis 5.0. To use Azure Redis Cache for Pub/sub, set the version to (PREVIEW) 6.
    3. Click Create to kickoff deployment of your Redis instance.
    4. Make note of the Redis instance hostname from the Overview page in Azure portal for later.
      • It should look like xxxxxx.redis.cache.windows.net:6380.
    5. Once your instance is created, grab your access key:

      1. Navigate to Access Keys under Settings.

      2. Create a Kubernetes secret to store your Redis password:

        1. kubectl create secret generic redis --from-literal=redis-password=*********
    1. Deploy a Redis instance from .

    2. Note the Redis hostname in the AWS portal for later.

    3. Create a Kubernetes secret to store your Redis password:

      1. kubectl create secret generic redis --from-literal=redis-password=*********
    1. Deploy a MemoryStore instance from GCP Cloud MemoryStore.

    2. Note the Redis hostname in the GCP portal for later.

    3. Create a Kubernetes secret to store your Redis password:

    Dapr defines resources to use for building block functionality with components. These steps go through how to connect the resources you created above to Dapr for state and pub/sub.

    Locate your component files

    In self-hosted mode, component files are automatically created under:

    • Windows: %USERPROFILE%\.dapr\components\

    Create State store component

    Create a file named redis-state.yaml, and paste the following:

    1. apiVersion: dapr.io/v1alpha1
    2. kind: Component
    3. metadata:
    4. name: statestore
    5. namespace: default
    6. spec:
    7. type: state.redis
    8. version: v1
    9. metadata:
    10. - name: redisHost
    11. value: localhost:6379
    12. - name: redisPassword
    13. secretKeyRef:
    14. name: redis
    15. key: redis-password
    16. # uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
    17. # - name: enableTLS
    18. # value: true
    1. apiVersion: dapr.io/v1alpha1
    2. kind: Component
    3. metadata:
    4. namespace: default
    5. spec:
    6. type: state.redis
    7. version: v1
    8. metadata:
    9. - name: redisHost
    10. value: <REPLACE WITH HOSTNAME FROM ABOVE - for Redis on Kubernetes it is redis-master.default.svc.cluster.local:6379>
    11. - name: redisPassword
    12. secretKeyRef:
    13. name: redis
    14. key: redis-password
    15. # uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
    16. # - name: enableTLS
    17. # value: true

    Note the above code example uses the Kubernetes secret you created earlier when setting up a cluster.

    Other stores

    If using a state store other than Redis, refer to the for information on options to set.

    Create Pub/sub message broker component

    Create a file called redis-pubsub.yaml, and paste the following:

    1. apiVersion: dapr.io/v1alpha1
    2. kind: Component
    3. metadata:
    4. name: pubsub
    5. namespace: default
    6. spec:
    7. type: pubsub.redis
    8. version: v1
    9. - name: redisHost
    10. value: localhost:6379
    11. - name: redisPassword
    12. secretKeyRef:
    13. name: redis
    14. key: redis-password
    15. # uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
    16. # - name: enableTLS
    17. # value: true

    Note the above code example uses the Kubernetes secret you created earlier when setting up a cluster.

    Other stores

    If using a pub/sub message broker other than Redis, refer to the supported pub/sub message brokers for information on options to set.

    For development purposes only, you can skip creating Kubernetes secrets and place passwords directly into the Dapr component file:

    1. apiVersion: dapr.io/v1alpha1
    2. kind: Component
    3. metadata:
    4. name: statestore
    5. spec:
    6. type: state.redis
    7. version: v1
    8. metadata:
    9. - name: redisHost
    10. value: <HOST>
    11. - name: redisPassword
    12. value: <PASSWORD>
    13. # uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
    14. # - name: enableTLS
    15. # value: true
    1. apiVersion: dapr.io/v1alpha1
    2. kind: Component
    3. metadata:
    4. name: pubsub
    5. namespace: default
    6. spec:
    7. type: pubsub.redis
    8. version: v1
    9. metadata:
    10. - name: redisHost
    11. value: <HOST>
    12. - name: redisPassword
    13. value: <PASSWORD>
    14. # uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)
    15. # - name: enableTLS
    16. # value: true

    When you run dapr init, Dapr creates a default redis pubsub.yaml on your local machine. Verify by opening your components directory:

    • On Windows, under %UserProfile%\.dapr\components\pubsub.yaml
    • On Linux/MacOS, under ~/.dapr/components/pubsub.yaml

    For new component files:

    1. Create a new components directory in your app folder containing the YAML files.
    2. Provide the path to the dapr run command with the flag --resources-path

    If you initialized Dapr in (without Docker), you need to manually create the default directory, or always specify a components directory using --resources-path.

    Run kubectl apply -f <FILENAME> for both state and pubsub files:

    1. kubectl apply -f redis-state.yaml