|
| 1 | +--- |
| 2 | +id: c0e1671b46 |
| 3 | +question: How to Inspect Messages in a Kafka Topic Using Offsets? |
| 4 | +sort_order: 33 |
| 5 | +--- |
| 6 | + |
| 7 | +An offset in Kafka is a per-partition sequence number that uniquely identifies messages within that partition. There is no global offset for the entire topic, and consumers use offsets to track what they have processed. |
| 8 | + |
| 9 | +Why inspecting offsets helps: when errors occur in a real-time stream, inspecting messages near a known offset helps you see what data caused the error, understand surrounding context, and reproduce the issue locally. |
| 10 | + |
| 11 | +Viewing offsets and consumer lag |
| 12 | + |
| 13 | +Use the Kafka CLI to see how far a consumer group has progressed: |
| 14 | + |
| 15 | +```bash |
| 16 | +kafka-consumer-groups \ |
| 17 | + --bootstrap-server localhost:9092 \ |
| 18 | + --describe \ |
| 19 | + --group rides-to-postgres |
| 20 | +``` |
| 21 | + |
| 22 | +You can also run this via Docker: |
| 23 | + |
| 24 | +```bash |
| 25 | +docker run --rm -it --network pyflink_default confluentinc/cp-kafka:7.6.0 kafka-consumer-groups \ |
| 26 | +--bootstrap-server redpanda:29092 \ |
| 27 | +--describe \ |
| 28 | +--group rides-to-postgres |
| 29 | +``` |
| 30 | + |
| 31 | +Key fields to look at: |
| 32 | + |
| 33 | +- CURRENT-OFFSET: last offset processed by the consumer |
| 34 | +- LOG-END-OFFSET: last offset available in the topic |
| 35 | +- LAG: messages pending to be processed |
| 36 | + |
| 37 | +For more details, see the official docs: kafka-consumer-groups-sh |
| 38 | + |
| 39 | +Consuming messages from the beginning of a topic |
| 40 | + |
| 41 | +To inspect all messages in a topic, you can use kafka-console-consumer: |
| 42 | + |
| 43 | +```bash |
| 44 | +kafka-console-consumer \ |
| 45 | + --bootstrap-server localhost:9092 \ |
| 46 | + --topic rides \ |
| 47 | + --from-beginning |
| 48 | +``` |
| 49 | + |
| 50 | +Or via Docker: |
| 51 | + |
| 52 | +```bash |
| 53 | +docker run --rm -it --network pyflink_default confluentinc/cp-kafka:7.6.0 kafka-console-consumer \ |
| 54 | +--bootstrap-server redpanda:29092 \ |
| 55 | +--topic rides \ |
| 56 | +--from-beginning |
| 57 | +``` |
| 58 | + |
| 59 | +Notes: |
| 60 | +- This is useful for basic exploration but does not allow jumping to a specific offset. |
| 61 | + |
| 62 | +Inspecting messages from a specific offset with kcat (formerly kafkacat) |
| 63 | + |
| 64 | +A very handy tool is kcat for reading messages starting from a given offset: |
| 65 | + |
| 66 | +```bash |
| 67 | +kcat -C \ |
| 68 | +-b localhost:9092 \ |
| 69 | +-t rides \ |
| 70 | +-p 0 \ |
| 71 | +-o 25 \ |
| 72 | +-c 5 |
| 73 | +``` |
| 74 | + |
| 75 | +Docker usage: |
| 76 | + |
| 77 | +```bash |
| 78 | +docker run --network pyflink_default edenhill/kcat:1.7.1 -C \ |
| 79 | +-b redpanda:29092 \ |
| 80 | +-t rides \ |
| 81 | +-p 0 \ |
| 82 | +-o 25 \ |
| 83 | +-c 5 |
| 84 | +``` |
| 85 | + |
| 86 | +Options explained: |
| 87 | + |
| 88 | +- consumer mode `-C` |
| 89 | +- broker `-b` (host:port) |
| 90 | +- topic `-t` the topic to read from |
| 91 | +- partition `-p` the partition |
| 92 | +- start at offset `-o` to begin reading |
| 93 | +- read up to `-c` messages |
| 94 | + |
| 95 | +This lets you see exactly what happens starting from a particular offset. |
| 96 | + |
| 97 | +Notes: |
| 98 | +- kcat is the successor to kafkacat; you can install or run it from Docker. |
| 99 | +- Replace broker address and topic/partition as per your environment. |
| 100 | + |
| 101 | +Conclusion |
| 102 | + |
| 103 | +Knowing how to inspect messages with specific offsets is a fundamental skill for Kafka debugging. Use these commands to locate the data around a known offset, monitor consumer lag, and reproduce issues locally when needed. |
0 commit comments