May 13, 2024

Read what is a distributed stream processing system Kafka

What is Kafka

Kafka is a distributed stream processing system. The stream processing system enables it to publish or subscribe messages like a message queue. Distributed provides fault tolerance and concurrent message processing mechanisms.

The basic concept of Kafka

Kafka runs on a cluster that contains one or more servers. Kafka puts the message in the topic, and each message contains a key, a value, and a timestamp.

Kafka has some basic concepts:

Producer - The message producer is the client that sends messages to the kafka broker.

Consumer - The message consumer, the consumer of the message, responsible for consuming messages on the Kafka server.

Topic - The theme, defined by the user and configured on the Kafka server, is used to establish a subscription relationship between the Producer and the Consumer. The producer sends a message to the specified Topic, and the message consumer consumes the message from this Topic.

Partition - A message partition. A topic can be divided into multiple partitions. Each partition is an ordered queue. Each message in the partition is assigned an ordered id (offset).

Broker - A kafka server is a broker. A cluster consists of multiple brokers. A broker can hold multiple topics.

Consumer Group - A group of consumers used to group similar consumers. Each consumer belongs to a specific consumer group. Multiple consumers can jointly message a message under Topic. Each consumer consumes some of the messages. These consumers form a group with the same group name, usually Known as the consumer cluster.

Offset - The offset of the message in the partition. Each message has a unique offset in the partition, and the messager can specify an offset to specify the message to consume.

Kafka distributed architecture

As shown above, kafka will have messages in the topic in different partitions. If there is a key, the message is classified into different parts according to the key. If there is no key, the message exists in different partitions according to the Round Robin mechanism. By default, the key determines which partition a message will be in.

The sequence of messages in partition is an ordered sequence of messages. Kafka uses the offset in the partition to specify the location of the message. A partition of a topic can only be consumed by a consumer in a consumer group. It is not allowed for multiple consumers to consume data in the same partition, but a consumer can consume data in multiple partitions.

Kafka copies the partition data to a different broker, providing a backup of the partition data. Each partition has a broker as the leader and several brokers as the follower. All data is read and written by the server where the leader is located, and the leader copies data between different brokers.

In the figure above, for Partition 0, broker 1 is its leader, and broker 2 and broker 3 are follower. For Partition 1, Broker 2 is its leader, and Broker 1 and Broker 3 are followers.

In the figure above, when there is a Client (that is, Producer) to write data to Partition 0, it will be written to leader Broker 1, and Broker 1 will copy the data to Follower Broker 2 and Broker 3.

In the above figure, when the client writes data to Partition 1, it is written to Broker 2, because Broker 2 is the leader of Partition 1, and then Broker 2 copies the data to follower Broker 1 and Broker 3.

The topic in the above picture has a total of 3 partitions. The reading and writing of each partition is handled by different brokers, so the total throughput is improved.

Lab 1: Kafka-python implements producer consumers

Kafka-python is a python Kafka client that can be used to send messages and consume messages to kafka's topic.

This experiment will implement a producer and a consumer, the producer will send a message to kafka, and the consumer consumes the message from the topic. The structure is as follows

Producer code

Consumer code

Next create test topic

Open two windows, we run the producer in window1, as follows

Run consumer in window2, as follows

You can see that the consumer in window2 successfully read the data written by the producer.

Experiment 2: The consumer group implements a fault tolerance mechanism

This experiment will demonstrate the fault-tolerant characteristics of the consumer group. In this experiment, a topic with 2 partitions will be created, and 2 consumers will consume the data in the same topic. The structure is as follows

The producer part code is the same as experiment one and will not be repeated here. Consumer needs to specify the corresponding consumer group, the code is as follows

Next we create the topic, the name test, set the number of partitions to 2

Open three windows, one to run the producer, and two to run the consumer.

The output of the two windows running the consumer is as follows:

You can see that when two consumers are running at the same time, they consume data in different partitions. The consumer in window1 consumes the data in partition 0, and the consumer in window2 consumes the data in parition 1.

We try to close the consumer in window1, you can see the following result

At the beginning of the window2 consumer only consumes the data in partition1, when the consumer in window1 exits, the consumer in window2 also begins to consume the data in partition 0.

Experiment 3: Offset management

Kafka allows the consumer to submit the offset of the currently consumed message to kafka, so that if the consumer exits due to an exception, the next time you start, you can continue to consume the message from the last recorded offset.

The structure of this experiment is the same as that of Experiment 1. Using a producer, a consumer, the number of partitions for the test topic is set to 1.

The producer code is the same as in Experiment 1, and will not be repeated here. The consumer code is slightly modified, where the consumer prints the offset of the next message to be consumed. The consumer code is as follows

Start the producer in one window, in another window and start the consumer. The output of consumer is as follows

You can try to exit the consumer and start the consumer. Every time you restart, the consumer is consumed from the message offset=98.

Modify the consumer code as follows, after the consumer consumes each message, submit the offset back to kafka

Start consumer

You can see that the consumer starts to consume from the offset=98 message. When offset=829, we Ctrl+C to exit the consumer.

We start the consumer again

It can be seen that after restarting, the consumer continues to consume the message from the offset of the last record. After each consumer restart, the consumer will continue to consume from the last stop.

This article mainly introduces the basic concepts of kafka, and combines some experiments to help understand some difficulties in kafka, such as the fault-tolerance mechanism of multiple consumers, offset management.

Screen Protector For Samsung

A TPU Screen Protector made of the super toughness of the honeycomb structure. Its unique ultra-soft properties allow it to cover the most complex curves and contours in a device.

The self-healing design of the Hydrogel Screen Protector can protect the display screen of the device from damage, leave no air bubbles, and maintain the sensitivity of the touch screen. Advanced anti-fingerprint and dust- and oleophobic overlays keep your screen smudge- and dirt-free. This overlay is also important in providing maximum touch sensitivity for improved high-speed glide and optimal touch response.

The optical transparency of the Hydrogel Film is more than 90%, showing you the most original screen color and bringing the most realistic visual experience.

If you want to know more about the product information of the Hydrogel Screen Protector for Samsung, please click the product details to view the parameters, model, picture, price and other information of the Samsung Screen Protector.
Whether you are a group or an individual, we will do our best to provide you with accurate and comprehensive information about Hydrogel Screen Protectors!

Screen Protector For Samsung,Hydrogel Screen Protector for Samsung,TPU Screen Protector for Samsung,Hydrogel Film for Samsung

Shenzhen Jianjiantong Technology Co., Ltd. , https://www.jonsun-sz.com