Lesson 1 of 2 Amazon Kinesis

Ingesting Real-Time Data with Kinesis Data Streams

This lesson explains how Amazon Kinesis Data Streams ingests large volumes of real-time data into shards, which consumers read in order and can replay.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how shards provide capacity and how a partition key chooses a record's shard.
  • Explain why a partition key with too few values creates a hot shard.
  • Describe how retention and per-consumer positions let several applications read and replay the same data.
  • Distinguish shared-throughput consumers from enhanced fan-out consumers.

Shards and capacity

A Kinesis data stream is divided into shards, and each shard is an ordered sequence of records with a fixed capacity. When a producer writes a record, it supplies a partition key; Kinesis hashes the key to choose the shard, so all records with the same key go to the same shard and stay in order. Keys with many distinct values spread records evenly, whereas a few heavily used keys create a hot shard that throttles writes with ProvisionedThroughputExceededException while other shards are idle.

Each shard accepts up to 1 MB or 1,000 records per second of writes. For reads, standard consumers poll with GetRecords and share 2 MB per second per shard, so adding consumers divides the bandwidth among them. Enhanced fan-out consumers instead receive records pushed over a dedicated connection with their own 2 MB per second per shard and lower latency, at an extra cost. The following table summarises the limits.

Per shardLimit
Writes1 MB/s or 1,000 records/s
Reads (standard consumers)2 MB/s, shared by all consumers
Reads (enhanced fan-out)2 MB/s for each registered consumer

Manage shards yourself in provisioned mode, or let on-demand mode scale them automatically.

Example. Producers write 3,000 records/s of 2 KB (6 MB/s), read by three standard consumers. Take the largest requirement:

Write bandwidth:  6 MB/s / 1 MB/s per shard        = 6 shards
Write records:    3,000 / 1,000 records per shard  = 3 shards
Reads, 3 standard consumers: 3 × 6 MB/s = 18 MB/s / 2 MB/s = 9 shards
Reads with enhanced fan-out (2 MB/s per consumer per shard):  6 shards suffice

1MB/s

Writes per shard

1,000records/s

Writes per shard

2MB/s

Reads per shard, shared by standard consumers

Fill in the value

Shard capacity

Exam skills 1.1.6

Recall the capacity figures that size a Kinesis data stream.

Log in to try this activity Interactive activities are free with an account.

Partition keys and producers

Each record's partition key decides its shard. Ordering is guaranteed only within a shard, so records that must stay in order should share a key. A key with too few values creates a hot shard and ProvisionedThroughputExceededException; fix it with a more evenly distributed key, retries with backoff or more shards.

kinesis.put_record(StreamName="telemetry", Data=json.dumps(reading), PartitionKey=reading["deviceId"])

The example above writes a single record. The following list describes how records are written in larger volumes.

  • PutRecords accepts up to 500 records but can partly fail: check FailedRecordCount and retry only the failed records.
  • The Kinesis Producer Library adds batching and aggregation; the Kinesis Agent ships log files.

Retention and consumers

Records are kept for 24 hours by default, and up to 365 days. Unlike SQS, reading does not remove a record, so several applications can process and replay the same data. Each consumer tracks its own position in each shard, and a consumer can restart from an earlier position, for example to reprocess data after a bug fix.

Several kinds of consumer read streams. A Lambda event source mapping polls each shard and invokes a function with batches of records; settings such as the parallelisation factor, which processes up to ten batches per shard at once, the batching window and bisect on error control throughput and error handling. Applications built with the Kinesis Client Library (KCL) run on EC2 or containers; the library assigns shards to workers and records each worker's progress, called a checkpoint, in a DynamoDB table, so another worker can continue if one fails. Amazon Data Firehose reads a stream and delivers it to storage such as S3, and Amazon Managed Service for Apache Flink runs continuous analytics, such as rolling averages over time windows. The following table summarises the consumers.

ConsumerNotes
Lambda event source mappingSettings for parallelisation factor, batch window, bisect on error and on-failure destinations
Kinesis Client Library (KCL) appsCheckpoint their progress in DynamoDB
Amazon Data FirehoseLoads the stream into storage
Managed Service for Apache FlinkReal-time stream processing

The GetRecords.IteratorAgeMilliseconds metric shows how far consumers are lagging.

24hours

Default retention

365days

Maximum retention

Scenario questions

Keep a stream healthy

Exam skills 1.1.6 · 4.1.1

Practise fixing producer and consumer problems in Kinesis Data Streams.

Log in to try this activity Interactive activities are free with an account.
Worked example

A hot shard

A stream with 10 shards receives ProvisionedThroughputExceededException although total traffic is only 4 MB/s, well under the 10 MB/s the shards provide. The producer uses the device type ("phone", "tablet", "tv") as the partition key.

CauseFix
Only three distinct partition keys, so only three shards receive dataUse a high-cardinality key such as the device ID
90% of devices are phones, so one shard takes most trafficEven distribution across all 10 shards
Producers retry immediatelyRetry with backoff; batch with PutRecords

With device ID as the key, records spread across all shards, and ordering is still preserved for each device, which is all the application needs.

Check yourself

Several applications must read the same data, and one must reprocess the last two days after a bug fix. Why does Kinesis Data Streams suit this better than SQS?

Show answer

Reading a record does not delete it, so consumers read independently, and records are kept for up to 365 days, so a consumer can restart from an earlier position. SQS deletes a message once it is processed.

Exam Tips

Remember for the exam

  • A hot shard is fixed by choosing a more evenly distributed partition key.
  • When you need replay, or several consumers reading the same data, choose Kinesis rather than SQS.
  • When PutRecords partly fails, check FailedRecordCount and retry only the failed records.
  • A rising IteratorAgeMilliseconds means consumers are falling behind; add shards or parallelisation.