DEV Community

Cover image for Kafka | everything you need to get started
Oussama Belhadi
Oussama Belhadi

Posted on

Kafka | everything you need to get started

Decoding Kafka: The Architectural Shift to Scalable, Decoupled Systems

Apache Kafka is one of the most transformative technologies in modern system design, moving applications from sluggish, tightly coupled messes to real-time, scalable data streams. But what is it, and why is everyone so excited about it?

Let's dive into the problem Kafka solves and its fundamental concepts, using the real-life example of an e-commerce platform.


1. The Tightly Coupled Trap

When a startup builds an e-commerce application, they often start with the simplest microservices architecture where services call each other directly.

tightly-coupled-example

Imagine an Order Service that needs to tell the Payment, Inventory, Analytics, and Notification services about a new order. The communication is synchronous:

"Hey Inventory, update your stock. Wait for the confirmation. Hey Payment, process this. Wait again..."

It looks like this in Programming :

programming-example

While straightforward at first, this approach leads to a nightmare when traffic spikes, such as during a busy holiday season:

  • Tight Coupling: If the Payment Service or the Inventory Service goes down, the entire order process freezes, blocking the Order Service from completing its task.
  • Synchronous Communication: Each order becomes a game of dominoes. One slow service delays the entire chain reaction, causing customers to stare at loading screens.
  • Single Points of Failure: An outage for one essential service can mean hours of order backlogs and lost sales.

The solution is to decouple these services by introducing a highly reliable middleman.


2. Introducing Kafka: The Post Office Analogy

Think of Kafka as a central Post Office or mail delivery service.

kafka-central-post_office-example

When you ship a package, you don't personally drive to every person who needs to know about that shipment. You hand it over to the post office and go home. You trust the post office (the middleman) to handle the delivery.

Kafka is that middleman. Instead of services calling each other directly, they hand their information to Kafka and immediately get back to work.

This simple change moves the architecture from a synchronous "hot potato" game to an asynchronous, scalable conveyor belt of items.


3. Kafka's Core Concepts

To understand how Kafka works, we need to know its main components:

Concept E-commerce Example Post Office Analogy
Event An order placed, a payment failed, stock updated. The letter or package being sent.
Producer The Order Service writes a new "Order Placed" event to Kafka. The sender dropping off a package at the counter.
Broker A Kafka server that stores and manages the events. A post office branch or the entire mail delivery infrastructure.
Topic A category or feed for a stream of related events, e.g., orders, payments, inventory. A dedicated section for a type of mail (letters, large packages, international mail).
Consumer Services subscribed to a Topic, e.g., Notification Service, Inventory Service. The recipient who is notified and picks up their mail.

The Flow of an Event

chain-of-event

  1. The Order Service (Producer) creates an Event (an order payload with all details) and writes it to the orders Topic.
  2. The Order Service immediately moves on to the next task—it doesn't wait.
  3. The Notification Service (Consumer), Inventory Service (Consumer), and Analytics Dashboard (Consumer) are all subscribed to the orders Topic.
  4. Kafka notifies all subscribed consumers about the new event.
  5. Each consumer performs its own action independently and in parallel:
    • Notification: Sends a confirmation email to the customer.
    • Inventory: Updates the stock level in the database.
    • Payment: Generates the invoice.

chain-of-events

Because they are decoupled, the failure of the Inventory Service will not stop the Notification Service from sending the email or the Order Service from accepting new orders.


4. Achieving Scalability: Partitions and Consumer Groups

Kafka's true power lies in its ability to handle millions of events per second. It achieves this primarily through two concepts: Partitions and Consumer Groups.

Partitions: Scaling Writes and Reads

A Topic is divided into ordered, immutable sequences called Partitions.

  • Analogy: If the "Letters" section of the Post Office gets overloaded, you add more workers. But instead of random assignments, you distribute work based on a criteria: "Ann processes letters for Europe, Steve handles the US."
  • In Kafka: The orders Topic might be partitioned into EU_orders, US_orders, and Asia_orders. This allows Producers to write data to different partitions in parallel, significantly increasing throughput. Partitions also allow the storage load to be distributed across multiple Brokers (Kafka servers).

Consumer Groups: Scaling Processing

When orders come in too fast, a single consumer service (like Inventory) can get overwhelmed.

  • Analogy: The letters are arriving, but the single recipient is getting buried under the pile. You need helpers to sort through the mail.
  • In Kafka: You can start additional instances (replicas) of the same service (e.g., three instances of the Inventory Service). These replicas form a Consumer Group.
  • Kafka automatically distributes the partitions among the consumers in the group. If the orders Topic has three partitions, each of the three Inventory Service instances will be assigned one partition, allowing them to process the data in parallel.

5. Beyond Messaging: The Power of Streams

Unlike traditional message queues, Kafka persists every event/message as long as you need (based on a configurable retention policy). This enables powerful use cases like Stream Processing.

  • Traditional Message Queue: Once a message is read, it's deleted (like watching live TV—if you miss it, it's gone).
  • Kafka (Stream Platform): Events are saved long-term, and Consumers can read them anytime, even multiple times (like watching a streaming service—you can pause, replay, or start from the beginning).

This capability is essential for real-time analytics and complex event processing, like this Driver Live location example:

live-strean-example

  • Real-time Analytics: The Analytics Service can continuously stream all Order and Payment events to update sales dashboards and revenue numbers in real time.
  • Stateful Processing: An application can constantly process incoming Inventory events, check if the stock dropped below a certain threshold, and immediately trigger a low-inventory alert, initiating an automatic restock order.

By decoupling services, enabling massive scale through partitioning, and providing stream-processing capabilities, Apache Kafka becomes the circulatory system for a modern, data-driven application.

I have found this video very helpful, most screenshots and points discussed are extracted from it.

Top comments (0)