Skip to main content
Data Pipelines

Building Real-Time Data Pipelines with Apache Kafka

How to design Kafka pipelines that stay observable and replayable, and when a scheduled job beats a broker: partitions, delivery semantics, replay, sinks.

By February 6, 20269 min readUpdated October 31, 2026
Building Real-Time Data Pipelines with Apache Kafka
Article focusReal-time pipelines only create value when they are observable, replayable, and designed for the downstream decisions they serve.

A Kafka pipeline stays useful in production when four things are designed before the first consumer ships: the event contract, the partition plan, the delivery semantics and the replay path. Before any of that, check that you need Kafka at all. Publishing messages is the easy part; the hard part is a system that stays observable, replayable and affordable as traffic grows.

Start with the downstream decision

Before you create topics or write consumers, define the decision that needs fresh data.

Examples:

  • risk scoring that needs minute-level fraud signals
  • inventory monitoring that needs rapid anomaly alerts
  • support workflows that need agent context from live systems

If no decision becomes materially better with faster data, a batch pipeline is often the better answer. This is the question that decides whether the rest of this article applies to you at all, and it is worth being honest about. A dashboard that a human looks at twice a day does not become more useful because the numbers behind it are four seconds old instead of four hours old.

Check whether you need Kafka at all

Real time is a spectrum, and Kafka sits at one end of it. One of our own pipelines shows where the line usually falls.

An energy market analytics team needed every dispatch report from AEMO, the Australian electricity market operator, which publishes a new zipped CSV every five minutes covering the five regions of the National Electricity Market. Data that arrives as a file every five minutes does not need a broker. The pipeline we built is a scheduled AWS Lambda function, triggered by CloudWatch every five minutes, that fetches the latest report, archives it to S3 (skipped if it already exists) and writes 19 dispatch metrics per region to DynamoDB under a key built from the settlement date and the region. A Plotly Dash app reads those records for the analysts. There is no cluster to run, and a failed run is safe to repeat because both writes are idempotent.

Kafka earns its place when the shape of the problem changes: many producers emitting events continuously, several independent teams consuming the same stream, consumers that replay history at their own pace, or a strict per-key ordering guarantee across services.

OptionFits whenWhat it costs you
Scheduled job (cron, Lambda, a cloud scheduler)The source publishes on an interval, one or two consumers read it, minutes of delay are fineLittle to run; latency is bounded by the schedule
Managed queue (SQS, Pub/Sub)Events arrive continuously, a handful of consumers, little need to replay old historySimple to operate; replay and ordering are limited next to Kafka's offsets
Kafka (self-run or managed)High event volume, many independent consumers, replay by offset, per-key orderingPartitions, schemas, retention and consumer lag to operate every day

If your row is the first or the second, most of this article still applies, since contracts, idempotent sinks and replay matter in any pipeline, but you can skip running a broker.

Design the contract before the code

One of the biggest streaming mistakes is shipping events first and governing them later. In practice, event contracts should be designed before scale arrives.

That means agreeing on:

  • event naming
  • payload ownership
  • versioning rules
  • dead-letter behavior
  • replay expectations

The goal is not bureaucracy. The goal is making sure downstream teams can trust what the stream means.

Make the schema a build-time concern

A schema registry turns "we agreed on the payload" into something the build can enforce. The part worth understanding is the compatibility mode, because it decides who has to deploy first:

  • BACKWARD (the common default): a new schema can read data written with the old one. Consumers upgrade first. Safe for adding optional fields, removing fields.
  • FORWARD: the old schema can read data written with the new one. Producers upgrade first.
  • FULL: both directions hold. The most restrictive, and the right choice for topics with many independent consumers you do not control.

Picking a mode is not a formality. It is the difference between a field rename that rolls out quietly and one that takes down a consumer at 2am because nobody knew which side had to ship first.

Partition design is the decision you cannot undo cheaply

Partitions are where most Kafka regret accumulates, because the two properties they control pull in opposite directions.

Partition count sets your parallelism ceiling. Within a consumer group, one partition is consumed by exactly one consumer. Ten partitions means at most ten useful consumers; the eleventh sits idle. Size from measured throughput rather than a round number: take the peak events per second you need to absorb, divide by what one consumer sustains against your real workload, and add headroom for growth and for recovery after an outage, when you need to catch up faster than real time.

