<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MSK Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/tag/msk/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/tag/msk/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Wed, 15 Mar 2023 12:52:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Performing broker migrations on your Kafka consumers</title>
		<link>https://blogit.create.pt/joaorafael/2023/03/15/kafka-consumer-broker-migration-aws-msk/</link>
					<comments>https://blogit.create.pt/joaorafael/2023/03/15/kafka-consumer-broker-migration-aws-msk/#respond</comments>
		
		<dc:creator><![CDATA[João Rafael]]></dc:creator>
		<pubDate>Wed, 15 Mar 2023 12:44:45 +0000</pubDate>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Kafka]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[MSK]]></category>
		<category><![CDATA[Streaming]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=12805</guid>

					<description><![CDATA[<p>When using Kafka for communication between systems, you may find yourself having to change your consumer configuration. This may happen due to the publisher changing topic, or a new message broker being created and a company-wide mandate that everyone switch until a set date. Doing this sort of migration isn&#8217;t very complicated, but any errors [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/joaorafael/2023/03/15/kafka-consumer-broker-migration-aws-msk/">Performing broker migrations on your Kafka consumers</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>When using Kafka for communication between systems, you may find yourself having to change your consumer configuration. This may happen due to the publisher changing topic, or a new message broker being created and a company-wide mandate that everyone switch until a set date. Doing this sort of migration isn&#8217;t very complicated, but any errors may prove to be costly. If we re-consume a new topic from the beginning, for example, we may accidentally fill up our database, for example. We want to start consuming the new topic at around the same time we stopped consuming the old one (with a buffer of a few minutes preferably, just to be safe).</p>



<p>The first step here is to ensure that your consumer code is idempotent, but that’s something that you should always ensure &#8211; just imagine that you have to re-consume a couple of messages, in that case lacking idempotency may result in your system getting strange data and bugs.</p>



<h2 class="wp-block-heading">Setting up Kafka on your machine</h2>



<p><em>(Feel free to skip this section if you have everything set up.)</em></p>



<p>Kafka releases aren’t included in most package managers, except for Homebrew in Mac systems. They should be available, however, in the <a href="https://kafka.apache.org/downloads" target="_blank" rel="noreferrer noopener">official download page</a>. For Unix systems (Mac, Linux, WSL, *BSD), simply get one of the binary downloads and extract it wherever you prefer. Then, make sure to add the bin folder to your $PATH, so that you can use these scripts in any folder. In my case, I put the extracted archive in $HOME/.local/share/kafka, so I edit my .zshrc (or .bashrc in a plainer Linux/WSL system) and add:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
export PATH=$HOME/.local/share/kafka/bin:$PATH
</pre></div>


<p><br>Now we can use the official kafka scripts in any directory. That is, unless we have some form of authentication. We can add additional configs to a file somewhere, and include that file in our commands. Let’s put a file called <code>dev.cfg</code> in <code>$HOME/.config/kafka/</code> (the exact directory doesn’t matter, but .config is the appropriate directory for configs), and write the following on it:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
security.protocol=SASL_SSL
sasl.mechanism=SCRAM-SHA-512
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \
    username=&quot;me&quot; \
    password=&quot;noneofyourbusiness&quot;;
</pre></div>


<p>This is just an example, your broker&#8217;s security config may vary. After doing this, just add to any of the Kafka commands you use:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
--command-config $HOME/.config/kafka/dev.cfg
</pre></div>


<h2 class="wp-block-heading">Checking consumer offsets</h2>



<p>Before migrating topics, it&#8217;s best if we make sure that the old topic consumer is totally up-to-date. We can use this command to check the consumption lag:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
kafka-consumer-groups.sh \
	--bootstrap-server=&#039;&lt;your broker endpoints, separated by commas&gt;&#039; \
	--describe \
	--group=&#039;&lt;your consumer group&gt;&#039;

</pre></div>


<p>This should yield something like the output below:</p>



<pre class="wp-block-code"><code>TOPIC      PARTITION   NEWEST_OFFSET   OLDEST_OFFSET     CONSUMER_OFFSET     LEAD         LAG
your-topic 0           30000000        0                 30000000            30000000     0
your-topic 1           23900000        0                 23900000            23900000     0

CLIENT_HOST      CLIENT_ID         TOPIC          ASSIGNED_PARTITIONS
/128.0.0.0       your-consumer     your-topic     0
/128.0.0.1     	 your-consumer     your-topic     1</code></pre>



<p>From this we can see that there is zero lag, since the newest offset is equal to the consumer offset on both partitions. We can also see that our consumers are still running (they&#8217;re the clients with ID &#8220;your-consumer&#8221;).</p>



<h2 class="wp-block-heading">Performing the Kafka consumer migration</h2>



<p>Before switching topics or brokers, we should first create a consumer group for the new configuration. This will allow us to change the offsets <em>before</em> beginning the message consumption proper. The easiest way to do this is to use <a href="https://github.com/edenhill/kcat"><code>kcat</code></a>, a simple command-line tool that allows us to consume and produce Kafka messages. To create a consumer group, you must consume messages, so it&#8217;s a matter of setting up <code>kcat</code> (check the install guide on the kcat repo) and consuming a few messages from the new topic:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
kcat \
	-C \
	-b &#039;&lt;your brokers&gt;&#039; \
	-t new-topic \
	-G new-consumer-group

# If you have authentication, just add: 

	-X security.protocol=SASL_SSL \
	-X sasl.mechanisms=SCRAM-SHA-512 \
	-X sasl.username=&quot;me&quot; \
	-X sasl.password=&quot;noneofyourbusiness&quot;

</pre></div>


<p>Just run this for a second, check that messages come out, and then run the describe command for the new group:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
kafka-consumer-groups.sh \
	--bootstrap-server=&#039;new-broker&#039; \
	--describe \
	--group=&#039;new-group&#039;


TOPIC     PARTITION   NEWEST_OFFSET   OLDEST_OFFSET     CONSUMER_OFFSET     LEAD         LAG
new-topic 0           12387000        0                 12                  12           12386988
new-topic 1           12360000        0                 38                  38           12359962

CLIENT_HOST      CLIENT_ID         TOPIC          ASSIGNED_PARTITIONS
</pre></div>


<p>We have huge lag here, but that&#8217;s fine, as we&#8217;re going to reset the offsets anyway. Stop the workers consuming from the older topic by whatever means you can and note down the time at which they stopped. We will now reset the consumer group offset for this new group. Just a note: in this case, we don&#8217;t have any consumers connected to this group, but if we did we&#8217;d also have to turn them off. You <strong>cannot</strong> reset a consumer group offset if there are clients connected to it.</p>



<p>To reset the offsets, just run the following command (here we&#8217;ll assume that we stopped the previous consumers at 10:00 of March 15th, 2023, and subtract ten minutes just to be safe):</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
kafka-consumer-groups.sh \
	--bootstrap-server=&#039;new-brokers&#039; \
	--group=&#039;new-group&#039; \
	--topic=&#039;new-topic&#039; \
	--reset-offsets \
	--to-datetime 2023-03-15T09:50:00.000 \
	--dry-run
</pre></div>


<p>Running this dry run will not make any changes, It will only show you the new offsets for the consumer group, e.g.:</p>



<pre class="wp-block-code"><code>TOPIC     PARTITION   NEWEST_OFFSET   OLDEST_OFFSET     CONSUMER_OFFSET     LEAD         LAG
new-topic 0           12387000        0                 12386998            12386988     2
new-topic 1           12360000        0                 12369951            12369951     49

CLIENT_HOST      CLIENT_ID         TOPIC          ASSIGNED_PARTITIONS</code></pre>



<p>If nothing looks strange to you, go ahead, substitute <code>--dry-run</code> with <code>--execute</code> and run the command. Afterwards, deploy the new consumer configuration, turn on the worker, and let it run.</p>
<p>The post <a href="https://blogit.create.pt/joaorafael/2023/03/15/kafka-consumer-broker-migration-aws-msk/">Performing broker migrations on your Kafka consumers</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/joaorafael/2023/03/15/kafka-consumer-broker-migration-aws-msk/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
