Apache Kafka Use Cases
Apache Kafka is a platform for handling real-time data. It is used when applications need to send, receive and process large amounts of data quickly and reliably. Kafka works like a central pipeline through which data flows between many different systems.
This guide explains Kafka’s most important use cases in simple terms, with small working examples for many of them: activity tracking, microservices events, change data capture, stream processing, log and metrics pipelines and more. It ends with a complete online-shopping example and an honest look at when Kafka is not the right tool. New to Kafka? Start with Kafka Introduction.
1. Why Do We Need Kafka?
Modern applications generate data continuously:
- Users click, search and browse websites and apps.
- Customers place orders and make payments.
- Servers write logs and metrics every second.
- Devices and sensors send readings.
- Microservices tell each other what has happened.
Sending all this data directly from each system to every other system quickly becomes slow, fragile and hard to manage: every new consumer needs a new connection, and if a receiver is down, data is lost.
Without Kafka: every system talks to every other system
Orders ---> Analytics Orders ---> Search
Orders ---> Email Payments -> Analytics
Clicks ---> Analytics Payments -> Fraud ... (many fragile links)
With Kafka: one central pipeline
Orders ---+ +---> Analytics
Payments --+--> [ Kafka ] ---+---> Email
Clicks ---+ +---> Search, Fraud, Data warehouse ...
Kafka solves this by acting as a durable, high-speed buffer and message hub. Producers write once; any number of consumers read independently, at their own pace, and can even re-read past data.
2. Kafka Use Cases at a Glance
| Use case | What Kafka does | Typical consumers |
|---|---|---|
| Real-time streaming | Moves data the moment it is created | Live dashboards, notifications |
| Activity tracking | Collects clicks, views and searches | Analytics, recommendations |
| Microservices communication | Carries events between services | Order, payment, shipping services |
| Event sourcing | Stores every change as an event | Services rebuilding their state |
| Log aggregation | Collects logs from many servers | Search and monitoring tools |
| Metrics | Collects operational numbers | Monitoring and alerting |
| Change data capture | Streams database changes | Search indexes, caches, data warehouses |
| Stream processing | Transforms and analyzes data on the fly | Fraud detection, real-time aggregates |
| Data integration | Moves data between systems with connectors | Databases, data lakes, cloud storage |
| IoT | Ingests readings from many devices | Alerting, analytics, digital twins |
3. Real-Time Data Streaming
Kafka is widely used to stream data in real time. Applications publish data continuously, and other systems can read it within milliseconds.
Examples: live order tracking, live stock prices, sports scores, delivery driver locations and in-app notifications.
Because Kafka stores the data, a consumer that was briefly offline does not miss anything: it continues from where it stopped.
4. Website Activity Tracking
This was Kafka’s original use case. Every user action, such as a page view, search or “add to cart”, becomes an event. Many teams then use the same stream: analytics, personalization, A/B testing and fraud detection.
package com.example.tracking;
import java.time.Instant;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
@Service
public class ActivityTracker {
private final KafkaTemplate<String, String> kafka;
public ActivityTracker(KafkaTemplate<String, String> kafka) {
this.kafka = kafka;
}
public void track(String userId, String action, String productId) {
String event = """
{"userId":"%s","action":"%s","productId":"%s","at":"%s"}
""".formatted(userId, action, productId, Instant.now()).strip();
// Key = user ID, so one user's actions stay in order
kafka.send("user-activity", userId, event);
}
}
The web application calls activityTracker.track(...) and moves on immediately; it does not wait for analytics or recommendations to process the event.
5. Microservices Communication
In a microservices architecture, services need to tell each other what has happened. With Kafka, a service publishes an event and does not need to know who listens. Services stay loosely coupled, and if one is down, its events wait safely in Kafka until it is back.
// order-service: publishes an event after saving the order
kafka.send("orders", order.getId(), orderPlacedJson);
// shipping-service: reacts in its own time
@KafkaListener(topics = "orders", groupId = "shipping-service")
public void onOrderPlaced(String orderPlacedJson) {
shipmentService.prepareShipment(orderPlacedJson);
}
// notification-service: reacts to the same event independently
@KafkaListener(topics = "orders", groupId = "notification-service")
public void sendConfirmation(String orderPlacedJson) {
emailService.sendOrderConfirmation(orderPlacedJson);
}
See Asynchronous Communication in Microservices for more patterns.
6. Event-Driven Architecture and Event Sourcing
Kafka fits event-driven systems naturally. Whenever something happens, such as order placed, payment successful or item shipped, an event is published, and every interested system reacts.
A step further is event sourcing: instead of storing only the current state, a service stores every change as an event. The current state can always be rebuilt by replaying the events, which also gives a complete audit history.
Topic "account-events" (key = account-42)
offset 0: AccountOpened balance 0
offset 1: MoneyDeposited +500 -> 500
offset 2: MoneyWithdrawn -120 -> 380
offset 3: MoneyDeposited +50 -> 430
Current balance = replay all events for account-42 = 430
Kafka’s ordered, durable log and per-key ordering make it a strong foundation for both. See Event-Driven Architecture.
7. Log Aggregation
Servers and applications produce huge amounts of logs. Kafka collects logs from many machines and delivers them to search and monitoring systems, such as Elasticsearch or OpenSearch, and to long-term storage.
- Log shippers such as Fluent Bit, Vector or Filebeat send log lines to a Kafka topic.
- Kafka buffers them, so a slow or offline search cluster does not cause lost logs.
- Several consumers use the same logs: search, security analysis and archiving.
This helps teams investigate errors, performance problems and suspicious behavior across hundreds of servers in one place.
8. Metrics and Monitoring
Similar to logs, applications and infrastructure produce metrics: request rates, response times, CPU and memory usage, business numbers such as orders per minute. Kafka can carry these metrics from many sources to monitoring, alerting and analytics systems, decoupling the producers from the tools that store and display them.
9. Change Data Capture (CDC)
Change data capture (CDC) turns every insert, update and delete in a database into an event, without changing the application. A popular open-source tool for this is Debezium, which runs on Kafka Connect and reads the database’s transaction log.
{
"name": "shop-orders-cdc",
"config": {
"connector.class": "io.debezium.connector.postgresql.PostgresConnector",
"database.hostname": "postgres",
"database.port": "5432",
"database.user": "debezium",
"database.password": "${file:/secrets/db.properties:password}",
"database.dbname": "shop",
"topic.prefix": "shop",
"table.include.list": "public.orders,public.customers",
"plugin.name": "pgoutput"
}
}
# Register the connector with Kafka Connect
curl -X POST -H "Content-Type: application/json" \
--data @postgres-orders-connector.json \
http://localhost:8083/connectors
The password is read from a file through Kafka Connect’s FileConfigProvider (enabled in the Connect worker settings), so it is not stored in the connector configuration itself. Every change to the orders table now appears in the topic shop.public.orders. Typical uses: keeping a search index or cache in sync, feeding a data warehouse, and gradually splitting a monolith into microservices without touching its code. See Kafka Connect.
10. Real-Time Stream Processing
Kafka does not only move data; with Kafka Streams, a Java library, you can process it as it flows: filter, transform, join and aggregate. This example counts how many times each product is viewed per minute:
package com.example.streams;
import java.time.Duration;
import java.util.Properties;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.Produced;
import org.apache.kafka.streams.kstream.TimeWindows;
public class ProductViewCounter {
public static void main(String[] args) {
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "product-view-counter");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.StringSerde.class);
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.StringSerde.class);
StreamsBuilder builder = new StreamsBuilder();
// Input: key = product ID, value = view event (JSON)
KStream<String, String> views = builder.stream("product-views");
views.groupByKey()
.windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(1)))
.count()
.toStream()
.map((window, count) -> KeyValue.pair(window.key(), count))
.to("product-views-per-minute", Produced.with(Serdes.String(), Serdes.Long()));
KafkaStreams streams = new KafkaStreams(builder.build(), props);
Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
streams.start();
}
}
The results land in a new topic, ready for a live “trending products” widget. Similar code powers fraud detection (flag unusual payments within seconds), real-time pricing and alerting. See Kafka Streams.
11. Big Data, Analytics and Data Integration
Kafka is often the entry point for big data and analytics. It feeds data continuously into data lakes, data warehouses and processing engines such as Apache Spark and Apache Flink.
With Kafka Connect sink connectors, topics can be written to other systems with configuration instead of code, for example a JDBC sink that copies an orders topic into a reporting database, or a cloud-storage sink that archives events to object storage.
Source connectors Kafka Sink connectors
(bring data in) (send data out)
PostgreSQL (CDC) ----+ +---> Data warehouse
MySQL (CDC) ---------+--> [ topics ] --------+---> Elasticsearch / OpenSearch
Files / APIs --------+ +---> Cloud object storage
12. IoT and Sensor Data
Connected devices, such as vehicles, smart meters, factory machines and wearables, send continuous streams of readings. Kafka can ingest data from very large numbers of devices, usually through a gateway or an MQTT bridge, and make it available for:
- Real-time alerts, such as a temperature above a threshold.
- Analytics over long periods.
- Predictive maintenance, spotting failing machines before they break.
13. Kafka Use Cases by Industry
| Industry | Example use cases |
|---|---|
| E-commerce and retail | Order events, inventory updates, recommendations, clickstream analytics |
| Banking and payments | Payment processing, fraud detection, account event histories, regulatory reporting |
| Transport and delivery | Live vehicle tracking, route updates, delivery status notifications |
| Media and streaming | Viewing activity, personalization, quality-of-service monitoring |
| Telecommunications | Network monitoring, usage records, real-time billing |
| Manufacturing | Sensor data, predictive maintenance, production monitoring |
| Healthcare | Device readings, patient event streams, integration between hospital systems |
| Gaming | Player events, leaderboards, cheat detection, in-game analytics |
14. A Real-World Example: Online Shopping
Consider an e-commerce application. When a user places an order, several things must happen:
- The order service saves the order.
- The payment service processes the payment.
- The inventory service updates stock.
- The notification service sends a confirmation email.
14.1 Without Kafka
The order service calls each other service directly, one after another. If one of them is slow or down, the whole order process slows down or fails, and adding a new step means changing the order service.
14.2 With Kafka
- The order service saves the order and publishes an
OrderPlacedevent to Kafka. - The payment service reads the event and processes the payment.
- The inventory service reads the event and updates stock.
- The notification service reads the event and sends the email.
+--> Payment Service (group: payments)
Order Service --OrderPlaced--> |
[ topic: orders ] +--> Inventory Service (group: inventory)
|
+--> Notification Service (group: notifications)
All services work independently. Kafka keeps the event safely even if a service is temporarily unavailable; that service simply catches up when it returns. Next month, an analytics or loyalty service can start reading the same topic without any change to the order service.
15. Benefits of Using Kafka
| Benefit | What it means |
|---|---|
| High performance | Handles very large numbers of messages per second with low latency |
| Large volumes | Stores and moves huge amounts of data across many brokers |
| Fault tolerance | Replicated data survives server failures |
| Decoupling | Producers and consumers do not need to know about each other |
| Replay | Consumers can re-read past data to recover or rebuild |
| Scalability | Add brokers and partitions as traffic grows |
| Rich ecosystem | Kafka Connect, Kafka Streams and many client libraries |
16. When Kafka Is Not the Right Choice
Kafka is powerful, but not always the best tool:
| Situation | Better option |
|---|---|
| Small application with low message volume | A simple queue, such as RabbitMQ, or a database table |
| Request–response calls that need an immediate answer | REST or gRPC |
| Task queues needing per-message priorities, delays or complex routing | RabbitMQ |
| Storing data for ad-hoc queries | A database or data warehouse (Kafka can feed it) |
| Very small team with no capacity to operate a cluster | A managed Kafka service, or a simpler tool |
See Kafka vs RabbitMQ and Kafka vs JMS for detailed comparisons.
17. Summary
Apache Kafka is a powerful platform for handling real-time data and communication between systems. It is used for real-time streaming, website activity tracking, microservices communication, event-driven architecture and event sourcing, log and metrics pipelines, change data capture, stream processing, big data integration and IoT. By acting as a durable, scalable central pipeline, Kafka makes applications more reliable, scalable and easier to extend. If your system deals with high data volumes or real-time events that many systems need, Kafka is a strong choice; for small, simple or request–response needs, lighter tools may fit better. Kafka is not just a messaging system: it is a backbone for modern, data-driven applications. Continue with Kafka Architecture to see how it works inside.
18. Frequently Asked Questions (FAQ)
Q1. What is Apache Kafka mainly used for?
Kafka is mainly used to move and process streams of events in real time between many systems. The most common uses are:
- Event-driven communication between microservices.
- Website and app activity tracking.
- Log and metrics collection.
- Streaming database changes (change data capture).
- Real-time analytics and stream processing.
- Feeding data lakes and data warehouses.
Q2. Which companies use Kafka?
Kafka is used by a large share of big technology, finance, retail, telecom and transport companies, as well as many small and medium businesses. It was originally created at LinkedIn to track user activity, and is now an Apache Software Foundation project used worldwide.
It is also available as a managed service from all major cloud providers, which has made it common in smaller organizations too.
Q3. Is Kafka only for big companies with huge data?
No. Kafka shines at large scale, but many teams use it for moderate volumes because of its durability, replay and decoupling: several services can react to the same events, and new consumers can be added later without changing producers.
For very small, simple needs, a lighter tool may be easier. Managed Kafka services reduce the operational burden for smaller teams.
Q4. Can Kafka be used as a database?
Kafka stores data durably and can keep it for a long time, even forever. With log compaction, it keeps the latest value for each key, like a simple key–value store.
But Kafka is not a general-purpose database: it has no ad-hoc queries, indexes or joins. A common pattern is to use Kafka as the source of events and build queryable views in databases or search engines from it.
Q5. What is website activity tracking with Kafka?
Every user action, such as a page view, search, click or purchase, is sent to Kafka as an event. Many teams then read the same events: analytics dashboards, recommendation engines, A/B testing and fraud detection.
This was Kafka’s original use case at LinkedIn, and it remains one of the most common.
Q6. How is Kafka used in microservices?
Services publish events, such as OrderPlaced, and other services subscribe to them. The publisher does not need to know who listens, and listeners can be offline for a while and catch up later.
This loose coupling lets teams add new services without changing existing ones, and keeps the system working when some services are temporarily down.
Q7. What is change data capture (CDC)?
CDC turns every insert, update and delete in a database into an event, usually by reading the database’s transaction log. Tools such as Debezium run on Kafka Connect and publish these changes to Kafka topics.
It is used to keep search indexes, caches and data warehouses in sync, and to migrate from monoliths to microservices without changing the old application’s code.
Q8. What is event sourcing, and why use Kafka for it?
In event sourcing, a system stores every change as an event instead of only the latest state. The current state is rebuilt by replaying the events.
Kafka suits this because it keeps events in order per key, stores them durably, and lets consumers replay them from the beginning. Many teams combine Kafka with a database snapshot of the current state for fast queries.
Q9. What is Kafka Streams used for?
Kafka Streams is a Java library for processing data while it flows through Kafka: filtering, transforming, joining and aggregating streams in real time, with results written back to Kafka topics.
Typical uses: real-time counts and dashboards, fraud detection, enriching events with reference data, and detecting patterns within time windows.
Q10. How does Kafka help with log aggregation?
Log shippers on each server send log lines to a Kafka topic. Kafka buffers them reliably and delivers them to search and monitoring tools, security systems and long-term archives.
If the search cluster is slow or down, logs wait safely in Kafka instead of being lost, and several tools can use the same logs.
Q11. Can Kafka be used for IoT?
Yes. Kafka can ingest large streams of readings from vehicles, meters, machines and wearables, usually through a gateway or MQTT bridge, since devices often speak MQTT rather than Kafka’s protocol.
From Kafka, the data feeds real-time alerts, dashboards, long-term analytics and predictive maintenance.
Q12. Is Kafka good for fraud detection?
Yes, it is a common use case. Payment and login events flow through Kafka, and a stream-processing application, such as Kafka Streams or Apache Flink, checks each event against rules or models within milliseconds.
Suspicious events are written to a separate topic, where alerting or blocking services act on them immediately.
Q13. Can Kafka replace a REST API between services?
Not for everything. Kafka is ideal for events: “this happened, react if you care”. When a service needs an immediate answer, such as checking a price before showing a page, a synchronous REST or gRPC call is simpler.
Most systems use both: synchronous calls for queries, Kafka for events and follow-up work.
Q14. What is Kafka Connect used for?
Kafka Connect moves data between Kafka and other systems using ready-made connectors, configured with JSON instead of code.
- Source connectors bring data in, for example from databases (CDC), files or APIs.
- Sink connectors send data out, for example to data warehouses, search engines or cloud storage.
Q15. How long can Kafka keep data?
As long as you configure. The default retention is 7 days, but topics can keep data for hours, months or forever, or be limited by size. Log compaction keeps only the latest value per key.
Newer Kafka versions also support tiered storage, which moves older data to cheaper object storage, making long retention more affordable.
Q16. Is Kafka suitable for real-time notifications?
Yes, as the pipeline behind them. Events such as “order shipped” flow through Kafka to a notification service, which sends emails, text messages or push notifications.
Kafka itself does not push messages to browsers or phones; the notification service, or a WebSocket gateway, delivers them to users.
Q17. What are the costs and challenges of using Kafka?
- Operations: running a reliable cluster needs skills in configuration, monitoring and upgrades.
- Infrastructure: brokers, disks for retention and replication, and network bandwidth.
- Design: choosing topics, partitions, keys and message formats carefully.
- Consumers: handling duplicates, ordering and failures correctly.
Managed Kafka services remove most of the operational work, at a price.
Q18. When should I not use Kafka?
- The application is small with low message volume.
- You need an immediate request–response answer.
- You need per-message priorities, delays or complex routing (consider RabbitMQ).
- You need ad-hoc queries over stored data (use a database).
- No one on the team can operate it, and a managed service is not an option.
Q19. Can I use Kafka with Spring Boot?
Yes, easily, with Spring for Apache Kafka. Add spring-kafka, configure spring.kafka.bootstrap-servers, then:
kafkaTemplate.send("orders", orderId, orderJson); // publish
@KafkaListener(topics = "orders", groupId = "shipping-service")
void onOrder(String orderJson) { ... } // consume
Spring Boot also supports Kafka Streams and testing with an embedded Kafka or Testcontainers.
Q20. How do I start learning Kafka through a real use case?
- Run Kafka locally with Docker:
docker run -d -p 9092:9092 apache/kafka:latest. - Build a small producer that sends “order placed” events.
- Add two consumers in different groups, such as email and analytics, and watch both receive every event.
- Stop one consumer, send more events, restart it and see it catch up.
- Add a Kafka Streams application that counts orders per minute.