Over-partitioning is not free either. More partitions mean more open file handles, more metadata, and longer rebalances, which is exactly what you do not want during an incident.

The partition key sets your ordering guarantee. Kafka orders messages within a partition, not across a topic. Keying by account_id means every event for one account arrives in order, which is usually the guarantee that actually matters. Keying by something high-cardinality and meaningless, or leaving the key null for round-robin, gives you throughput and no ordering.

Watch for hot keys. If one tenant produces thirty percent of your traffic and you key by tenant, one partition carries thirty percent of the load and one consumer becomes your bottleneck. A composite key such as tenant_id:entity_id spreads the load while preserving the ordering that matters, as long as per-entity ordering is genuinely enough.

Choose your delivery semantics deliberately

"Exactly-once" is the most misread phrase in streaming. It is worth separating two questions.

Inside Kafka, exactly-once is real. An idempotent producer deduplicates retries, and transactions make a read-process-write cycle atomic:

# producer
acks=all
enable.idempotence=true
max.in.flight.requests.per.connection=5
retries=2147483647
delivery.timeout.ms=120000
compression.type=zstd
linger.ms=20
batch.size=131072

# consumer
enable.auto.commit=false
isolation.level=read_committed
auto.offset.reset=earliest
partition.assignment.strategy=org.apache.kafka.clients.consumer.CooperativeStickyAssignor

Two of those lines carry more weight than they look. enable.auto.commit=false means you commit offsets after the work succeeded, not on a timer that can acknowledge messages you never processed. CooperativeStickyAssignor makes rebalances incremental rather than stop-the-world, so adding a consumer no longer pauses every consumer in the group.

Outside Kafka, the moment data lands in a warehouse, a search index, or an API call, transactions no longer cover you. The workable pattern is at-least-once delivery into a sink that is idempotent: carry a stable event_id, and write with a merge rather than an insert. Then a replay produces the same rows instead of duplicates, and you never have to reason about whether a batch was applied once or twice.

Plan for replay on day one

Backfills and consumer restarts are not rare edge cases. They are normal operating events, and the time to design for them is before the first one.

Retention is the first lever. A topic you might need to replay from cannot have seven-day retention if your worst-case recovery window is two weeks:

cleanup.policy=delete
retention.ms=1209600000   # 14 days

For topics that represent current state rather than a history of events, compaction is usually the better fit. It keeps the latest value per key indefinitely and discards superseded ones, so a new consumer can rebuild state from the topic without you storing every intermediate change:

cleanup.policy=compact
min.compaction.lag.ms=3600000

The second lever is the sink. Replay is only safe if reprocessing the same events converges on the same result, which comes back to idempotent writes. A merge keyed on the event identifier makes replay boring:

MERGE INTO analytics.orders AS target
USING staging.order_events AS source
  ON target.event_id = source.event_id
WHEN NOT MATCHED THEN INSERT *
WHEN MATCHED AND source.ingested_at > target.ingested_at THEN UPDATE SET *;

Treat observability as a product feature

If a consumer slows down or silently drops malformed events, the business impact may be larger than a full outage, because nobody gets paged for data that is quietly wrong.

Measure at least these:

  • Consumer lag, the gap between the log end offset and the committed offset, per partition rather than averaged. An average hides the single stuck partition that is actually your problem.
  • Message age, the difference between event time and processing time. This is the one to alert on, because it is the number a business owner understands. "Fraud signals are nine minutes stale" means something; "lag is 40,000" does not.
  • Dead-letter rate, as a proportion of throughput rather than a raw count.
  • Rebalance frequency. Frequent rebalances usually mean max.poll.interval.ms is too tight for how long processing actually takes.

Give poison messages somewhere to go

A single unparseable message should not stall a partition. Route failures to a dead-letter topic with enough context to diagnose them later, and keep the original offset in the headers so you can find the source event:

orders.v1        -> main topic
orders.v1.dlq    -> failures, with headers:
                    x-error-class, x-error-message,
                    x-source-partition, x-source-offset, x-failed-at

