Kokil Thapa - Professional Web Developer in Nepal
Freelancer Web Developer in Nepal with 15+ Years of Experience

Kokil Thapa is an experienced full-stack web developer focused on building fast, secure, and scalable web applications. He helps businesses and individuals create SEO-friendly, user-focused digital platforms designed for long-term growth.

Apache Kafka Fundamentals

By Kokil Thapa | Last reviewed: September 2026

Apache Kafka Fundamentals start with a simple idea: treat business events as an append-only log, not a transient message that disappears after delivery. Orders placed, payments confirmed, documents uploaded, and inventory adjusted all become durable records that multiple services can read at their own pace. If you build REST APIs and webhook-driven integrations, Kafka sits one layer below that surface—it is the backbone when volume, replay, and fan-out outgrow a single database or a basic queue. This guide walks through the core concepts a working engineer needs before wiring Kafka into a Laravel app, a microservice fleet, or a data pipeline.

What are the core components of Apache Kafka Fundamentals?

Kafka is a distributed commit log marketed as an event streaming platform. Unlike a traditional message broker that deletes a message once a consumer acknowledges it, Kafka retains records for a configurable period. That retention window is what makes replay, audit trails, and multiple independent consumers practical.

The five primitives you must internalize are:

  • Topic — a named category of events, such as orders.created or payments.settled.
  • Partition — an ordered, immutable sequence of records inside a topic; parallelism lives here.
  • Broker — a Kafka server that stores partition data and serves read/write requests.
  • Producer — a client that publishes records to a topic partition.
  • Consumer — a client that reads records and tracks its position via an offset.

A Kafka cluster is two or more brokers working together. Cluster metadata— which broker leads which partition—lives in an internal topic called __consumer_offsets and, in modern deployments, is managed by KRaft (Kafka Raft) rather than an external ZooKeeper ensemble. For greenfield clusters in 2026, plan on KRaft mode; ZooKeeper-based installs are legacy.

Kafka Cluster ArchitectureProducersWeb apps, ETLBroker ClusterTopic: ordersPartitions 0, 1, 2Topic: paymentsPartitions 0, 1KRaft metadataConsumersGroups A and BEach partition is an ordered, replicated log shard
Apache Kafka Fundamentals: producers write to topic partitions on brokers; consumer groups read independently.

Official reference material lives in the Apache Kafka documentation. Read the "Introduction" and "Design" sections before you touch production sizing.

How do Kafka topics and partitions work?

A topic is a logical stream. Partitions are the unit of storage, ordering, and parallelism. Records in a single partition are strictly ordered. Records across partitions have no global order unless you design for it.

Choosing a partition key

Producers assign each record to a partition. If you supply a key—such as customer_id—Kafka hashes the key and routes all events for that customer to the same partition. That gives you per-customer ordering. If you omit a key, records round-robin across partitions, which maximizes throughput but sacrifices ordering guarantees.

// Conceptual producer record (Java client style)
ProducerRecord<String, String> record =
    new ProducerRecord<>("orders", order.getCustomerId(), order.toJson());

Replication and the leader-follower model

Each partition has one leader broker that handles reads and writes. Follower brokers replicate the log for fault tolerance. The replication factor—often three in production—defines how many copies exist. Writes succeed when the leader and enough followers acknowledge according to your acks setting.

Partition count is chosen at topic creation. You can add partitions later, but you cannot reduce them without rebuilding the topic. A common mistake is creating too few partitions and hitting a throughput ceiling six months later.

SettingTypical dev valueProduction starting pointTrade-off
Partitions per topic1–36–24 (workload dependent)More partitions = more parallelism, more file handles
Replication factor13Higher durability, more disk and network
Retention1 day7–30 days (compliance may require longer)Longer retention = more disk
min.insync.replicas12 (with RF=3)Prevents silent data loss on broker failure

For deeper coverage of how consumer groups map to partitions, see the companion piece on Kafka consumer groups and partitions.

How does a Kafka producer publish events reliably?

