Imagine you’ve got a Kafka topic and start a new consumer group that consumes from the beginning. Now, imagine this takes time. Lots of time. How do you know how many more messages are left? Can you guess an ETA of how much longer it’ll take to the most recent state?
This was a task I had in hands. I did not find a tool that right out of the box did what I want, but luckily Kafka’s CLI tools enables me to come up with single command line to solve the issue.
If you’re in a hurry, here is the command:
Setting up the environment
If you don’t plan to see this in action in your environment, or you already have one, then skip this whole section. Really.
Start by downloading the tool, enter the folder and start a healthy Kafka cluster with one topic and one consumer group:
What you’ve got now is a topic named quickstart-events and a consumer group with 5 partitions named quickstart-group:
Now, we’ve got to send some messages:
If this command fails on you, don’t worry. It is due to repeat. I use ZSH and it works for me, but there are otherreplacements.
If you ever need more messages, just stop the consumer and add more (or do the whole dance but for another topic).
Let’s check again our consumer:
It’s ok for the offset to be different. What’s important here is that the consumer is lagging 5.000.000 messages in total. This number of messages enables us to actually see the consumer’s group progress. There are other ways to do it, but we’ll stick with this one.
Don’t forget, at the end, to clean everything, stop all active executions and clean the leftovers:
The command in action
One marvel of the bin/kafka-consumer-groups.sh --describe command is that it outputs a formatted table space separated (head for the last code snippet in the previous section to see one example).
One way to track progress is to execute the command every few seconds (e.g., watch -n 1), but that does not seem much of an option unless you want to count every lag on each update. Otherwise, you’ll just stare at changing numbers.
It’s time to make use of the nicely formatted table with AWK. We just got to pipe the table and sum the fourth and fifth columns (consumed and total messages, respectively) to get a percentage:
And we got the command which was presented at the beginning:
If you did the previous section, then you can see it in action:
The output will be a single percentage that may be nifty to use with any other commands.
I hope you enjoyed this little hack. More will come.