Lesson 1 of 4 Amazon Kinesis and Streaming Services

Capturing Streaming Data with Kinesis Data Streams

This lesson explains how Kinesis Data Streams captures and stores streaming data in real time for custom processing.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe shard capacity.
  • Size a stream from its write requirements.
  • Identify producers and consumers, including enhanced fan-out.
  • Choose between Kinesis and SQS.

Shards

A Kinesis data stream is made of shards, and each shard has a fixed capacity. It accepts up to 1 MB or 1,000 records per second of writes. Standard consumers share 2 MB per second of reads from each shard, so adding consumers divides the bandwidth among them; consumers registered for enhanced fan-out each receive their own 2 MB per second, pushed to them with lower latency. A producer that exceeds a shard's write limit is throttled, and a consumer that cannot keep up falls behind. The following table summarises the limits.

Per shardLimit
Ingest1 MB/s or 1,000 records/s
Read (shared)2 MB/s across consumers
Read (enhanced fan-out)2 MB/s per consumer

Records with the same partition key go to the same shard in order.

The following diagram shows records distributed across three shards and read by two kinds of consumer.

How a Kinesis data stream uses shards. The partition key of each record decides its shard, and records with the same key stay in order on that shard. Each shard accepts up to 1 MB or 1,000 records per second. Standard consumers share 2 MB per second of reads per shard, while each enhanced fan-out consumer receives its own 2 MB per second.
The shard count is illustrative. A stream's capacity is the per-shard limit multiplied by the number of shards.

1MB/s

Writes per shard

1,000records/s

Writes per shard

2MB/s

Reads per shard, shared by standard consumers

Sizing a stream

In provisioned mode, the number of shards must cover the larger of the two write requirements. A stream receiving 5 MB/s at 3,000 records/s needs 5 shards for the data and 3 for the records, so 5 shards plus headroom.

Kinesis offers two capacity modes. In on-demand mode, the stream adds and removes capacity automatically and is billed per gigabyte, which suits unpredictable traffic and avoids sizing altogether. In provisioned mode, you choose the number of shards, pay per shard per hour, and reshard by splitting or merging shards as traffic changes, which is cheaper for steady traffic. The following table summarises the two modes.

ModeScalingBilling
On-demandAutomaticPer GB
ProvisionedYou choose shards and reshardPer shard

Retention: 24 hours default, up to 365 days, so consumers can replay.

Fill in the value

Sizing shards

Exam skills 3.5.2 · 3.5.6

Work out the number of shards a stream needs.

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

Producers and consumers

Producers write records to a Kinesis data stream in several ways: with the AWS SDK or the Kinesis Producer Library (KPL), which batches records for efficiency; with the Kinesis Agent, which sends log files from servers; from AWS IoT; and from CloudWatch Logs subscription filters.

Consumers read the stream independently of one another, each at its own pace and position. They include Lambda functions, applications built with the Kinesis Client Library (KCL) on EC2 or ECS, Amazon Managed Service for Apache Flink applications and Amazon Data Firehose. Because records stay in the stream for the retention period, a new consumer can be added later and read past data.

Kinesis or SQS

Kinesis Data Streams and SQS both sit between producers and consumers, but they model data differently. A Kinesis stream is an ordered log: records stay in the stream for the retention period after they are read, so several applications can each read every record, in order per shard, and any of them can replay earlier records. It suits real-time dashboards, clickstreams, log processing and custom processing within a second.

An SQS queue holds independent tasks: each message is processed by one consumer and then deleted, and consumers scale out simply by adding more of them. It suits decoupling components and distributing work. The following table summarises the differences.

Kinesis Data Streams
ModelOrdered stream read by several apps, replayable
Use forReal-time dashboards, clickstreams, logs, sub-second custom processing
SQS
ModelIndependent tasks processed once and deleted
Use forDecoupling and work queues
Sort into groups

Kinesis or SQS?

Exam skills 3.5.2 · 2.1.3

Decide which service fits each requirement.

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

Sizing a stream

IoT devices send 4,000 records per second of 1 KB each (4 MB/s). Two applications read the full stream.

Write capacity per shard: 1 MB/s or 1,000 records/s
  By size:    4 MB/s / 1 MB/s           = 4 shards
  By records: 4,000 / 1,000             = 4 shards
Read capacity per shard: 2 MB/s shared by standard consumers
  Two consumers read 4 MB/s each = 8 MB/s  -> 8 MB/s / 2 MB/s = 4 shards
Answer: 4 shards (or on-demand mode); with enhanced fan-out each consumer gets its own 2 MB/s per shard

If traffic were unpredictable, on-demand capacity mode would remove the calculation entirely. Using the device ID as the partition key spreads load evenly and keeps each device's records in order.

Check yourself

How are records kept in order in Kinesis Data Streams?

Show answer

Records with the same partition key go to the same shard, where they are kept in order.

Exam Tips

Remember for the exam

  • For real-time processing with several consumers and replay, use Kinesis Data Streams.
  • For a hot shard, choose a better partition key or add shards.
  • For unknown throughput without managing shards, use on-demand mode.
  • When several applications must read the same data and replay it, use Kinesis rather than SQS.
  • The number of shards needed for ingestion is the larger of the MB/s rate and the records per second divided by 1,000.