Producers batch records for efficiency. They retry on transient failures. They can enforce ordering within a partition by setting max.in.flight.requests.per.connection=1 when idempotence matters, though the idempotent producer (enabled via enable.idempotence=true) handles most duplicate concerns automatically in modern clients.

The acks ladder

  1. acks=0 — fire-and-forget; fastest, no durability guarantee.
  2. acks=1 — leader acknowledges; risk of loss if leader dies before replication.
  3. acks=all — wait for all in-sync replicas; standard for money-moving or compliance events.
# docker-compose excerpt — local Kafka 3.x with KRaft (illustrative)
services:
  kafka:
    image: apache/kafka:3.9.0
    environment:
      KAFKA_NODE_ID: 1
      KAFKA_PROCESS_ROLES: broker,controller
      KAFKA_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@kafka:9093
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1

Create a topic and produce a test message from the CLI shipped with Kafka:

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

bin/kafka-console-producer.sh --topic orders --bootstrap-server localhost:9092
> {"order_id": 1001, "status": "created"}

In Laravel-centric architectures, producers often live in the app layer or a sidecar service. The app validates business rules, writes to MySQL inside a transaction, then publishes an outbox event—a pattern that avoids "database committed but Kafka message lost" races. That design aligns with how enterprise application development teams treat transactional boundaries.

Producer Publish FlowProducerBatch + retryLeader BrokerPartition 0 logAppend recordFollowersReplicate logIn-sync setAck Decision (acks setting)acks=0No waitacks=1Leader onlyacks=allAll ISR ack
Producer publish flow in Apache Kafka Fundamentals: records append to the leader, replicate to followers, then ack based on policy.

How do Kafka consumer groups and offsets keep readers in sync?

Consumers never delete data from the log. They commit an offset—a pointer to the next record they will read. Offsets are stored in the internal __consumer_offsets topic (or externally via Kafka Connect offset stores for specialized setups).

Consumer group rules

Consumers sharing the same group.id coordinate partition assignment. Each partition is consumed by at most one consumer in the group at a time. If you run three consumers in a group against a topic with six partitions, each consumer typically gets two partitions. Add a seventh consumer and one sits idle—partitions are the upper bound on parallelism.

Different consumer groups are independent. A fraud-detection service and a warehouse analytics job can both read the same topic from the beginning or from "now," each tracking its own offsets. That fan-out model is why Kafka beats a point-to-point queue when many downstream systems need the same event stream.

Consumer Groups and OffsetsTopic: orders (3 partitions)Partition 0Partition 1Partition 2Group: billing2 consumers assignedGroup: analytics1 consumer assignedOffset store per groupEach group tracks its own read position
Consumer groups in Apache Kafka Fundamentals: each group maintains separate offset pointers per partition.

Commit strategies

Auto-commit is convenient for prototypes. It commits offsets on an interval and can double-process records after a crash. Manual commit after successful side effects—database write, email send, payment capture—is the production default. The rule is simple: commit only after your downstream work succeeds, or design consumers to be idempotent.

Compare this mental model with RabbitMQ in the RabbitMQ vs Kafka guide. RabbitMQ excels at task queues and competing consumers with acknowledgment-based removal. Kafka excels at high-volume logs, replay, and many subscribers.

When should you choose Kafka over Redis queues or Laravel Horizon?

Not every project needs Kafka. On a typical Laravel 12 or 13 deployment, Redis-backed queues with Horizon handle order emails, image resizing, and webhook retries cleanly. I've used that stack on production apps including booking and eCommerce systems where throughput stays in the hundreds of jobs per minute.

Kafka earns its operational cost when you hit one or more of these triggers:

  • Event volume exceeds comfortable Redis memory or single-broker limits.
  • Multiple teams need the same event stream with independent consumption speeds.
  • You must replay history after a bug fix or a new analytics pipeline launch.
  • Stream processing—aggregations, joins, windowed counts—is a first-class requirement.
  • Audit and compliance require an immutable event trail beyond database binlogs.

