Amazon Kinesis Data Streams
medium 26 minKinesis 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
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
- 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
criticalAll writes of a minute go to one shard while the others idle.
Wrongkey = f"bucket-{now() // 60}"Rightkey = user_idIgnoring partial failure
criticalPutRecords can succeed while single records failed. They are simply lost.
Wrongclient.put_records(Records=batch)Rightres = client.put_records(Records=batch)todo = [r for r, x in zip(batch, res["Records"]) if "ErrorCode" in x]Deduplicating by sequence number
majorA producer retry creates a new sequence number for the same event.
Wrongseen.add(record.sequence_number)Rightseen.add(event["id"])Checkpoint before processing
majorA crash after the checkpoint skips records for good.
Wrongcheckpoint(); process(batch)Rightprocess(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.