AlgoViz

Amazon Kinesis Data Streams

medium 26 min

Kinesis Data Streams is a managed log on AWS.

See it run

Send a record to its shard, split a busy shard, read with checkpoints, and see why one hot key cannot be scaled away.

Operations

Put a record

From a key to a durable position.

Step 1 of 5

Send

Our systemProducerskey user-7Shard 1low hashesShard 2high hashesKCL workerreads bothDynamoDBcheckpoints

A producer sends a click with the partition key user-7.

Step 1: Send

def put(key, data):    h = md5(key)    shard = shard_owning(h)    if shard.over_limit(): raise Throttled    copy_to_three_zones(data)    return shard.id, next_sequence()
Delay
Milliseconds
Limit
1 MB/s or 1,000 rec/s
Fire and forget
may lose the record

A timeout is not a failure. The record may be stored, and the retry makes a second copy.

In the real world

The same amazon kinesis data streams doing real jobs. Pick one and step through it.

One account sends most of the traffic.

Step 1 of 3

Even

Our systemProducersShard 1low hashesShard 2high hashesKCL workerreads bothDynamoDBcheckpoints
Shard 1
50%
Shard 2
48%

Both shards are half busy.

Step 1: Even

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A time bucket as the partition key

    critical

    All writes of a minute go to one shard while the others idle.

    Wrong
    key = f"bucket-{now() // 60}"
    Right
    key = user_id
  • Ignoring partial failure

    critical

    PutRecords can succeed while single records failed. They are simply lost.

    Wrong
    client.put_records(Records=batch)
    Right
    res = client.put_records(Records=batch)todo = [r for r, x in zip(batch, res["Records"])        if "ErrorCode" in x]
  • Deduplicating by sequence number

    major

    A producer retry creates a new sequence number for the same event.

    Wrong
    seen.add(record.sequence_number)
    Right
    seen.add(event["id"])
  • Checkpoint before processing

    major

    A crash after the checkpoint skips records for good.

    Wrong
    checkpoint(); process(batch)
    Right
    process(batch); checkpoint()

Interview tips

  • How to explain it

    • A managed log made of shards.
    • Key hash chooses the shard, order is per shard.
    • Readers keep positions, records stay until retention ends.
  • Memory trick

    • One in, two out, per second, in MB.
    • Colour decides the lane.
    • The clerk notes the last boat.
  • Reach for it when

    • Several consumers need the same live data on AWS.
    • You want to replay.
    • Nobody wants to run brokers.
  • Be careful when

    • One key is heavy.
    • Retention is shorter than an outage might be.
    • You need a second Region.