For a digital grocery platform with delivery-zone logic, Redis queues often suffice until a dedicated analytics or partner-integration layer appears. At that point, publishing order.placed events to Kafka while keeping Laravel Horizon for operational jobs is a sane split.

Kafka vs Queue DecisionNew event workload?Low volumeRedis / HorizonTask queueRabbitMQHigh volumeReplay neededChoose KafkaStream platformStart simple; add Kafka when replay or fan-out forces the issue
Decision guide for Apache Kafka Fundamentals: match the broker to volume, replay, and fan-out requirements.

How do you operate and monitor Kafka in production?

Kafka's durability does not remove the need for observability. Treat broker disk, under-replicated partitions, and consumer lag as paging-worthy signals. Consumer lag is the gap between the log end offset and a group's committed offset. Rising lag means downstream processing cannot keep pace.

Metrics that matter

  • Under-replicated partitions — replication is unhealthy; investigate broker loss or network partitions.
  • Offline partitions — no leader available; producers and consumers block for those shards.
  • Request handler idle ratio — sustained low idle on brokers signals CPU or disk saturation.
  • Consumer lag by group — business-visible backlog; tie alerts to SLA thresholds.

Pair Kafka with a metrics stack described in Prometheus monitoring fundamentals. Export JMX metrics via the Kafka exporter or Confluent-compatible agents. Log correlation through request IDs in event payloads simplifies tracing from an HTTP request to a published record.

Schema governance

Raw JSON in topics works until two services disagree on field types. Avro, Protobuf, or JSON Schema with a registry—Confluent Schema Registry or an open-source equivalent—enforces compatibility rules. Set BACKWARD or FULL compatibility before multiple producers share a topic. Breaking schema changes without a migration plan are a common source of silent consumer failures.

Security basics for production clusters include TLS encryption on the wire, SASL authentication, and ACLs limiting which principals can read or write each topic. Never expose plaintext broker ports to the public internet. On Ubuntu servers I administer, Kafka listeners sit behind private network interfaces with firewall rules similar to those used for MySQL and Redis—patterns covered under Linux system administration.

Payload debugging during integration work often starts with structured JSON. A JSON formatter helps validate sample events before you register a schema.

How do web applications integrate with Kafka safely?

Most PHP and Laravel teams do not embed a Kafka client inside every request cycle. Long-lived consumers run as separate worker processes—Supervisor, systemd, or Kubernetes Deployments—while HTTP controllers publish asynchronously.

Transactional outbox pattern

  1. Begin a database transaction.
  2. Insert or update business rows.
  3. Insert a row into an outbox_events table with the serialized payload.
  4. Commit the transaction.
  5. A relay process reads the outbox and publishes to Kafka, marking rows as sent.

This mirrors reliable webhook delivery patterns: persist first, deliver second, retry with backoff on failure. The difference is the transport. Webhooks push to one URL; Kafka retains for many readers.

Rate limiting and abuse prevention on the HTTP edge still matter. See API rate limiting and abuse prevention for the ingress layer while Kafka handles asynchronous fan-out behind it.

For a shipped example of high-throughput order flows in Laravel, review the Quick And Easy Nepalese Grocery portfolio case. Event streaming was not the first tool chosen there; queues and solid database design carried the load until scale demanded more.

Stream processing frameworks—Kafka Streams, Apache Flink, ksqlDB—sit adjacent to core Kafka. They consume topics, compute aggregates, and write derived topics. Treat them as phase-two architecture unless you have a concrete windowed metric requirement on day one.

Related infrastructure topics on this site include Envoy proxy fundamentals for ingress routing and Ceph storage fundamentals when broker disks need a shared block layer across nodes.

Key Takeaways

  • Kafka is a durable, partitioned commit log—not a traditional delete-on-ack queue.
  • Partition count sets your parallelism ceiling; choose keys to preserve ordering where it matters.
  • Use acks=all and replication factor 3 for production events you cannot afford to lose.
  • Consumer groups scale readers; offsets must commit only after successful side effects.
  • Start with Redis or RabbitMQ for modest Laravel workloads; adopt Kafka when replay and multi-subscriber fan-out become requirements.
  • Monitor consumer lag and under-replicated partitions; pair brokers with Prometheus-style metrics and schema registry governance.

