# How to work with kcat and KafScale

> Produce, consume and list metadata on KafScale v2.2.0 with kcat (formerly kafkacat). Copy-paste commands for keys, headers, consumer groups, offset lookups by time and mTLS with a config file.

Canonical page: https://kafscale.io/kafka-tools/kcat/
# How to work with kcat and KafScale

## kcat

[kcat](https://github.com/edenhill/kcat) (formerly kafkacat) is a small command line Kafka producer, consumer and metadata browser built on librdkafka. It starts fast and needs no JVM. It reads from stdin and writes to stdout, so it fits shell scripts, CI checks and quick debugging. See the [kcat install notes](https://github.com/edenhill/kcat#install), or run the `edenhill/kcat` container image.

## Before you start

- A KafScale v2.2.0 cluster with the proxy enabled. Clients connect to the proxy at `proxy.advertisedHost` and `proxy.advertisedPort` from your Helm values.
- kcat 1.7 or later. This guide was tested with kcat 1.7.1 (librdkafka 2.8.0) against KafScale v2.2.0 with 2 brokers and 2 proxy replicas.

```bash
export BOOTSTRAP=kafka.example.com:9092
```

kcat does not create topics. Create one first, for example with the [Kafka CLI](/kafka-tools/kafka-cli/#1-create-a-topic).

## Try it yourself

### 1. Read the metadata

```bash
kcat -b "$BOOTSTRAP" -L -t orders
```

```text
Metadata for orders (from broker 0: kafka.example.com:9092/0):
 1 brokers:
  broker 0 at kafka.example.com:9092 (controller)
 1 topics:
  topic "orders" with 4 partitions:
    partition 0, leader 0, replicas: 0, isrs: 0
    partition 1, leader 0, replicas: 0, isrs: 0
    partition 2, leader 0, replicas: 0, isrs: 0
    partition 3, leader 0, replicas: 0, isrs: 0
```

The cluster shows as one broker, node 0, at the proxy address, with one replica per partition. That is expected. See [how tools see a KafScale cluster](/kafka-tools/#how-tools-see-a-kafscale-cluster). Add `-J` for JSON output.

### 2. Produce

Keyed records, one `key:value` per line, to partition 1:

```bash
printf 'k1:hello\nk2:world\n' | kcat -P -b "$BOOTSTRAP" -t orders -K: -p 1
```

One record with a key and a header:

```bash
echo 'v-with-header' | kcat -P -b "$BOOTSTRAP" -t orders -p 1 -k k3 -H source=kcat
```

Leave out `-p` to let the partitioner pick by key. Idempotent producing works. Add `-X enable.idempotence=true`.

### 3. Consume

Read everything and exit at the end, with partition, offset, key, value and headers:

```bash
kcat -C -b "$BOOTSTRAP" -t orders -o beginning -e -q -f 'p%p o%o k=%k v=%s h=%h\n'
```

```text
p0 o0 k=a v=1 h=
p0 o1 k=b v=2 h=
p1 o0 k=k1 v=hello h=
p1 o1 k=k2 v=world h=
p2 o0 k=c v=3 h=
p1 o2 k=k3 v=v-with-header h=source=kcat
```

Records `a`, `b` and `c` come from the [Kafka CLI guide](/kafka-tools/kafka-cli/#2-produce-and-consume). Records are in order within a partition, not across partitions. `-q` hides the "Reached end of topic" lines.

The last two records of partition 1:

```bash
kcat -C -b "$BOOTSTRAP" -t orders -p 1 -o -2 -e -q
```

```text
world
v-with-header
```

Consume as a consumer group, with offsets committed to KafScale:

```bash
kcat -b "$BOOTSTRAP" -G orders-kcat orders -o beginning -e -q -f '%k=%s\n'
```

```text
k1=hello
k2=world
c=3
a=1
b=2
k3=v-with-header
```

Check the group afterwards with [`kafka-consumer-groups.sh --describe`](/kafka-tools/kafka-cli/#3-check-the-consumer-group).

### 4. Look up an offset by time

Print the first offset at or after a time. The time is in milliseconds since the epoch:

```bash
kcat -Q -b "$BOOTSTRAP" -t orders:1:"$(( $(date +%s) - 3600 ))000"
```

```text
orders [1] offset 0
```

To start reading at a time, use `-o s@<timestamp>` with the same value. If no record is at or after that time, kcat prints `offset -1`.

## Connect with mTLS

When the proxy has [mTLS enabled](/kafka-authentication/), give kcat the CA and a client certificate in a config file. SASL is not supported. Keep the file and the key readable only by you, and do not pass key passwords with `-X` on the command line:

```bash
umask 077
cat > kcat.conf <<'CONF'
bootstrap.servers=kafka.example.com:9092
security.protocol=ssl
ssl.ca.location=/path/to/ca.crt
ssl.certificate.location=/path/to/client.crt
ssl.key.location=/path/to/client.key
CONF
chmod 600 kcat.conf
```

If the key is encrypted, add `ssl.key.password` to the file with an editor. Then use `-F` instead of `-b`:

```bash
kcat -F kcat.conf -L -t secure-events
echo 'from-kcat' | kcat -F kcat.conf -P -t secure-events -k k9
kcat -F kcat.conf -C -t secure-events -o beginning -e -q -f '%p %o %k %s\n'
```

Without a client certificate signed by a CA the proxy trusts, the connection fails with `Failed to acquire metadata: Local: Broker transport failure`.
