{"id":945,"date":"2020-01-24T07:25:14","date_gmt":"2020-01-24T06:25:14","guid":{"rendered":"http:\/\/www.klaushaller.net\/?page_id=945"},"modified":"2020-01-24T10:02:09","modified_gmt":"2020-01-24T09:02:09","slug":"part-2-kafka-fault-tolerance-using-replica","status":"publish","type":"page","link":"https:\/\/www.klaushaller.net\/?page_id=945","title":{"rendered":"Apache Kafka Tutorial, Part 2"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Part 2: Kafka Fault\nTolerance using Replica <\/h1>\n\n\n\n<p>If you replicate events on multiple Kafka servers, you still have all the data when one of the server crashes. The other server stores the data as well and is ready to deliver it to the consumers. This is the idea behind replica in Kafka. Note, however, that the overall platform is blocked if there is no&nbsp; Zookeeper up and running. <\/p>\n\n\n\n<p>The concept\nis relatively simple for replica: There is one leader. It is the only one\naccepting new messages and events from producers and it is the only one\ndelivering events and messages to consumers. Replica servers \u201cjust\u201d get a copy\nof the events and messages from the leader.<\/p>\n\n\n\n<p>To try out this functionality by ourselves, we extend our infrastructure by increasing the number of Kafka nodes from one to three and adding a new topic using the Kafka replica feature as shown in the figure below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"985\" src=\"http:\/\/www.klaushaller.net\/wp-content\/uploads\/2020\/01\/Kafka-Tutorial-Klaus-Haller-Part-2-1024x985.png\" alt=\"\" class=\"wp-image-964\" srcset=\"https:\/\/www.klaushaller.net\/wp-content\/uploads\/2020\/01\/Kafka-Tutorial-Klaus-Haller-Part-2-1024x985.png 1024w, https:\/\/www.klaushaller.net\/wp-content\/uploads\/2020\/01\/Kafka-Tutorial-Klaus-Haller-Part-2-300x289.png 300w, https:\/\/www.klaushaller.net\/wp-content\/uploads\/2020\/01\/Kafka-Tutorial-Klaus-Haller-Part-2-768x739.png 768w, https:\/\/www.klaushaller.net\/wp-content\/uploads\/2020\/01\/Kafka-Tutorial-Klaus-Haller-Part-2-624x600.png 624w, https:\/\/www.klaushaller.net\/wp-content\/uploads\/2020\/01\/Kafka-Tutorial-Klaus-Haller-Part-2.png 1097w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Figure 2: Kafka platform with topic replication <\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2.1 Configuring Additional\nKafka Nodes<\/h2>\n\n\n\n<p>We copy the file <code>server.properties<\/code> twice to get two new files, <code>server1.properties<\/code> and <code>server2.properties<\/code>. In these files, we set the broker id parameter to 1 and 2. Also, we have to configure new log file directories for each of them, since we run all of them on our local machine in this tutorial.<\/p>\n\n\n\n<p>So, the file <code>server.properties<\/code> should contain:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># The id of the broker. This must be set to a unique integer for each broker.\nbroker.id=0\n[\u2026]\n# listeners=PLAINTEXT:\/\/:9092\n[\u2026]\n# A comma separated list of directories under which to store log files\nlog.dirs=C:\\Users\\post\\Kafka\\kafka_2.13-2.4.0\\logs_server0<\/code><\/pre>\n\n\n\n<p>The file <code>server1.properties<\/code> should contain:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># The id of the broker. This must be set to a unique integer for each broker.\nbroker.id=1\n[\u2026]\nlisteners=PLAINTEXT:\/\/:9093\n[\u2026]\n# returned from java.net.InetAddress.getCanonicalHostName().\nadvertised.listeners=PLAINTEXT:\/\/localhost:9093\n[\u2026]\n# A comma separated list of directories under which to store log files\nlog.dirs=C:\\Users\\post\\Kafka\\kafka_2.13-2.4.0\\logs_server1<\/code><\/pre>\n\n\n\n<p>The file <code>server2.properties<\/code> should contain:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># The id of the broker. This must be set to a unique integer for each broker.\nbroker.id=2\n[\u2026]\nlisteners=PLAINTEXT:\/\/:9094\n[\u2026]\n# returned from java.net.InetAddress.getCanonicalHostName().\nadvertised.listeners=PLAINTEXT:\/\/localhost:9094\n[\u2026]\n# A comma separated list of directories under which to store log files\nlog.dirs=C:\\Users\\post\\Kafka\\kafka_2.13-2.4.0\\logs_server2\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.2 Start the Additional\nKafka Nodes<\/h2>\n\n\n\n<p>The next\nsteps are straight-forward:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Open a new command shell.<\/li><li>Start a Kafka server using configuration file server1.properties (see Part 1 of the tutorial if you cannot remember the command).<\/li><li>If everything went well, you should see the following: <code>INFO [KafkaServer id=1] started (kafka.server.KafkaServer)<\/code><\/li><li>Open another new command shell.<\/li><li>Start a Kafka server using configuration file server2.properties (see Part 1 of the tutorial if you cannot remember the command).<\/li><li>If everything went well, you should see the following: <code>INFO [KafkaServer id=2] started (kafka.server.KafkaServer)<\/code><\/li><\/ol>\n\n\n\n<p>At this moment, you should have four open command shell windows: one for the Zookeeper and three Kafka server nodes. All of them must be up and running to continue.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.3 Create New Topic with\nReplication<\/h2>\n\n\n\n<p>Now the\nsystem is set-up so that we can create a new topic myReplicaChannel, this time using the replica\nfeatures. We want to have one leader and two replica. We open another new\ncommand shell and execute the following command:<\/p>\n\n\n\n<p><code>kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic myReplicaChannel<\/code><\/p>\n\n\n\n<p>Do you\nremember in which folder you have to execute this command?<\/p>\n\n\n\n<p>Now, we\ncreate a new producer and consumer in two separate, new command shell windows\nusing these two commands:<\/p>\n\n\n\n<p><code>kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic myReplicaChannel<\/code><\/p>\n\n\n\n<p>and<\/p>\n\n\n\n<p><code>kafka-console-producer.bat --broker-list localhost:9092 --topic myReplicaChannel<\/code><\/p>\n\n\n\n<p>In the producer\ncommand shell, we type in \u201cReplication is great!\u201d and press the return key. Directly\nafterwards, we see this sentence in the consumer command shell window. So, what\ndoes this mean?<\/p>\n\n\n\n<p>The implication is simple: There is no change for users respectively producers and consumers if we use the replica feature.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.4 Checking the Kafka\nSystem State <\/h2>\n\n\n\n<p>Using the command line interface, we get an overview of the overall system state (in a later part of the tutorial, we look on more production-like monitoring solutions). More precise, we can use the Kafka topics command with the list option:<\/p>\n\n\n\n<p><code>kafka-topics.bat --list --zookeeper localhost:2181<\/code><\/p>\n\n\n\n<p>As a result, we get a list with our two topics: <code>myFirstChannel<\/code>and <code>myReplicaChannel<\/code>.<\/p>\n\n\n\n<p>Let\u2019s\nassume we do not only have one or two topics and \u201cjust\u201d three Kafka nodes, but\n50 topics and 15 nodes. We might be interested in understanding which server is\ninvolved in which topic \u2013 and how the actual configuration looks like. Did we\nspecify 3 replica, or did we not consider this because we were experimenting with\nthis topic for a while and forgot about this issue? <\/p>\n\n\n\n<p>Here, we\ncan use the topics command, this time with the \u201cdescribe\u201d option:<\/p>\n\n\n\n<p><code>kafka-topics.bat --describe --zookeeper localhost:2181 --topic myReplicaChannel<\/code><\/p>\n\n\n\n<p>Before continue reading, you might want to compare the result for the <code>myReplicaChannel <\/code>with the one for the <code>myFirstChannel<\/code>. When we submit the command above, we get this result:<\/p>\n\n\n\n<p><code>Topic: myReplicaChannel&nbsp;&nbsp;&nbsp; PartitionCount: 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReplicationFactor: 3&nbsp;&nbsp;&nbsp; Configs:<\/code><\/p>\n\n\n\n<p><code>Topic: myReplicaChannel&nbsp;&nbsp;&nbsp; Partition: 0&nbsp;&nbsp;&nbsp; Leader: 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Replicas: 0,2,1 Isr: 0,2,1<\/code><\/p>\n\n\n\n<p>So, what does this mean? <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>All information regarding partitions is out of our focus now. We discuss in the <a href=\"http:\/\/www.klaushaller.net\/?page_id=947\">next part of this tutorial<\/a>.<\/li><li>The replication factor is three with the Kafka nodes with the broker ids 0, 2, 1.<\/li><li>The leader, i.e., the node all the producers and consumers communicate with, is the node with broker id 0.<\/li><li>All replica are up to date, because they are all listed after \u201cIsr\u201d.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2.5 A Simple Failover Test<\/h2>\n\n\n\n<p>Now we want to check what is happening if we lose the leader node. Therefore, we terminate the process by closing the command shell. With our data above, the node with broker id 0 has to be terminated, i.e., the server started with the <code>server.properties<\/code> configuration. If we run the description command again, we will see that now a different broker became the leader. We will have a closer look on this topic later. First, we look on how Kafka enables parallel processing and, thus, a high throughput.<\/p>\n\n\n\n<p>Before continuing to the next section of the tutorial, you can <\/p>\n\n\n\n<p>Click <a href=\"http:\/\/www.klaushaller.net\/?page_id=947\">here <\/a>to go to the next part of this tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Part 2: Kafka Fault Tolerance using Replica If you replicate events on multiple Kafka servers, you still have all the data when one of the server crashes. The other server stores the data as well and is ready to deliver it to the consumers. This is the idea behind replica in Kafka. Note, however, that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":940,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-945","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.klaushaller.net\/index.php?rest_route=\/wp\/v2\/pages\/945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.klaushaller.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.klaushaller.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.klaushaller.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.klaushaller.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=945"}],"version-history":[{"count":5,"href":"https:\/\/www.klaushaller.net\/index.php?rest_route=\/wp\/v2\/pages\/945\/revisions"}],"predecessor-version":[{"id":967,"href":"https:\/\/www.klaushaller.net\/index.php?rest_route=\/wp\/v2\/pages\/945\/revisions\/967"}],"up":[{"embeddable":true,"href":"https:\/\/www.klaushaller.net\/index.php?rest_route=\/wp\/v2\/pages\/940"}],"wp:attachment":[{"href":"https:\/\/www.klaushaller.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}