This section describes the design philosophy of Kafka from an overall perspective. Each idea can be studied in depth. In the future, I may issue a special article to introduce it in depth, here we will only make a general description so that you can better understand the uniqueness of Kafka. This section mainly involves the following content:
- Basic concepts of Kafka Design
- Data Compression in Kafka
- Reliability during Kafka Message Transfer
- Copying a Kafka cluster Image
- Kafka backup mechanism
I. Origins of KafkaDue to overspending on daily JMS management and limitations on traditional JMS scalability, LinkedIn (www.linkedin.com) kafka has been developed to meet their high requirements for Real-time Data Stream monitoring and metrics such as CPU and IO utilization. At the beginning of LinkedIn's development of Kafka, we focused on the following aspects:
- Provides a common API for producers and consumers.
- Message persistence
- High throughput for processing millions of messages
- Distributed and highly scalable support
Ii. Basic IdeasThe most basic architecture is that a producer publishes a message to a topic in Kafka. This topic is provided by the broker playing the kafkaserver role. Consumers subscribe to this topic and then obtain the message from it, the following figure shows a more intuitive description of the scenario: the architecture is divided into three parts: producers, Kafka broker, and consumers, which run on different nodes respectively. The following describes some Kafka design ideas:
Consumer group:Each consumer can form a group. Each message can only be consumed by one consumer in the group. If one message can be consumed by multiple consumers, these consumers must be in different groups.
Message status:In Kafka, the message status is saved in consumer, so the broker does not care about which message is consumed and who is consumed, only one offset value is recorded (pointing to the next message location in partition to be consumed), which means that if the consumer cannot process well, a message on the broker may be consumed multiple times.
Message Persistence:In Kafka, messages are persisted to the local file system and maintained extremely high efficiency.
Message validity period:Kafka retains the messages for a long time so that the consumer can consume the messages multiple times. Of course, many of the details are configurable.
Batch Sending:Kafka supports Batch Sending by message set to improve the push efficiency.
Push-and-pull:In Kafka, the producer and consumer adopt the push-and-pull mode, that is, the producer only pushes messages to the broker, and the consumer only sends messages from the pull of the broker, the two are asynchronous for message production and consumption.
Relationship Between Brokers in a Kafka cluster:It is not a master-slave relationship, and each broker has the same status in the cluster. We can add or delete any broker node at will.
Server Load balancer:Kafka provides a Metadata API to manage the load between brokers (for kafka0.8.x, 0.7.x primarily relies on zookeeper to achieve load balancing ).
Synchronous and asynchronous:Producer adopts asynchronous push mode, which greatly improves the throughput of the Kafka System (whether synchronous or asynchronous mode can be used through parameter control ).
Partition mechanism partition:The broker of Kafka supports message partitions. The producer can decide which partition to send messages to. In a partition, the order of messages is the order in which the producer sends messages. A topic can have multiple partitions, the number of partitions is configurable. Partition is of great significance, and the subsequent content will be gradually reflected.
Offline data loading:Due to its support for Scalable Data Persistence, Kafka is also suitable for loading data to hadoop or data warehouse.
Plugin support:Many active communities have developed many plug-ins to expand Kafka functions, such as using plug-ins related to storm, hadoop, and flume.
Iii. Message CompressionWe have learned that Kafka supports sending messages in a set. On this basis, Kafka also supports compressing the message set. The producer can compress the message set in gzip or snappy format. After the producer end is compressed, the consumer end needs to be decompressed. The advantage of compression is to reduce the amount of data transmitted and the pressure on network transmission. In terms of big data processing, bottlenecks are often reflected in the network rather than the CPU (compression and decompression consume part of CPU resources ). So how can we distinguish whether messages are compressed or uncompressed? Kafka adds
Description compresses attribute bytes.The last two digits of this Byte indicate the encoding used for message compression. If the last two digits are 0, the message is not compressed. See https://cwiki.apache.org/confluence/display/KAFKA/Compression for details
Iv. reliability during Message TransferIn the message system, it is very important to ensure the reliability of messages in the production and consumption processes. In the actual message transmission process, the following three situations may occur:
- An error occurred while sending a message.
- One message is sent multiple times
- Ideal: Exactly-once, a message is successfully sent and only once
Many systems claim that they implement exactly-once, but they ignore the possibility that the producer or consumer may fail in the production and consumption processes. For example, if a producer successfully sends a message, but the message is lost or successfully sent to the broker, it is also successfully removed by the consumer, but the consumer fails to process the acquired message. From the perspective of the producer: Kafka processes the message in this way. When a message is sent, the producer waits for the broker to receive the message feedback successfully (the parameter can be used to control the wait time ), if a message is lost on the way or one of the brokers fails, the producer will resend the message (we know that Kafka has a backup mechanism and can control whether to wait for all backup nodes to receive the message through parameters ). From the consumer perspective: we have talked about partition. The broker records an offset value in partition. This value points to the next message to be consumed by the consumer. When a consumer receives a message but fails during the processing, the consumer can locate the previous message through the offset value and then process it. Consumer also has the permission to control the offset value and perform arbitrary processing on messages that are persisted to the broker.
V. mirror a Kafka ClusterFor the mirror of the Kafka cluster, refer to the figure below: For details, see: https://cwiki.apache.org/confluence/display/KAFKA/Kafka+mirroring
Vi. Backup mechanismThe backup mechanism is a new feature of kafka0.8, which greatly improves the reliability and stability of Kafka clusters. With the backup mechanism, Kafka allows nodes in the cluster to be suspended without affecting the work of the entire cluster. A cluster with N backups allowed n-1 nodes failed. Among all the backup nodes, one node acts as the lead node. This node saves the list of other backup nodes and maintains the parallel synchronization between backups. The following figure illustrates the backup mechanism of Kafka:
See https://cwiki.apache.org/confluence/display/KAFKA/Kafka+Replication for details