CockroachDB

Detailed information on the CockroachDB state store component

Create a Dapr component

Create a file called cockroachdb.yaml, paste the following and replace the <CONNECTION STRING> value with your connection string. The connection string for CockroachDB follow the same standard for PostgreSQL connection string. For example, "host=localhost user=root port=26257 connect_timeout=10 database=dapr_test". See the CockroachDB documentation on database connections for information on how to define a connection string.

If you want to also configure CockroachDB to store actors, add the actorStateStore option as in the example below.

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. spec:
  6. type: state.cockroachdb
  7. version: v1
  8. metadata:
  9. # Connection string
  10. - name: connectionString
  11. value: "<CONNECTION STRING>"
  12. # Timeout for database operations, in seconds (optional)
  13. #- name: timeoutInSeconds
  14. # value: 20
  15. # Name of the table where to store the state (optional)
  16. #- name: tableName
  17. # value: "state"
  18. # Name of the table where to store metadata used by Dapr (optional)
  19. #- name: metadataTableName
  20. # value: "dapr_metadata"
  21. # Cleanup interval in seconds, to remove expired rows (optional)
  22. #- name: cleanupIntervalInSeconds
  23. # value: 3600
  24. # Max idle time for connections before they're closed (optional)
  25. #- name: connectionMaxIdleTime
  26. # value: 0
  27. # Uncomment this if you wish to use CockroachDB as a state store for actors (optional)
  28. #- name: actorStateStore
  29. # value: "true"

Warning

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

Spec metadata fields

FieldRequiredDetailsExample
connectionStringYThe connection string for CockroachDB“host=localhost user=root port=26257 connect_timeout=10 database=dapr_test”
timeoutInSecondsNTimeout, in seconds, for all database operations. Defaults to 2030
tableNameNName of the table where the data is stored. Defaults to state. Can optionally have the schema name as prefix, such as public.state“state”, “public.state”
metadataTableNameNName of the table Dapr uses to store a few metadata properties. Defaults to dapr_metadata. Can optionally have the schema name as prefix, such as public.dapr_metadata“dapr_metadata”, “public.dapr_metadata”
cleanupIntervalInSecondsNInterval, in seconds, to clean up rows with an expired TTL. Default: 3600 (i.e. 1 hour). Setting this to values <=0 disables the periodic cleanup.1800, -1
connectionMaxIdleTimeNMax idle time before unused connections are automatically closed in the connection pool. By default, there’s no value and this is left to the database driver to choose.“5m”
actorStateStoreNConsider this state store for actors. Defaults to “false”“true”, “false”

Setup CockroachDB

  1. Run an instance of CockroachDB. You can run a local instance of CockroachDB in Docker CE with the following command:

    This example does not describe a production configuration because it sets a single-node cluster, it’s only recommend for local environment.

    1. docker run --name roach1 -p 26257:26257 cockroachdb/cockroach:v21.2.3 start-single-node --insecure
  2. Create a database for state data.

    To create a new database in CockroachDB, run the following SQL command inside container:

    1. docker exec -it roach1 ./cockroach sql --insecure -e 'create database dapr_test'

The easiest way to install CockroachDB on Kubernetes is by using the CockroachDB Operator:

Advanced

TTLs and cleanups

This state store supports Time-To-Live (TTL) for records stored with Dapr. When storing data using Dapr, you can set the ttlInSeconds metadata property to indicate after how many seconds the data should be considered “expired”.

Because CockroachDB doesn’t have built-in support for TTLs, you implement this in Dapr by adding a column in the state table indicating when the data should be considered “expired”. “Expired” records are not returned to the caller, even if they’re still physically stored in the database. A background “garbage collector” periodically scans the state table for expired rows and deletes them.

You can set the interval for the deletion of expired records with the cleanupIntervalInSeconds metadata property, which defaults to 3600 seconds (that is, 1 hour).

  • Longer intervals require less frequent scans for expired rows, but can require storing expired records for longer, potentially requiring more storage space. If you plan to store many records in your state table, with short TTLs, consider setting cleanupIntervalInSeconds to a smaller value - for example, 300 (300 seconds, or 5 minutes).
  • If you do not plan to use TTLs with Dapr and the CockroachDB state store, you should consider setting cleanupIntervalInSeconds to a value <= 0 (e.g. 0 or -1) to disable the periodic cleanup and reduce the load on the database.

Last modified March 21, 2024: Merge pull request #4082 from newbe36524/v1.13 (f4b0938)