People Also Ask

What is the difference between a Kafka topic and a partition?

A topic is the named stream—such as user.signups. Partitions are the physical shards inside that topic, each an ordered log. Producers write to specific partitions; consumers read from assigned partitions within their group. More partitions mean more parallel readers, up to the number of consumers in a group.

Does Kafka delete messages after consumers read them?

No. Consumers advance an offset pointer; records remain until retention policy deletes them by time or size. That design enables replay, late-joining consumers, and audit use cases. If you need automatic removal after processing, a message queue like RabbitMQ is often a better fit.

What is KRaft in Apache Kafka?

KRaft (Kafka Raft) is Kafka's built-in metadata quorum that replaces Apache ZooKeeper for controller duties. New clusters in 2026 should deploy in KRaft mode for simpler operations and faster metadata recovery. Legacy ZooKeeper-based clusters still exist but are on a deprecation path—check the version-specific migration guide on kafka.apache.org before upgrading.

How many brokers do you need for a production Kafka cluster?

Three brokers is the common minimum when replication factor is three and you want to survive a single broker loss without losing write availability. Smaller teams sometimes run two brokers for non-critical staging environments. Production also demands separate disks for log directories, monitored free space, and a clear backup strategy for topic data you cannot rebuild from upstream sources.

Build event-driven systems with the right foundation

Apache Kafka Fundamentals boil down to a durable log, smart partitioning, and consumer groups that scale independently. Master those ideas before you provision a three-broker cluster or wire schema registry policies. Match the tool to the workload—Redis queues for operational jobs, Kafka when history, fan-out, or stream analytics become non-negotiable.

If you are planning an event-driven platform, API layer, or migration from synchronous webhooks to a stream backbone, contact us or explore custom software development services to scope architecture that fits your team size and budget. For ongoing broker tuning and observability, support and maintenance keeps production lag dashboards green after launch.

Frequently Asked Questions

Apache Kafka is a distributed commit log used as an event streaming platform. Business events are stored as durable, append-only records in topics, not transient messages that disappear after delivery.

The five primitives are topics (named event streams like orders.created), partitions (ordered shards inside a topic), brokers (servers storing and serving data), producers (writers), and consumers (readers tracking position via offsets). Consumer groups coordinate parallel readers. A cluster runs two or more brokers. Modern deployments use KRaft for cluster metadata instead of ZooKeeper. For greenfield clusters in 2026, plan on KRaft mode; ZooKeeper-based installs are legacy.

A topic is the logical, named stream—such as user.signups or payments.settled. Partitions are the physical shards inside that topic, each an ordered, immutable sequence of records. Producers write to specific partitions; consumers in a group read from assigned partitions. More partitions allow more parallel readers, up to the number of consumers in a group. Records in one partition are strictly ordered; records across partitions have no global order unless you design for it with partition keys.

No. Consumers advance an offset pointer; the log retains records until retention policy removes them by time or size. That enables replay, late-joining consumers, and audit trails.

Producers assign each record to a partition. With a key such as customer_id, Kafka hashes it and routes all events for that customer to the same partition, giving per-customer ordering. Without a key, records round-robin across partitions, which maximizes throughput but sacrifices ordering guarantees. In Laravel-centric architectures, publishing order events keyed by customer_id after a database transaction is a common pattern when downstream services must process a single customer's events in sequence.

Use replication factor 3 and acks=all for events you cannot afford to lose, such as payments or compliance records. With RF=3, set min.insync.replicas=2 so writes fail if too few followers are in sync, preventing silent data loss on broker failure. acks=0 is fire-and-forget with no durability guarantee. acks=1 acknowledges the leader only, risking loss if the leader dies before replication completes. Enable enable.idempotence=true on producers to handle duplicate concerns in modern clients.

