October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Kafka Partitioning and Message Ordering Explained for Go Developers

Kafka orders records within a partition, not across a multi-partition topic. Go developers can preserve per-key order with stable keys, an explicit partition balancer, and careful consumer offset commits.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Kafka preserves message order within a partition, not across every partition in a topic. In Go, keep related events in sequence by assigning them a stable key—such as an account ID—and configuring the producer’s partitioner to route that key consistently to the same partition. This preserves per-key order while allowing different keys on different partitions to be processed in parallel.

What ordering Kafka guarantees

A Kafka topic is made up of partitions, and each partition is an ordered log. Apache Kafka documents that messages sent by a producer to a particular topic-partition are appended in send order; consumers read records in the order stored in that partition’s log. Kafka’s ordering guarantee is therefore partition-local.

When a topic has multiple partitions, records in different partitions can be consumed at different times and in parallel. Kafka does not define a single total order across those partitions. If one event is in partition 0 and another is in partition 1, their relative consumption or processing time does not establish which came first for the topic as a whole.

How to preserve order for related events

Use a stable message key that represents the entity whose events need a shared sequence. For example, key account balance events by account ID. The producer must consistently map the same key to the same partition; then those events share that partition’s ordered log. This guarantees order for that key’s records as stored, not a total order between different keys.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Partition assignment is a producer-side choice. A key alone is not a universal guarantee: it must be paired with a compatible key-based partitioner, and producers writing related events must use a consistent assignment strategy. Kafka’s producer documentation describes partition selection and keyed records. See the Kafka producer configuration documentation.

Configure key-based partitioning with kafka-go

In kafka-go, inspect the Writer’s Balancer setting rather than assuming a Go client default. Its Hash balancer routes records with the same key to the same partition. Other choices, including round-robin and least-bytes balancing, distribute records differently and may not keep related events together. The kafka-go Balancer documentation describes the available strategies.

w := &kafka.Writer{
    Addr:     kafka.TCP("localhost:9092"),
    Topic:    "account-events",
    Balancer: &kafka.Hash{},
}

err := w.WriteMessages(ctx,
    kafka.Message{Key: []byte("account-42"), Value: []byte("deposit")},
    kafka.Message{Key: []byte("account-42"), Value: []byte("withdrawal")},
)

This example makes the intended partitioning strategy explicit: both events carry the same key, and the writer uses the hash balancer. Confirm the API and behavior against the version of kafka-go in your application. If you change partitioning behavior or the topic’s partition count, verify how that affects key-to-partition assignment; do not assume an existing key’s events will forever map identically after a topology or partitioner change.

Choose partition count for both ordering and parallelism

A consumer group distributes a topic’s partitions among its members. Members can process separate partitions in parallel, while each partition retains its own log order. A single partition gives the topic one partition sequence, but only one group member can actively read that partition at a time. Adding more group members than the topic has partitions does not give that group more active partition-level readers.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Design Ordering scope Parallelism implication Best fit
One partition One sequence for the topic’s records At most one group member actively reads that partition Use when a single sequence is essential and the throughput and parallelism tradeoff is acceptable.
Multiple partitions with stable key routing Per key, provided related records consistently map to one partition Different partitions can be processed in parallel Usually appropriate when each entity needs ordered events but separate entities can proceed independently.
Unkeyed or distributing assignment Partition-local only; related records may land in different partitions Can distribute work, depending on the balancer Use when spreading load matters more than keeping related events in one sequence.

One partition is not the only way to preserve useful order. If the requirement is “events for each account must be sequential,” stable key routing lets different accounts use different partitions, retaining concurrency across accounts. If the requirement truly is “every event in the topic must have one shared sequence,” a single partition provides that sequence, with the associated limit on partition-level consumer parallelism.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep consumer processing and offset commits in order

Receiving records in partition order is different from finishing application work in that order. If a consumer hands records from one partition to concurrent workers, a later record may finish first. Committing its offset too early can move the group’s committed position past earlier work that has not finished.

In kafka-go, group-mode ReadMessage automatically commits offsets. For explicit control, use FetchMessage and then CommitMessages after processing. The library documents that committing the highest offset for a partition commits earlier offsets in that partition too. See the kafka-go Reader commit documentation.

  1. Fetch: retrieve records from the group with FetchMessage.
  2. Process: complete the application work for a record before treating it as safe to commit.
  3. Commit safely: if processing records concurrently, track completion by partition and commit only a position that does not skip unfinished earlier work when that matters to your delivery requirements.

This matters when a consumer restarts: work whose offset has already been committed will not be replayed from the group’s previous committed position. A higher offset is not an independent acknowledgment for only one message; it represents progress in that partition.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Checklist for an ordering requirement

  • Define what must be ordered: all topic records, all events for one entity, or only records handled by a particular worker.
  • For per-entity order, choose a stable key and explicitly configure a compatible key-based balancer, such as kafka-go’s Hash.
  • Make sure all producers of the related events use compatible key and partitioning rules.
  • Choose a partition count that supports the desired consumer-group parallelism without implying a cross-partition total order.
  • If processing concurrently, prevent commits from moving beyond unfinished earlier records in the same partition.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.