ХабрПостыНовостиМегапосты?

Apache Kafka… Basics to drive

What is Apache Kafka?

Apache Kafka is a distributed event-streaming platform designed to handle real-time data feeds. It allows applications to publish, process, and subscribe to streams of data in a highly scalable, fault-tolerant manner.

Core notions in Kafka

  1. Topics:

    • Kafka organizes data into topics, which are similar to tables in a database.

    • A topic is divided into partitions, which enable parallel processing.

  2. Producers:

    • Producers publish data (events/messages) to Kafka topics.

    • Data is written to partitions based on a partitioning key.

  3. Consumers:

    • Consumers read data from topics.

    • Consumers are part of consumer groups, ensuring load balancing.

  4. Brokers:

    • Brokers are the servers that store and serve Kafka data.

    • Kafka clusters usually have multiple brokers for scalability and fault tolerance.

  5. Zookeeper/KRaft:

    • Zookeeper (or the newer KRaft mode) manages metadata and coordinates the cluster.

How Kafka Works

  1. Producers send messages to Kafka topics. Messages are stored in partitions within the topics.

  2. Consumers fetch messages from these partitions. Kafka retains messages for a configurable retention period.

  3. Data is stored in a log-based structure, ensuring high write and read throughput.

Stream processing involves handling and analyzing data in real time, as it flows through systems. This is different from batch processing, where data is processed in chunks at intervals.

Example usage HDFS - Kafka

Steps to Load HDFS Data into Kafka:

Brockers

Producers

Consumers

Broker Coordination


Interconnections in Action

  1. Producers to Brokers:

    • A producer queries the cluster metadata to determine which broker is the leader for a partition.

    • The producer sends messages directly to the leader broker for that partition.

  2. Brokers to Consumers:

    • Consumers fetch metadata from the cluster to discover which partitions belong to their subscribed topic(s).

    • Consumers connect directly to the leader broker(s) for those partitions to fetch data.

  3. Brokers to Each Other:

    • Brokers exchange metadata (e.g., partition assignments and replicas) to keep the cluster consistent.

    • Replication occurs between brokers to ensure fault tolerance.

Data Flow Example

  1. Producer sends a message:

    • Producer connects to the cluster and queries metadata.

    • Metadata response identifies the leader broker for the target partition.

    • Producer sends the message to the leader broker.

  2. Broker stores the message:

    • The leader broker appends the message to the partition’s log.

    • Replication: The leader propagates the message to follower brokers.

  3. Consumer reads the message:

    • Consumer fetches metadata to identify the leader broker for the assigned partition.

    • Consumer connects to the leader broker and fetches messages.

    • Kafka tracks the offset of each message to ensure no data is skipped or duplicated.


Key Advantages of These Interconnections

  1. Scalability:

    • Adding brokers increases the cluster’s capacity.

    • Producers and consumers automatically adjust to the new cluster size.

  2. Fault Tolerance:

    • Replication ensures data availability even if a broker fails.

    • Consumers can continue reading from replicas if a leader broker goes down.

  3. High Throughput:

    • Partitioning enables parallel processing.

    • Producers and consumers can write and read from multiple brokers simultaneously.

      Producers --> [Broker1 (Leader)] <-- Consumers
                    [Broker2 (Follower)]
                    [Broker3 (Follower)]

      In this example:

      • Producer sends data to Broker1, the leader of a partition.

      • Broker2 and Broker3 replicate the data from Broker1.

      • Consumers fetch data from Broker1 (or one of the followers if configured).

Kafka's Storage Model

  1. Log-Based Storage:

    • Kafka organizes data into topics, and each topic is divided into partitions.

    • Each partition is an append-only log stored on disk.

    • Messages are written sequentially to the end of the log, making writes extremely fast due to minimal disk seek overhead.

  2. Retention-Based Storage:

    • Kafka retains messages for a configurable retention period (e.g., 7 days by default) or until the log reaches a certain size.

    • Old messages are automatically deleted after they exceed the retention policy.

    • Kafka is not designed for long-term data storage like HDFS; it is optimized for real-time streaming and temporary storage.

  3. File Organization:

    • Each partition is stored as a set of files on the broker's disk.

    • These files are segmented for efficient access (e.g., log-0, log-1).

    • Kafka indexes these log files to enable fast lookups by message offset.

Kafka vs. HDFS Storage

Feature

Kafka

HDFS

Purpose

Real-time streaming, message queue

Long-term distributed file storage

Data Retention

Configurable (e.g., time/size-based)

Permanent until explicitly deleted

Read/Write Pattern

Append-only log, sequential writes

Random access, distributed writes

Scalability

Horizontal scaling via brokers

Horizontal scaling via DataNodes

Fault Tolerance

Replication at partition level

Replication across DataNodes

Primary Use Case

Real-time data pipelines

Storing large datasets for analysis

How Kafka Handles Storage

  1. Persistence:

    • Kafka stores all data on disk, even though it's a messaging system.

    • This ensures durability and allows consumers to replay messages if needed.

  2. Replication:

    • Messages in Kafka are replicated across brokers for fault tolerance.

    • For example, a topic with a replication factor of 3 will have its data stored on three brokers.

  3. Segmented Storage:

    • Kafka splits each partition's log into segments.

    • When a segment reaches a configured size, Kafka creates a new segment.

    • Old segments are deleted according to the retention policy.

  4. Compaction (Optional):

    • Kafka offers a log compaction feature, which retains only the latest value for each key.

    • This is useful for scenarios like updating state or maintaining a compact view of data.

Kafka does have its own storage, but it is designed for short-term storage and high-throughput message delivery. For long-term storage or big data analysis, Kafka is usually paired with systems like HDFS, S3, or other data lakes.

@Falcon_eye
11.01.2025 19:55 UTC
Первоисточник