Consumers sharing the same group.id coordinate partition assignment—each partition is consumed by at most one consumer in the group at a time. With six partitions and three consumers, each typically gets two; a seventh consumer sits idle because partitions cap parallelism. Different consumer groups read the same topic independently with separate offset pointers. Offsets live in the internal __consumer_offsets topic. Auto-commit suits prototypes; production should manually commit only after downstream side effects succeed, or design idempotent consumers.

On typical Laravel 12 or 13 deployments, Redis-backed queues with Horizon handle order emails, image resizing, and webhook retries cleanly at hundreds of jobs per minute. Kafka earns its operational cost when event volume exceeds comfortable Redis limits, multiple teams need the same stream at independent speeds, you must replay history after a bug fix or new pipeline, stream processing is required, or compliance demands an immutable event trail. A sane split keeps Horizon for operational jobs and publishes order.placed events to Kafka when analytics or partner integrations appear.

KRaft (Kafka Raft) manages cluster metadata— which broker leads which partition—internally rather than through an external ZooKeeper ensemble. The article's local docker-compose example runs Kafka 3.9.0 in KRaft mode with KAFKA_PROCESS_ROLES set to broker,controller. For greenfield clusters in 2026, KRaft is the planned default. ZooKeeper-based installs are legacy. KRaft simplifies operations by removing a separate coordination service you must patch, monitor, and keep highly available alongside your brokers.

Partition count is set at topic creation and defines your parallelism ceiling. You can add partitions later but cannot reduce them without rebuilding the topic. A common mistake is too few partitions and hitting a throughput ceiling months later. Dev setups often use 1–3 partitions; production starting points run 6–24 depending on workload. More partitions mean more parallelism but also more file handles and broker overhead. Match partition count to expected consumer group size and throughput, not a arbitrary small number.

Most PHP and Laravel teams do not embed a Kafka client in every HTTP request cycle. Instead, begin a database transaction, insert or update business rows, insert a serialized payload into an outbox_events table, then commit. A relay process reads the outbox, publishes to Kafka, and marks rows as sent. This avoids the race where MySQL commits but the Kafka message is lost. Long-lived consumers run as separate worker processes under Supervisor, systemd, or Kubernetes Deployments while controllers publish asynchronously.

Consumer lag is the gap between the log end offset and a consumer group's committed offset. Rising lag means downstream processing cannot keep pace with producers—a business-visible backlog. Treat it as a paging-worthy signal alongside under-replicated partitions and offline partitions. Tie alerts to SLA thresholds per consumer group. On production clusters I administer, lag spikes often trace to slow database writes, unoptimized consumer batch sizes, or a new downstream service reading from the beginning of a high-volume topic without enough partitions.

RabbitMQ excels at task queues and competing consumers with acknowledgment-based message removal. Kafka excels at high-volume logs, replay, and many independent subscribers reading the same topic at their own pace. Kafka consumers never delete data from the log; they commit offsets. RabbitMQ removes messages once acknowledged. Choose RabbitMQ for job dispatch and request-reply patterns. Choose Kafka when fan-out, retention, and replay outgrow a single database or basic queue—patterns familiar if you already build REST APIs and webhook-driven integrations.

Track under-replicated partitions (replication unhealthy—investigate broker loss or network issues), offline partitions (no leader—producers and consumers block), request handler idle ratio (sustained low idle signals CPU or disk saturation), and consumer lag by group. Export JMX metrics via the Kafka exporter or Confluent-compatible agents and pair with a Prometheus-style stack. Log correlation through request IDs in event payloads helps trace from an HTTP request to a published record. Kafka's durability does not remove the need for observability.

Raw JSON works until two services disagree on field types. Use Avro, Protobuf, or JSON Schema with a registry such as Confluent Schema Registry, setting BACKWARD or FULL compatibility before multiple producers share a topic. Breaking schema changes without a migration plan cause silent consumer failures. For security, enable TLS on the wire, SASL authentication, and ACLs limiting which principals read or write each topic. Never expose plaintext broker ports to the public internet; listeners should sit behind private network interfaces with firewall rules similar to MySQL and Redis.

Share this article

0 Comments

Leave a comment

Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

Quick Contact Options
Choose how you want to connect me: