Skip to content

Add clickhouse state store docs v1.16 #4697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v1.16
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
---
type: docs
title: "ClickHouse"
linkTitle: "ClickHouse"
description: Detailed information on the ClickHouse state store component
aliases:
- "/operations/components/setup-state-store/supported-state-stores/setup-clickhouse/"
---

## Component format

To setup ClickHouse state store create a component of type `state.clickhouse`. See [this guide]({{< ref "howto-get-save-state.md#step-1-setup-a-state-store" >}}) on how to create and apply a state store configuration.

```yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: <NAME>
spec:
type: state.clickhouse
version: v1
metadata:
- name: clickhouseURL
value: <CONNECTION_URL>
- name: databaseName
value: <DATABASE_NAME>
- name: tableName
value: <TABLE_NAME>
- name: username # Optional
value: <USERNAME>
- name: password # Optional
value: <PASSWORD>
```

{{% alert title="Warning" color="warning" %}}
The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described [here]({{< ref component-secrets.md >}}).
{{% /alert %}}

## Spec metadata fields

| Field | Required | Details | Example |
|--------------------|:--------:|---------|---------|
| clickhouseURL | Y | Connection URL for the ClickHouse server | `"clickhouse://localhost:9000"`, `"clickhouse://clickhouse-server:9000"` |
| databaseName | Y | Name of the database to use | `"dapr_state"`, `"my_database"` |
| tableName | Y | Name of the table to store state data | `"state_table"`, `"dapr_state_store"` |
| username | N | Username for ClickHouse authentication. Can be `secretKeyRef` to use a secret reference | `"default"`, `"my_user"` |
| password | N | Password for ClickHouse authentication. Can be `secretKeyRef` to use a secret reference | `"my_password"` |

## Setup ClickHouse

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

{{< tabs "Self-Hosted" "Kubernetes" "Cloud" >}}

{{% codetab %}}

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

```bash
docker run -d --name clickhouse-server \
-p 8123:8123 -p 9000:9000 \
-e CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1 \
-e CLICKHOUSE_PASSWORD=my_password \
clickhouse/clickhouse-server
```

2. Create a database for state data (optional, as Dapr will create it automatically):

```sql
CREATE DATABASE IF NOT EXISTS dapr_state;
```

{{% /codetab %}}

{{% codetab %}}

