Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

docs(clickhouse): add Kafka engine settings baseline, refresh cluster… (#103192)

Bryan Ciaraldi38ff3ee1d39c
diff --git a/.agents/skills/clickhouse-migrations/SKILL.md b/.agents/skills/clickhouse-migrations/SKILL.md
index 0ec874a8a882..0769619440ee 100644
--- a/.agents/skills/clickhouse-migrations/SKILL.md
+++ b/.agents/skills/clickhouse-migrations/SKILL.md
@@ -54,6 +54,29 @@ Distributed engine:
- Sharded: `Distributed(data_table="sharded_events", sharding_key="sipHash64(person_id)")`
- Non-sharded: `Distributed(data_table="my_table", cluster=settings.CLICKHOUSE_SINGLE_SHARD_CLUSTER)`
+Kafka engine — `kafka_engine()` returns the ENGINE clause only, so every table appends its own
+`SETTINGS`. Start from this baseline:
+
+```sql
+SETTINGS kafka_skip_broken_messages = 100,
+ kafka_num_consumers = {kafka_num_consumers(1)},
+ kafka_thread_per_consumer = 1,
+ kafka_poll_timeout_ms = 10000,
+ kafka_max_block_size = 100000
+```
+
+- `kafka_skip_broken_messages` defaults to 0, where one malformed message stops the consumer for good.
+- `kafka_poll_timeout_ms` is 10000 for WarpStream, which does not support `fetch.min.bytes`.
+- `kafka_max_block_size` defaults to about a million rows, so 100000 is a reduction; lower for wide
+ rows, not below ~1000.
+- `kafka_num_consumers` is per node, so the role's node count multiplies it; the total must not
+ exceed the topic's partitions.
+- Columns and settings are fixed at creation: drop and recreate the table with its MV, no `SYNC`.
+ `ALTER TABLE <mv> MODIFY QUERY` covers an MV-SELECT-only change. Never for
+ `kafka_events_json_ws` / `events_json_ws_mv` — see Critical rules.
+
+`posthog/clickhouse/migrations/AGENTS.md` has the sizing rules and the rest.
+
### Critical rules
- NEVER use `ON CLUSTER` clause in SQL statements
diff --git a/posthog/clickhouse/migrations/AGENTS.md b/posthog/clickhouse/migrations/AGENTS.md
index d60dc60b66f7..2133f1376581 100644
--- a/posthog/clickhouse/migrations/AGENTS.md
+++ b/posthog/clickhouse/migrations/AGENTS.md
@@ -4,42 +4,23 @@ PostHog ClickHouse setup
## Environments
-There are 3 environments:
+There are 3 environments: Development, US Production, and EU Production. All three run the same
+roles and the same schema, and differ only in size, so a migration targets a role in `NodeRole` and
+never a count.
-- Development
-- US Production
-- EU Production
+## Clusters
-## US Production
+Every environment runs one main cluster plus the same satellites and ingestion nodes.
-The main clusters are:
+- Main cluster (`posthog`) — sharded, several shards, multiple replicas per shard. Holds the
+ sharded tables and is the default target for a migration.
+- Satellites — `ai_events`, `aux`, `batch_exports`, `endpoints`, `logs`, `sessions`, `ops`. Each is
+ a single shard with multiple replicas, and owns the tables for one product area.
+- Ingestion nodes — `ingestion-events`, `ingestion-medium`, `ingestion-small`. Stateless, one shard
+ per node, no replicas. See Ingestion nodes below.
-- 30 worker nodes (10x3)
- - 10 shards
- - 3 replicas
-- ai_events 1x2
-- aux 1x2
-- batch_exports 1x2
-- endpoints 1x2
-- logs 1x2
-- sessions 1x2
-- ops 1x2
-
-## Development
-
-Development mirrors the US Production layout — the main sharded cluster plus the same
-satellite clusters — at reduced node counts. Schema parity between Development and US
-Production is expected; migrations target both the same way.
-
-## EU Production
-
-The main cluster is:
-
-- 24 worker nodes - all tables are defined on worker nodes
- - 8 shards
- - 3 replicas
-
-Additionally, on k8s we have stateless nodes:
+Shard and replica counts differ per environment and change over time, so nothing in a migration may
+depend on them. Read `system.clusters` when you need the current shape.
## Ingestion nodes
@@ -312,6 +293,65 @@ run_sql_with_exceptions(
)
```
+### Required settings
+
+`kafka_engine()` returns the ENGINE clause only, so each table appends its own `SETTINGS`.
+Start from this baseline and record a reason next to anything you change:
+
+```sql
+SETTINGS kafka_skip_broken_messages = 100,
+ kafka_num_consumers = {kafka_num_consumers(1)},
+ kafka_thread_per_consumer = 1,
+ kafka_poll_timeout_ms = 10000,
+ kafka_max_block_size = 100000
+```
+
+- `kafka_skip_broken_messages` defaults to 0, where one malformed message raises on every retry of
+ the block and the consumer never advances past it. Set it on every table. Skipped rows are
+ counted in `KafkaRowsRejected` and discarded.
+- `kafka_poll_timeout_ms` defaults to under a second. WarpStream does not support
+ `fetch.min.bytes`, so a short timeout buys many small fetches instead of a few large ones.
+- `kafka_thread_per_consumer` serializes flushes when off, and changes nothing unless
+ `kafka_num_consumers` is above 1.
+- Leave `kafka_flush_interval_ms` unset; it defaults to 7500. Leave `kafka_max_wait_ms` alone, it is
+ a user-profile setting.
+
+### Sizing
+
+`kafka_max_block_size` is the row count of one insert into the target table, defaulting to about a
+million rows, so every value we set is a reduction. Larger blocks make larger parts and fewer
+merges, but each one holds memory until it flushes and its rows are invisible until then. Start at
+100000, go lower for wide rows or a view that fans one message into several targets, and stay above
+about 1000 to avoid trading tiny parts for merge pressure.
+
+`kafka_num_consumers` is per node. Nodes of a role share the topic's partitions and a consumer with
+no partition still holds a thread, so the total across the role must not exceed the partition count
+— a role on many nodes reaches that ceiling at 1. Each consumer also takes a slot in
+`background_message_broker_schedule_pool_size`, a server-level pool of 16 shared by every Kafka
+table on the node. Resolve the count through `kafka_num_consumers()` in
+`posthog/clickhouse/kafka_engine.py`, which returns 1 outside deployed cloud.
+
+### Changing a Kafka table or its materialized view
+
+Columns and settings are fixed at creation, so drop and recreate the table together with its MV, as
+migration 0307 does. Neither is replicated, so no `SYNC`. `ALTER TABLE <mv> MODIFY QUERY` covers a
+change to an MV's SELECT alone (0298, 0199, 0208). This does not apply to `kafka_events_json_ws` or
+`events_json_ws_mv`, which are never dropped or recreated — see the no-go zone caution above.
+
+With no MV attached nothing drives consumption and no offsets advance, so the cost is lag, not
+loss, provided the recreated table keeps the same `kafka_group_name`. Rows already written keep the
+old shape, so a change to how a column is derived needs a backfill or a sentinel marking pre-change
+rows, the way 0307 coalesces to `''` and 0.
+
+### The repo is not the source of truth for these settings
+
+`posthog/clickhouse/kafka_engine.py` says so in its first line, and the same table can carry
+different settings per deployment. Read the live values before changing one:
+
+```sql
+SELECT name, engine_full FROM clusterAllReplicas('all', system.tables) WHERE engine = 'Kafka'
+```
+
## Materialized views
For main cluster (legacy):
diff --git a/posthog/clickhouse/migrations/README.md b/posthog/clickhouse/migrations/README.md
index 87ec52b8bbf1..8530d37dd7fd 100644
--- a/posthog/clickhouse/migrations/README.md
+++ b/posthog/clickhouse/migrations/README.md
@@ -104,6 +104,6 @@ Example PR for non-sharded table: https://github.com/PostHog/posthog/pull/38890/
Our main cluster (`posthog`) nodes were overwhelmed with ingestion and sometimes the query load
was interfering with ingestion. This was causing delays and at the end incidents.
-We added new nodes that are not part of our regular cluster setup, we run them on Kubernetes.
+We added new nodes that are not part of our regular cluster setup.
ClickHouse cluster as defined in it is a logical concept and one may add nodes that are running in different places, this is how we created a new cluster that has all workers and our new ingestion nodes.