MESSAGING & EVENT-DRIVEN ARCHITECTURE / SYSTEM CONCEPT BRIEF

Partitions

In messaging systems, a partition is an ordered, append-only subset of a topic's messages.

IntermediatePhase 06 / Topic 13 of 18RequirementsTrade-offsFailure modes
01

Overview

In messaging systems, a partition is an ordered, append-only subset of a topic's messages. Partitions are the unit of parallelism (one consumer per partition per group), ordering (guaranteed only within a partition), and replication (each partition has a leader and followers). A message's key determines its partition, usually by hashing.

Choosing the key and the number of partitions is the central design decision for a Kafka or Kinesis topic. A good key spreads load evenly while keeping related events (same order, same user) together so they are processed in order.

Lanes on a highway

More lanes carry more cars at once, and cars in the same lane stay in order. If everyone crowds into one lane (a hot key), the other lanes do not help.

02

When to use it

  • Designing Kafka topics or Kinesis streams.
  • Guaranteeing per-entity ordering.
  • Scaling consumer throughput.
  • Diagnosing hot partitions and lag.
03

Where it shows up in interviews

Partition key design

Recognize it when: events for an entity must stay ordered.

  • Design order event streams
  • Design a chat message pipeline
Throughput planning

Recognize it when: how many partitions for this load?

  • Design a logging pipeline
  • Design IoT ingestion
04

Where it is used in real software

Kafka default partitioner

Uses murmur2 hash of the key modulo partition count; keyless messages use sticky batching across partitions.

Kinesis shards

The same concept: partition keys hash to shards with fixed throughput.

Hot partition incidents

Celebrity accounts or a single large tenant can overload one partition, causing lag for everyone sharing it.

05

Key terms

Partition key
Value hashed to select a partition.
Leader replica
Handles reads and writes for the partition.
Hot partition
Partition receiving disproportionate traffic.
Repartitioning
Changing partition count, which remaps keys.
06

How it works, step by step

  1. 1
    Identify ordering needs

    Which events must be processed in order relative to each other?

  2. 2
    Choose the key

    The entity ID for that ordering (orderId, userId).

  3. 3
    Estimate throughput

    Partitions >= peak throughput / per-partition throughput.

  4. 4
    Plan consumer parallelism

    Partitions >= max consumers you expect.

  5. 5
    Check key distribution

    Look for skew; handle heavy keys specially.

07

Choosing a partition key for order events

Events: OrderPlaced, OrderPaid, OrderShipped

Step 1 / 4
KeyOrderingDistributionVerdict
orderIdPer order (what we need)EvenGood
customerIdPer customerSkewed by big buyersOK if per-customer order matters
countryPer countryVery skewedBad
null (no key)NoneEvenOnly if order does not matter

NOWKey: orderId | Ordering: Per order (what we need) | Distribution: Even | Verdict: Good

Key by the smallest entity whose events must stay in order; that usually gives both correctness and even load.

08

Implementation

// Estimate partitions and check key skew before creating a topicfunction partitionsNeeded(peakMBps: number, perPartitionMBps: number, maxConsumers: number) {  return Math.max(Math.ceil(peakMBps / perPartitionMBps), maxConsumers);} function keySkew(keys: string[], partitions: number, hash: (k: string) => number) {  const counts = new Array(partitions).fill(0);  for (const k of keys) counts[hash(k) % partitions]++;  const avg = keys.length / partitions;  return Math.max(...counts) / avg; // 1.0 = perfectly even; > 2 means a hot partition} console.log(partitionsNeeded(120, 10, 8)); // 12
09

Complexity and performance

Partition lookupO(1)

hash(key) mod N.

Throughput~linear with partitions

Until broker limits.

10

Trade-offs

Ordering vs parallelism

Coarser keys preserve more ordering but concentrate load; finer keys spread load but order less.

Changing partition counts

Increasing partitions remaps keys, breaking per-key order during the transition.

11

Variants and related techniques

Custom partitioners

Route heavy keys to dedicated partitions.

Key salting

Append a suffix to spread a hot key, at the cost of ordering for that key.

12

Common mistakes

  • Too few partitions.

    Fix: Cannot scale consumers later without remapping keys; plan for growth.

  • Low-cardinality keys.

    Fix: Few distinct keys means few busy partitions.

13

Interview questions

How do you keep events for one order in sequence?

Use the order ID as the partition key so all its events go to the same partition, which is consumed in order by a single consumer in the group.

What happens when you add partitions to a Kafka topic?

New messages for some keys go to different partitions, so per-key ordering can break between old and new messages; existing data is not moved.

14

Practice problems

ProblemDifficultyWhat it trains
Choose keys for 4 topicsEasyOrdering vs distribution.
Fix a hot partition caused by a large tenantMediumSalting and dedicated partitions.