> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to route malformed Kafka messages to a dead-letter topic with the Kafka table engine

> Use kafka_handle_error_mode = stream and two materialized views to insert well-formed Kafka messages into a table and send messages that fail to parse to a dead-letter topic.

Create the [`Kafka`](https://clickhouse.com/docs/reference/engines/table-engines/integrations/kafka) table with `kafka_handle_error_mode = 'stream'`. A message that cannot be parsed then no longer stops consumption: it arrives as a row whose virtual columns `_raw_message` and `_error` hold the original message and the parsing error. One materialized view inserts the rows without an error into your table, and a second one writes the failed messages to another Kafka topic through a second `Kafka` table.

## Setup

The examples use a broker at `kafka:9092`, a source topic `events` with JSON messages, and a dead-letter topic `events_dead_letter`.

1. Create the table that consumes the source topic, with the error mode set to `stream`:

```sql theme={null}
CREATE TABLE events_queue
(
    event_time DateTime,
    user_id UInt64,
    action String
)
ENGINE = Kafka
SETTINGS
    kafka_broker_list = 'kafka:9092',
    kafka_topic_list = 'events',
    kafka_group_name = 'clickhouse_events',
    kafka_format = 'JSONEachRow',
    kafka_handle_error_mode = 'stream';
```

2. Stop the table before you attach a view to it:

```sql theme={null}
SYSTEM STOP events_queue;
```

A `Kafka` table starts consuming as soon as its first materialized view exists. Without this step, the view of step 3 alone would consume the messages that wait in the topic or arrive during the setup: its filter drops a malformed message, the offset is committed, and the dead-letter view of step 5 never receives that message.

3. Create the destination table and a materialized view that keeps only the rows that parsed:

```sql theme={null}
CREATE TABLE events
(
    event_time DateTime,
    user_id UInt64,
    action String
)
ENGINE = MergeTree
ORDER BY (user_id, event_time);

CREATE MATERIALIZED VIEW events_mv TO events AS
SELECT
    event_time,
    user_id,
    action
FROM events_queue
WHERE length(_error) = 0;
```

4. Create a second `Kafka` table on the dead-letter topic. It is used only for writing: every row inserted into it is produced as one message in the format of the table.

```sql theme={null}
CREATE TABLE events_dead_letter_out
(
    topic String,
    partition UInt64,
    offset UInt64,
    raw_message String,
    error String
)
ENGINE = Kafka
SETTINGS
    kafka_broker_list = 'kafka:9092',
    kafka_topic_list = 'events_dead_letter',
    kafka_group_name = 'clickhouse_events_dead_letter',
    kafka_format = 'JSONEachRow';
```

5. Create the materialized view that sends the failed messages to it, together with their position in the source topic:

```sql theme={null}
CREATE MATERIALIZED VIEW events_dead_letter_mv TO events_dead_letter_out AS
SELECT
    _topic AS topic,
    _partition AS partition,
    _offset AS offset,
    _raw_message AS raw_message,
    _error AS error
FROM events_queue
WHERE length(_error) > 0;
```

6. Start consumption, now that both views exist:

```sql theme={null}
SYSTEM START events_queue;
```

> [!NOTE]
>
> `SYSTEM STOP` and `SYSTEM START` work for `Kafka` tables from ClickHouse 26.8. See [Managing background activity](https://clickhouse.com/docs/reference/statements/system.md#managing-background-activity). The stopped state is not kept across a server restart. On an earlier version, leave out steps 2 and 6. Consumption then starts with the view of step 3, and a malformed message that is consumed before the view of step 5 exists does not reach the dead-letter topic. By default, a consumer group without committed offsets reads the topic from the beginning, so this includes malformed messages that were produced long before the setup.

## Result

Take these two messages in the `events` topic:

```json theme={null}
{"event_time": "2026-01-15 10:00:00", "user_id": 1, "action": "login"}
{"event_time": "not a date", "user_id": 2, "action": "login"}
```

The first one is inserted into `events`. The `event_time` value of the second one cannot be parsed as `DateTime`, so the message is not inserted. Instead, the `events_dead_letter` topic receives one JSON message with the keys `topic`, `partition`, `offset`, `raw_message` (the complete original message) and `error` (the text of the parsing exception). A consumer of that topic can count the failures, or repair the messages and produce them again.

## How it works

* With the default `kafka_handle_error_mode = 'default'`, a message that cannot be parsed raises an exception and the table does not get past it, unless `kafka_skip_broken_messages` allows it to be skipped.
* With `stream`, a failed message still produces a row. All its data columns have default values, `_raw_message` contains the message, and `_error` contains the exception message. For a message that parsed, both virtual columns are empty. This is why `events_mv` needs the `WHERE length(_error) = 0` filter: without it, a row of default values is inserted for every failed message.
* `_raw_message` and `_error` exist only when the table is created with `kafka_handle_error_mode = 'stream'`. Otherwise the views fail to be created with `Unknown expression identifier`.
* Only parsing errors are reported this way. A JSON message with a missing or an unknown key is not an error: the missing columns get default values.
* The `Kafka` table engine has at-least-once delivery, so a failed message can occasionally be written to the dead-letter topic more than once. The `topic`, `partition` and `offset` fields identify the original message.
* A view receives only the messages that are consumed after it was created. This is why the setup stops the `Kafka` table until both views exist.

## Alternatives

* To keep the failed messages in ClickHouse instead of Kafka, point the second materialized view at a `MergeTree` table with the same five columns.
* `kafka_handle_error_mode = 'dead_letter_queue'` needs no materialized view for the errors: ClickHouse writes the failed messages to the [`system.dead_letter_queue`](https://clickhouse.com/docs/reference/system-tables/dead_letter_queue) table. The table must be enabled with the `dead_letter_queue` section of the server configuration, otherwise `CREATE TABLE` fails. In this mode a failed message produces no row, so the main view needs no filter.

## Related pages

* [`Kafka` table engine](https://clickhouse.com/docs/reference/engines/table-engines/integrations/kafka), including the list of [virtual columns](https://clickhouse.com/docs/reference/engines/table-engines/integrations/kafka.md#virtual-columns)
* [Using the Kafka table engine](https://clickhouse.com/docs/integrations/connectors/data-ingestion/kafka/kafka-table-engine.md)
* [`system.dead_letter_queue`](https://clickhouse.com/docs/reference/system-tables/dead_letter_queue)