The DLQ is only useful if someone looks at it. An alert on its rate is what turns it from a graveyard into a feedback loop.

Keep enrichment close to business value

Not every transform belongs in Kafka. Put lightweight event normalization near the stream, where it keeps the contract clean: field renaming, type coercion, dropping obviously malformed records. Move heavyweight modeling into systems that are easier to test, version, and backfill, which usually means the warehouse.

The practical test is whether the logic needs history. Anything that requires joining against months of data, or that analysts will want to revise later, is modeling work and belongs where a rebuild is cheap.

Protect warehouse cost

Streaming into the warehouse becomes expensive quickly if every message triggers a write. Three patterns carry most of the savings:

  1. Micro-batch rather than per-message writes. Buffer on a size or time trigger, whichever comes first, and write once. This is usually the difference between a warehouse bill that scales with events and one that scales with volume.
  2. Respect the table layout. Partition and cluster sink tables on the columns queries actually filter on, usually event date plus a tenant or entity key.
  3. Compact small files. High-frequency streaming writes produce many small files, and query engines pay for that on every scan afterwards.

Design human-readable failure paths

When something breaks, operators need an answer to two questions fast:

  1. what failed
  2. what action recovers the system safely

That answer should exist before the outage, not after it. In practice that means a short runbook per topic covering where the DLQ is, what the safe replay procedure looks like, and which downstream tables need rebuilding if you do replay. The team that writes this down before the first incident recovers in minutes rather than hours.

Where AI agents fit

Kafka pipelines become more valuable when paired with AI-assisted summaries or anomaly workflows. A useful pattern is:

  • stream events into operational storage
  • aggregate into monitoring tables
  • trigger an agent or assistant only when thresholds matter

That keeps agent activity focused on high-signal events instead of noisy raw traffic, and it keeps the cost predictable. Pointing a model at the raw firehose is both expensive and worse: the signal a reviewer needs is almost always in the aggregate, not the individual event. The same principle shapes our agentic BI work, where agents answer leadership questions from modelled dashboard data rather than from raw events.

A pre-launch checklist

Before a streaming pipeline carries anything a business depends on:

  • the downstream decision is written down, and it improves with fresher data
  • the schema is registered with a compatibility mode the team has agreed on
  • partition key and count come from measured throughput and a stated ordering guarantee
  • the sink is idempotent, so replay converges instead of duplicating
  • retention covers the worst-case recovery window, not the typical one
  • lag alerts fire on message age, per partition
  • a dead-letter topic exists and someone owns its rate
  • the replay runbook exists and has been tested once, deliberately, before it was needed

Related reading: Choosing Batch vs Streaming for Modern Data Pipelines and how we deliver data pipeline development.

The takeaway

Kafka is a great backbone for real-time systems, but only when the pipeline is designed around trust, recovery, and downstream business decisions.

A "working" stream is not enough. A useful streaming platform is one your operators can reason about under pressure, where a replay is a routine procedure rather than an act of courage, and where the cost of freshness is something the team chose on purpose.

Van Data Team in practice

How do I build a real-time Apache Kafka data pipeline that stays observable and replayable?

A Kafka pipeline stays operable when the event contract, partition plan, delivery semantics and replay path are designed before the first consumer ships. We start from the downstream decision that needs fresh data, enforce schemas through a registry with an agreed compatibility mode, size partitions from measured throughput, and add dead-letter handling, failure alerts and safe re-runs. Where faster data does not improve a decision, we recommend a batch pipeline instead.

Tran Tien Van
Tran Tien Van

Founder of Van Data Team. Builds Vanaxity, the AI agent system for SEO, GEO, and AEO, and leads data engineering delivery for B2B teams.

About the author

Article FAQ

Questions readers usually ask next.

These short answers clarify the practical follow-up questions that often come after the main article.

Share this

Need a similar system?

If this article maps to a workflow your team already operates, the next step is usually a scoped review of the system, constraints, and rollout path.

Consultation

Scope your next AI workflow.

Share the system and blockers. We will map fit, gaps, and the next step.

We use these details only to reply about your project. See our privacy policy.