You can use [Helm](https://helm.sh/) to quickly create a ClickHouse instance in your Kubernetes cluster. This approach requires [Installing Helm](https://github.com/helm/helm#install).

1. Add the ClickHouse Helm repository:
```bash
helm repo add clickhouse https://docs.altinity.com/clickhouse-operator/
helm repo update
```

2. Install ClickHouse into your cluster:
```bash
helm install clickhouse clickhouse/clickhouse
```

3. Run `kubectl get pods` to see the ClickHouse containers now running in your cluster.

4. Add the ClickHouse service endpoint as the `clickhouseURL` in your component configuration. For example:
```yaml
metadata:
- name: clickhouseURL
value: "clickhouse://clickhouse:9000"
```

{{% /codetab %}}

{{% codetab %}}

ClickHouse is available as a managed service from various cloud providers:

- [ClickHouse Cloud](https://clickhouse.com/cloud)
- [Altinity.Cloud](https://altinity.com/cloud-database/)
- [Yandex Managed Service for ClickHouse](https://cloud.yandex.com/services/managed-clickhouse)

When using a managed service, ensure you have the correct connection URL, database name, and credentials configured in your component metadata.

{{% /codetab %}}

{{< /tabs >}}

## Features

The ClickHouse state store supports the following features:

### ETags

The ClickHouse state store supports [ETags]({{< ref state-management-overview.md >}}) for optimistic concurrency control. ETags are automatically generated and updated when state data is modified.

### TTL (Time-To-Live)

This state store supports [Time-To-Live (TTL)]({{< ref state-store-ttl.md >}}) 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".

Example of setting TTL:

```json
{
"key": "my-key",
"value": "my-value",
"metadata": {
"ttlInSeconds": "3600"
}
}
```

Records with expired TTLs are automatically filtered out during read operations and are eligible for cleanup by ClickHouse's background processes.

## Advanced

### Table Schema

The ClickHouse state store creates a table with the following schema:

```sql
CREATE TABLE IF NOT EXISTS <database>.<table> (
key String,
value String,
etag String,
expire DateTime64(3) NULL,
PRIMARY KEY(key)
) ENGINE = ReplacingMergeTree()
ORDER BY key
```

The table uses ClickHouse's `ReplacingMergeTree` engine, which automatically deduplicates rows with the same primary key during background merges.

### Connection URL Format

The ClickHouse connection URL follows the standard format:

```
clickhouse://[username[:password]@]host[:port][/database][?param1=value1&...&paramN=valueN]
```

Examples:
- `clickhouse://localhost:9000`
- `clickhouse://user:password@clickhouse-server:9000/my_db`
- `clickhouse://localhost:9000?dial_timeout=10s&max_execution_time=60`

### Performance Considerations

- The ClickHouse state store is optimized for high-throughput scenarios
- For better performance with large datasets, consider partitioning your table by date or other relevant columns
- The `ReplacingMergeTree` engine provides eventual consistency for duplicate key handling
- Background merges in ClickHouse will automatically clean up old versions of updated records

### Bulk Operations

The ClickHouse state store supports bulk operations for improved performance:

- `BulkGet`: Retrieve multiple keys in a single operation
- `BulkSet`: Store multiple key-value pairs in a single operation
- `BulkDelete`: Delete multiple keys in a single operation

## Related links

- [Basic schema for a Dapr component]({{< ref component-schema >}})
- Read [this guide]({{< ref "howto-get-save-state.md#step-2-save-and-retrieve-a-single-state" >}}) for instructions on configuring state store components
- [State management building block]({{< ref state-management >}})
- [ClickHouse Official Documentation](https://clickhouse.com/docs)
11 changes: 11 additions & 0 deletions daprdocs/data/components/state_stores/generic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
etag: false
ttl: true
query: false
- component: ClickHouse
link: setup-clickhouse
state: Alpha
version: v1
since: "1.16"
features:
crud: true
transactions: false
etag: true
ttl: true
query: false
- component: CockroachDB
link: setup-cockroachdb
state: Stable
Expand Down
2 changes: 1 addition & 1 deletion sdkdocs/python
Submodule python updated 46 files
+2 −0 README.md
+60 −0 dapr/aio/clients/grpc/client.py
+10 −0 dapr/clients/grpc/_request.py
+17 −0 dapr/clients/grpc/_response.py
+60 −0 dapr/clients/grpc/client.py
+1 −1 dapr/clients/grpc/subscription.py
+38 −30 dapr/proto/common/v1/common_pb2.pyi
+87 −69 dapr/proto/runtime/v1/appcallback_pb2.pyi
+1 −1 dapr/proto/runtime/v1/dapr_pb2.py
+397 −337 dapr/proto/runtime/v1/dapr_pb2.pyi
+1 −1 dapr/version/version.py
+1 −1 daprdocs/content/en/python-sdk-docs/python-actor.md
+82 −84 daprdocs/content/en/python-sdk-docs/python-client.md
+69 −6 daprdocs/content/en/python-sdk-docs/python-sdk-extensions/python-workflow-ext/_index.md
+110 −126 daprdocs/content/en/python-sdk-docs/python-sdk-extensions/python-workflow-ext/python-workflow.md
+1 −1 dev-requirements.txt
+34 −0 examples/conversation/README.md
+7 −0 examples/conversation/config/conversation-echo.yaml
+33 −0 examples/conversation/conversation.py
+1 −1 examples/demo_actor/demo_actor/requirements.txt
+2 −0 examples/demo_workflow/app.py
+1 −1 examples/demo_workflow/demo_workflow/requirements.txt
+1 −1 examples/error_handling/README.md
+2 −2 examples/invoke-simple/requirements.txt
+10 −1 examples/pubsub-simple/README.md
+48 −0 examples/pubsub-simple/publisher.py
+36 −0 examples/pubsub-simple/subscriber.py
+2 −2 examples/w3c-tracing/requirements.txt
+66 −0 examples/workflow/README.md
+1 −0 examples/workflow/monitor.py
+2 −2 examples/workflow/requirements.txt
+170 −0 examples/workflow/simple.py
+1 −1 ext/dapr-ext-fastapi/dapr/ext/fastapi/version.py
+1 −1 ext/dapr-ext-fastapi/setup.cfg
+1 −1 ext/dapr-ext-grpc/dapr/ext/grpc/version.py
+1 −1 ext/dapr-ext-grpc/setup.cfg
+20 −10 ext/dapr-ext-workflow/dapr/ext/workflow/dapr_workflow_client.py
+1 −1 ext/dapr-ext-workflow/dapr/ext/workflow/version.py
+2 −2 ext/dapr-ext-workflow/setup.cfg
+37 −1 ext/dapr-ext-workflow/tests/test_workflow_client.py
+1 −1 ext/flask_dapr/flask_dapr/version.py
+1 −1 ext/flask_dapr/setup.cfg
+12 −0 tests/clients/fake_dapr_server.py
+53 −1 tests/clients/test_dapr_grpc_client.py
+52 −1 tests/clients/test_dapr_grpc_client_async.py
+1 −0 tox.ini