📰 IT Security NachrichtenNIST and CISA finalize playbook to stop token theft and forgery(16.09.2026 um 09:40 Uhr)
📰 IT Security NachrichtenQnap TS-432XeU review: A super-slim rack NAS at an equally small price(16.09.2026 um 09:00 Uhr)
🕵️ SicherheitslückenGoogle fixes actively exploited Android zero-day on Pixel devices(16.09.2026 um 09:00 Uhr)
📰 IT Security NachrichtenExternal Forecasting in SAP IBP: Externe Prognosen standardnah integrieren(16.09.2026 um 07:03 Uhr)
📰 IT Security NachrichtenSteht ein Social-Media-Verbot für Kinder bevor? Klärung erwartet(16.09.2026 um 08:14 Uhr)
📰 IT Security NachrichtenSalesforce und Nvidia bringen CRM-Modell Koa heraus(16.09.2026 um 08:42 Uhr)
📰 IT Security NachrichtenVertrauen steigert Erfolg von KI-Projekten(16.09.2026 um 09:17 Uhr)
🔧 AI Nachrichten Nvidia-Chef Huang lehnt KI-Regulierung ab(16.09.2026 um 09:38 Uhr)
📰 IT Security NachrichtenNIST and CISA finalize playbook to stop token theft and forgery(16.09.2026 um 09:40 Uhr)
📰 IT Security NachrichtenQnap TS-432XeU review: A super-slim rack NAS at an equally small price(16.09.2026 um 09:00 Uhr)
🕵️ SicherheitslückenGoogle fixes actively exploited Android zero-day on Pixel devices(16.09.2026 um 09:00 Uhr)
📰 IT Security NachrichtenExternal Forecasting in SAP IBP: Externe Prognosen standardnah integrieren(16.09.2026 um 07:03 Uhr)
📰 IT Security NachrichtenSteht ein Social-Media-Verbot für Kinder bevor? Klärung erwartet(16.09.2026 um 08:14 Uhr)
📰 IT Security NachrichtenSalesforce und Nvidia bringen CRM-Modell Koa heraus(16.09.2026 um 08:42 Uhr)
📰 IT Security NachrichtenVertrauen steigert Erfolg von KI-Projekten(16.09.2026 um 09:17 Uhr)
🔧 AI Nachrichten Nvidia-Chef Huang lehnt KI-Regulierung ab(16.09.2026 um 09:38 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 19 Min Lesezeit
0

Introduction to Messaging Systems with Kafka

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Table of Contents





  1. Introduction: Messaging Systems with Kafka




    • What Are Messaging Systems?

    • Why Use Messaging Systems?

    • Real World Use Cases

    • Why Kafka?




  2. Core Concepts of Messaging Systems and Kafka




    • Core Concepts of Messaging Systems

    • Core Concepts in Kafka

    • Kafka in Action: Example of a Ride Hailing App




  3. Hands-On: Setting Up Kafka




    • What Is Required to Run Kafka?


    • Installation Options


      • Using Docker (Recommended)

      • Native Installation



    • Verifying the Installation

    • Exploring Kafka Topics

    • Producing Messages

    • Consuming Messages

    • Simulating Real World Scenarios




  4. Building Your First Kafka Application




    • Setting Up the Environment

    • Writing a Kafka Producer

    • Writing a Kafka Consumer

    • Enhancing the Application

    • Testing the Application



  5. Conclusion







I. Introduction: Messaging Systems with Kafka






1. What Are Messaging Systems?



Messaging systems are the backbone of modern distributed architectures, enabling communication between independent services or applications. They work by transmitting messages (small packets of information) from one system to another, ensuring these systems can work asynchronously and independently.



Imagine an e-commerce platform where a customer places an order. Here’s what happens:





  1. Order Service: Accepts and stores the order details.


  2. Payment Service: Processes the payment.


  3. Inventory Service: Updates stock levels.


  4. Notification Service: Sends an email or SMS confirmation.



Rather than directly linking these services, a messaging system ensures each service receives and processes the required information through messages in a decoupled manner.






2. Why Use Messaging Systems?





  • Asynchronous Communication: Producers (senders) and consumers (receivers) don’t need to interact directly or operate simultaneously.


  • Scalability: Systems can handle high loads by scaling producers, consumers, or both.


  • Resilience: Messages persist even if a consumer is temporarily unavailable.


  • Flexibility: Multiple services can act on the same data, enabling publish-subscribe patterns.






3. Real World Use Cases





  1. Realtime Analytics:


    • Streaming live data to dashboards for insights (e.g., stock prices, website metrics).




  2. Log Aggregation:


    • Collecting logs from various servers for centralized monitoring and analysis.




  3. Event Driven Applications:


    • Handling user actions (e.g., clicking a button) and triggering downstream processes.




  4. Microservices Communication:


    • Decoupling independent services in a microservices architecture.








4. Why Kafka?



Apache Kafka is a distributed messaging system originally built by LinkedIn and now maintained by the Apache Software Foundation. Kafka goes beyond simple message queues, offering features that make it a powerful choice for handling real-time data streams and building event-driven systems.



Key Benefits of Kafka





  1. High Throughput and Low Latency



    Kafka can handle millions of messages per second with minimal delay, making it ideal for applications requiring real-time processing, such as financial transactions, log aggregation, and IoT data streams.





    • Why It Matters: Systems like RabbitMQ and ActiveMQ are great for traditional queuing but struggle under extremely high loads due to their broker-centric architectures. Kafka’s distributed design overcomes this limitation.




  2. Scalability



    Kafka scales horizontally by adding brokers (servers). Data is distributed across partitions, which can be processed independently by consumers.





    • Comparison: RabbitMQ relies on adding more queues for scale, but this approach can become complex to manage in larger systems. Kafka’s native partitioning simplifies scaling.




  3. Fault Tolerance and Durability



    Kafka replicates data across brokers, ensuring no messages are lost even if a broker fails. This is achieved through configurable replication factors.





    • Compared to Others: Systems like RabbitMQ require specific configurations or external tools for similar fault tolerance, while Kafka includes it natively as part of its design.




  4. Event Streaming at Scale



    Kafka isn’t just a messaging queue; it’s an event-streaming platform. Kafka supports replaying messages by retaining data for a configurable time, enabling applications to process historical data.





    • Advantage Over Queues: Traditional messaging systems delete messages once consumed. Kafka stores them for the duration of the retention policy, allowing multiple consumers to process the same data independently.




  5. Flexible Communication Patterns



    Kafka supports both publish-subscribe and point-to-point messaging models, making it versatile for various use cases.





    • Comparison: While RabbitMQ excels in transactional, one-to-one communications, Kafka’s multi-consumer design is better for analytics, streaming, and large-scale applications.




  6. Integration Ecosystem



    Kafka integrates seamlessly with a wide range of tools and frameworks:





    • Kafka Connect: For syncing with databases, Elasticsearch, Hadoop, etc.


    • Kafka Streams: For processing data in real time.


    • KSQL: For querying and transforming data using SQL-like syntax.


    • Compared to Others: While RabbitMQ or ActiveMQ may need third-party plugins for complex integrations, Kafka provides these capabilities natively.





When to Choose Kafka?





  • Use Kafka if:


    • Your system needs to process large volumes of data in real time.

    • You require scalable, fault-tolerant messaging.

    • You’re building a data pipeline or event-streaming system (e.g., log aggregation, metrics collection, user activity tracking).

    • Multiple consumers need to process the same data independently (e.g., notifications and analytics).








  • Consider Other Solutions if:


    • You need lightweight, transactional messaging with lower setup complexity (RabbitMQ or ActiveMQ might be better for simple use cases).

    • Your system is event-driven but does not process massive amounts of data.








Kafka’s Versatility in Real-World Applications





  1. Log Aggregation: Collect logs from multiple servers into a central repository for real-time monitoring.


  2. Event Sourcing: Track all user interactions (clicks, purchases, etc.) for downstream analysis.


  3. Real-Time Analytics: Stream data to dashboards or analytics systems for instant insights.


  4. Decoupling Microservices: Enable services to communicate asynchronously without direct dependencies.






II. Core Concepts of Messaging Systems and Kafka



To effectively work with Kafka and messaging systems, it’s crucial to understand their core building blocks. This section introduces the foundational concepts that underpin messaging systems in general and Kafka in particular.






1. Core Concepts of Messaging Systems





  • Producers and Consumers



    • Producers:


      • Applications that send data (messages) to a messaging system.

      • In Kafka, producers send messages to topics.








    • Consumers:


      • Applications that read messages from the messaging system.

      • Kafka consumers subscribe to topics and process messages either individually or as part of a group.












  • Topics


    • A topic is a category or feed to which messages are sent by producers and from which consumers retrieve messages.

    • Kafka topics are partitioned for scalability and performance.

    • Example:


      • Topic: user-signups


        • Messages: {"user_id": 101, "action": "signup"}, {"user_id": 102, "action": "signup"}
















  • Queues and Publish-Subscribe Models



    • Queue (Point-to-Point):


      • Messages are delivered to a single consumer.

      • Example: A task queue where only one worker processes each task.
        Point to point








    • Publish-Subscribe:


      • Messages are broadcast to multiple consumers.

      • Example: A topic that multiple services subscribe to, such as notifications and analytics.
        Publish-Subscribe












  • Message Delivery Semantics


    • Ensures how messages are delivered:



      • At-least-once: Messages might be delivered multiple times but never lost.


      • At-most-once: Messages might be lost but never duplicated.


      • Exactly-once: Messages are delivered precisely once (Kafka supports this with transactional guarantees).















2. Core Concepts in Kafka



Kafka builds on these general concepts, adding its own unique architecture and features.



and download the latest binary release.

  • Extract the downloaded file:


  • CODE
     tar -xzf kafka_2.13-<version>.tgz
    cd kafka_2.13-<version>




  • Start Zookeeper:




    • Use the built-in Zookeeper script to start Zookeeper:


    CODE
     bin/zookeeper-server-start.sh config/zookeeper.properties





  • Expected Output:




    CODE
       [INFO] binding to port 0.0.0.0/0.0.0.0:2181
    [INFO] Snapshot taken
    [INFO] Started admin server on port [8080]






    If you see "binding to port" and "Started admin server," Zookeeper is running successfully.





    1. Start Kafka Broker:


      • Open a new terminal and start Kafka:






    CODE
    bin/kafka-server-start.sh config/server.properties






    Expected Output:




    CODE
    Kafka server started
    INFO Registered broker 0 at path /brokers/ids/0








    1. Verify Kafka is Running:


      • Test your installation as described in 3. Verifying the Installation.
        ### 3. Verifying the Installation
        Step 1: List Existing Topics






    CODE
    bin/kafka-topics.sh --list --bootstrap-server localhost:9092






    What This Does:




    • Lists all topics currently managed by the Kafka broker.

    • Confirms Kafka is running properly.
      Expected Output:




    CODE
    (no output, as no topics exist yet)






    Step 2: Create a Topic




    CODE
    bin/kafka-topics.sh --create --topic test-topic --partitions 3 --replication-factor 1 --bootstrap-server localhost:9092







    What This Does:




    • Creates a new topic named test-topic.

    • Splits the topic into 3 partitions (for parallel processing).

    • Sets the replication factor to 1 (no redundancy).
      Expected Output:




    CODE
    Created topic test-topic.






    Step 3: Describe the Topic




    CODE
    bin/kafka-topics.sh --describe --topic test-topic --bootstrap-server localhost:9092






    What This Does:




    • Displays details about the topic, including partition count, replication factor, and leader assignments.
      Expected Output:




    CODE
    Topic: test-topic   PartitionCount: 3    ReplicationFactor: 1    Configs:
    Partition: 0 Leader: 0 Replicas: 0 Isr: 0
    Partition: 1 Leader: 0 Replicas: 0 Isr: 0
    Partition: 2 Leader: 0 Replicas: 0 Isr: 0









    4. Exploring Kafka Topics



    Create Multiple Topics

    Let’s create two topics, user-signups and order-events, each tailored for specific use cases.





    1. Create user-signups:



    CODE
    bin/kafka-topics.sh --create --topic user-signups --partitions 2 --replication-factor 1 --bootstrap-server localhost:9092







    • user-signups captures new user registrations.

    • Two partitions enable parallel processing.
      Expected Output:



    CODE
    Created topic user-signups.







    1. Create order-events:



    CODE
    bin/kafka-topics.sh --create --topic order-events --partitions 3 --replication-factor 1 --bootstrap-server localhost:9092







    • order-events records e-commerce transactions.

    • Three partitions ensure scalability for high-volume data.
      Expected Output:



    CODE
    Created topic order-events.





    List All Topics

    To confirm the topics were created:




    CODE
    bin/kafka-topics.sh --list --bootstrap-server localhost:9092






    Expected Output:




    CODE
    user-signups
    order-events









    5. Producing Messages



    Send Data to user-signups:




    1. Start a producer:




    CODE
    bin/kafka-console-producer.sh --topic user-signups --bootstrap-server localhost:9092







    1. Enter user signup events:




    CODE
    {"user_id": 1, "name": "Alice", "email": "alice@example.com"}
    {"user_id": 2, "name": "Bob", "email": "bob@example.com"}







    • Producing user signups mimics a real-world registration system.
      Send Data to order-events:

    • Start another producer:




    CODE
    bin/kafka-console-producer.sh --topic order-events --bootstrap-server localhost:9092







    1. Enter order events:




    CODE
    {"order_id": 101, "user_id": 1, "total": 99.99}
    {"order_id": 102, "user_id": 2, "total": 49.50}







    CODE
    - Simulates e-commerce transactions.






    6. Consuming Messages



    Consume Data from user-signups:




    1. Start a consumer:



    CODE
    bin/kafka-console-consumer.sh --topic user-signups --from-beginning --bootstrap-server localhost:9092






    Expected Output:




    CODE
    {"user_id": 1, "name": "Alice", "email": "alice@example.com"}
    {"user_id": 2, "name": "Bob", "email": "bob@example.com"}






    Consume Data from order-events:




    1. Start another consumer:




    CODE
    bin/kafka-console-consumer.sh --topic order-events --from-beginning --bootstrap-server localhost:9092






    Expected Output:




    CODE
    {"order_id": 101, "user_id": 1, "total": 99.99}
    {"order_id": 102, "user_id": 2, "total": 49.50}









    7. Simulating Real World Scenarios



    Offset Management




    1. Produce additional messages to order-events:




    CODE
    {"order_id": 103, "user_id": 1, "total": 75.00}







    1. Start a consumer and consume from the latest message only:




    CODE
    bin/kafka-console-consumer.sh --topic order-events --bootstrap-server localhost:9092






    Expected Output:




    CODE
    {"order_id": 103, "user_id": 1, "total": 75.00}







    1. Rewind to read all messages:




    CODE
    bin/kafka-console-consumer.sh --topic order-events --from-beginning --bootstrap-server localhost:9092






    Expected Output:




    CODE
    {"order_id": 101, "user_id": 1, "total": 99.99}
    {"order_id": 102, "user_id": 2, "total": 49.50}
    {"order_id": 103, "user_id": 1, "total": 75.00}






    Using Consumer Groups




    1. Start Consumer Group A for order-events:




    CODE
    bin/kafka-console-consumer.sh --topic order-events --group group-a --bootstrap-server localhost:9092







    • Consumer groups allow multiple consumers to process messages in parallel.

    • Produce more messages to order-events:




    CODE
    {"order_id": 104, "user_id": 3, "total": 120.00}







    1. Start Consumer Group B for order-events:




    CODE
    bin/kafka-console-consumer.sh --topic order-events --group group-b --bootstrap-server localhost:9092






    Expected Outputs:





    • Consumer Group A: Continues consuming from its last offset.




    CODE
    {"order_id": 104, "user_id": 3, "total": 120.00}








    • Consumer Group B: Reads all messages from the beginning if it's new.




    CODE
    {"order_id": 101, "user_id": 1, "total": 99.99}
    {"order_id": 102, "user_id": 2, "total": 49.50}
    {"order_id": 103, "user_id": 1, "total": 75.00}
    {"order_id": 104, "user_id": 3, "total": 120.00}






    Testing Replication




    1. Create a topic with replication:




    CODE
    bin/kafka-topics.sh --create --topic replicated-topic --partitions 2 --replication-factor 2 --bootstrap-server localhost:9092







    1. Check the topic details:




    CODE
    bin/kafka-topics.sh --describe --topic replicated-topic --bootstrap-server localhost:9092






    Expected Output:




    CODE
        Topic: replicated-topic   PartitionCount: 2    ReplicationFactor: 2    Configs:
    Partition: 0 Leader: 0 Replicas: 0,1 Isr: 0,1
    Partition: 1 Leader: 1 Replicas: 1,0 Isr: 1,0









    IV. Building Your First Kafka Application



    Now that Kafka is set up and you’ve explored its features through the CLI, let’s build a simple producer and consumer application using a programming language. For this section, we’ll use Python with the kafka-python library, a popular choice for interacting with Kafka.






    1. Setting Up the Environment



    Step 1: Install Python

    Ensure Python is installed on your system:




    CODE
    python --version






    Expected Output:




    CODE
    Python 3.x.x






    Step 2: Install kafka-python

    Install the Kafka client library for Python:




    CODE
    pip install kafka-python






    What This Does:




    • Installs kafka-python, which provides tools for producing and consuming messages in Kafka.
      ### 2. Writing a Kafka Producer
      The producer sends messages to a Kafka topic.
      Step 1: Create a File for the Producer
      Create a new file named producer.py and paste the following code:




    CODE
    from kafka import KafkaProducer
    import json

    # Initialize Kafka Producer
    producer = KafkaProducer(
    bootstrap_servers='localhost:9092', # Kafka broker address
    value_serializer=lambda v: json.dumps(v).encode('utf-8') # Serialize messages to JSON
    )

    # Topic Name
    topic = 'user-signups'

    # Send Messages
    messages = [
    {"user_id": 1, "name": "Alice", "email": "[email protected]"},
    {"user_id": 2, "name": "Bob", "email": "[email protected]"}
    ]

    for message in messages:
    producer.send(topic, value=message)
    print(f"Sent: {message}")

    # Close Producer
    producer.close()






    Explanation:





    • KafkaProducer:


      • Connects to the Kafka broker (localhost:9092).

      • Sends JSON-encoded messages.








    • Topic:


      • Sends messages to the user-signups topic.
        Step 2: Run the Producer
        Run the script:









    CODE
    python producer.py






    Expected Output:




    CODE
    Sent: {'user_id': 1, 'name': 'Alice', 'email': '[email protected]'}
    Sent: {'user_id': 2, 'name': 'Bob', 'email': '[email protected]'}






    Verify in Kafka:

    Start a Kafka consumer to confirm the messages:




    CODE
    bin/kafka-console-consumer.sh --topic user-signups --from-beginning --bootstrap-server localhost:9092







    Expected Output:




    CODE
    user_id": 1, "name": "Alice", "email": "alice@example.com"}
    {
    "user_id": 2, "name": "Bob", "email": "bob@example.com"}










    3. Writing a Kafka Consumer



    The consumer retrieves messages from a Kafka topic.

    Step 1: Create a File for the Consumer

    Create a new file named consumer.py and paste the following code:




    CODE
    from kafka import KafkaConsumer
    import json

    # Initialize Kafka Consumer
    consumer = KafkaConsumer(
    'user-signups', # Topic to subscribe to
    bootstrap_servers='localhost:9092',
    auto_offset_reset='earliest', # Start reading from the beginning
    group_id='user-signups-group', # Consumer group
    value_deserializer=lambda x: json.loads(x.decode('utf-8')) # Deserialize messages from JSON
    )

    # Consume Messages
    print("Listening for messages...")
    for message in consumer:
    print(f"Received: {message.value}")






    Explanation:





    • KafkaConsumer:


      • Subscribes to the user-signups topic.

      • Starts reading from the earliest offset.








    • Group ID:


      • Ensures that multiple consumers can work together to process the same topic.
        Step 2: Run the Consumer
        Run the script:









    CODE
    python consumer.py






    Expected Output:




    CODE
    Listening for messages...
    Received: {'user_id': 1, 'name': 'Alice', 'email': '[email protected]'}
    Received: {'user_id': 2, 'name': 'Bob', 'email': '[email protected]'}









    4. Enhancing the Application



    Add a Producer to Another Topic

    Expand the producer to send data to a second topic (order-events):




    CODE
    # Additional Topic
    order_topic = 'order-events'

    orders = [
    {"order_id": 101, "user_id": 1, "total": 99.99},
    {"order_id": 102, "user_id": 2, "total": 49.50}
    ]

    for order in orders:
    producer.send(order_topic, value=order)
    print(f"Sent to {order_topic}: {order}")






    Add a Second Consumer

    Create a new consumer for the order-events topic:




    CODE
    # Initialize Kafka Consumer for another topic
    order_consumer = KafkaConsumer(
    'order-events',
    bootstrap_servers='localhost:9092',
    auto_offset_reset='earliest',
    group_id='order-events-group',
    value_deserializer=lambda x: json.loads(x.decode('utf-8'))
    )

    # Consume Messages
    print("Listening for order events...")
    for order in order_consumer:
    print(f"Order Received: {order.value}")









    5. Testing the Application



    Step 1: Start Both Consumers




    1. Run consumer.py to listen to user-signups.

    2. Run the new consumer for order-events.
      Step 2: Run the Producer
      Run producer.py to send messages to both topics.
      Expected Outputs:


    3. User Signups Consumer:


      CODE
      Received: {'user_id': 1, 'name': 'Alice', 'email': '[email protected]'}
      Received: {'user_id': 2, 'name': 'Bob', 'email': '[email protected]'}







    • Order Events Consumer:


      CODE
      Order Received: {'order_id': 101, 'user_id': 1, 'total': 99.99}
      Order Received: {'order_id': 102, 'user_id': 2, 'total': 49.50}








    V. Conclusion



    Kafka has revolutionized how modern systems handle messaging and real-time data processing. By decoupling producers and consumers, it allows for scalable, fault-tolerant, and flexible architectures that are crucial for today’s distributed systems. Through this course, you’ve gained a foundational understanding of Kafka’s core components, such as topics, partitions, brokers, and consumer groups, as well as hands-on experience with producing and consuming messages.



    As a highly versatile platform, Kafka can serve as the backbone for a wide variety of applications, including real-time analytics, event-driven systems, and log aggregation pipelines. While this course focused on the basics, Kafka’s ecosystem extends far beyond messaging. Tools like Kafka Streams, KSQL, and Kafka Connect empower developers to build powerful stream processing applications and integrate seamlessly with other systems.



    Whether you’re building microservices, processing IoT data, or scaling enterprise systems, Kafka provides the tools and reliability needed for success. With this introduction as your foundation, you’re now ready to explore Kafka’s advanced features and unlock its full potential for solving complex, real-world challenges.

    Vollständiger Original-Artikel
    Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
    ↗ Original-Artikel auf dev.to lesen
    Wie bewertest du diesen Beitrag?
    1 Klick Feedback
    Teilen mit Netzwerk & Team:

    Community-Analysen & Experten-Meinungen 0

    Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
    Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
    Community Pulse: Relevanz-Einschätzung
    1 Klick Experten-Votum
    🔴 Akute Relevanz 0%
    🟡 In Evaluierung 0%
    🟢 Keine Auswirkung 0%
    Spannende Innovation 0%
    Verwandte Story-Cluster & Quellen (Vektor-KI)
    Port 8095 Engine
    1 Quelle
    Eine Tonne „grüner“ Stahl am Tag: US-Startup entwickelt neuen Ofen – und will diese alte Industrie umkrempeln
    1 Quelle
    Diversität im Backlash: Was Unternehmen jetzt tun sollten
    1 Quelle
    Größer als die Autobranche: Deutschlands Digitalunternehmen erwirtschaften 481 Milliarden Euro
    Ähnliche Beiträge
    🔍 Verwandte News

    Auch interessante Nachrichten Introduction to Messaging Systems with Kafka

    Thematisch verwandte Begriffe: Introduction, Messaging, Systems, with · 6 Treffer

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...