0Pricing
AWS Solutions Architect · Lesson

Kinesis Data Streams for Real-Time Event Processing

Produce and consume high-throughput event streams with Kinesis Data Streams, manage shards for throughput, and use Lambda as a consumer.

Kinesis Data Streams for Real-Time Event Processing is a free AWS Solutions Architect lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Kinesis Data Streams Core Concepts

Kinesis Data Streams (KDS) is a durable, ordered, real-time data streaming service. Data is organised into a stream composed of one or more shards. Each shard is an ordered sequence of data records. Producers put records onto shards using a partition key that determines which shard receives the record. Consumers read records from shards, processing them in the order they arrived within each shard.

Shard Capacity and Throughput Limits

Each shard supports 1 MB/s or 1,000 records/s write throughput and 2 MB/s read throughput (shared among all standard consumers on that shard). Total stream capacity scales linearly with shard count. Use the formula: shards_needed = max(write_MB_per_s / 1, read_MB_per_s / 2). If producers hit the write limit, ProvisionedThroughputExceededException errors appear — resolve by splitting shards or distributing partition keys more evenly.

# Calculate shards needed for a stream:
# - Ingest rate: 5 MB/s writes
# - Read rate: 3 consumers x 2 MB/s = 6 MB/s reads
# shards = max(5/1, 6/2) = max(5, 3) = 5 shards needed

aws kinesis create-stream \
  --stream-name iot-telemetry \
  --shard-count 5

Partition Keys and Data Distribution

The partition key is a string that Kinesis hashes (MD5) to determine which shard receives a record. A well-chosen partition key distributes records evenly across shards (hot shard prevention). For IoT: use device ID. For clickstreams: use session ID or user ID. Avoid low-cardinality keys (e.g., country name with only 5 values) as they cause hot shards where one shard receives disproportionate write traffic while others sit idle.

import boto3, json

client = boto3.client('kinesis', region_name='us-east-1')

# Good: use device_id as partition key for even distribution
event = {'deviceId': 'sensor-42', 'temp': 23.5, 'ts': '2024-01-15T10:00:00Z'}
client.put_record(
    StreamName='iot-telemetry',
    Data=json.dumps(event),
    PartitionKey='sensor-42'  # high-cardinality -> even distribution
)

Standard Consumers vs Enhanced Fan-Out

Standard consumers share the 2 MB/s read throughput per shard using GetRecords with polling. If you have 3 consumers on a shard each needs 2 MB/s, they will be throttled sharing 2 MB/s total. Enhanced Fan-Out (EFO) gives each registered consumer its own dedicated 2 MB/s read pipe via a persistent HTTP/2 push connection (SubscribeToShard). EFO adds a per-consumer-shard-hour cost but eliminates read contention entirely.

# Register an Enhanced Fan-Out consumer
aws kinesis register-stream-consumer \
  --stream-arn arn:aws:kinesis:us-east-1:123456789012:stream/iot-telemetry \
  --consumer-name real-time-analytics

# List registered consumers
aws kinesis list-stream-consumers \
  --stream-arn arn:aws:kinesis:us-east-1:123456789012:stream/iot-telemetry

Lambda as a Kinesis Consumer

Lambda integrates natively with Kinesis Data Streams via an Event Source Mapping. Lambda polls the stream, reads batches of records, and invokes your function. Configure BatchSize (1–10,000 records), StartingPosition (TRIM_HORIZON for oldest, LATEST for newest), and BisectBatchOnFunctionError to split failed batches. Parallelisation Factor (1–10) lets Lambda launch multiple concurrent invocations per shard to keep up with fast-moving streams.

# Create a Lambda event source mapping for Kinesis
aws lambda create-event-source-mapping \
  --function-name ProcessIoTEvents \
  --event-source-arn arn:aws:kinesis:us-east-1:123456789012:stream/iot-telemetry \
  --batch-size 100 \
  --starting-position LATEST \
  --parallelization-factor 5 \
  --bisect-batch-on-function-error true \
  --destination-config '{
    "OnFailure": {
      "Destination": "arn:aws:sqs:us-east-1:123456789012:kinesis-dlq"
    }
  }'

Kinesis Client Library (KCL)

The Kinesis Client Library (KCL) is an application framework for building robust Java (or multi-language via MultiLangDaemon) Kinesis consumers. KCL handles shard enumeration, lease management (distributing shards among worker instances), checkpointing progress to DynamoDB, and graceful shard splits/merges. Each KCL worker processes one or more shards, and KCL automatically rebalances shards as workers scale out or fail. KCL is preferred over raw SDK polling for production consumer applications.

# KCL stores checkpoints in a DynamoDB table automatically
# Each shard has one row tracking the last successfully processed sequence number
# KCL lease table structure:
# leaseKey (shardId) | checkpoint (sequenceNumber) | leaseOwner (workerId)
#
# To start a KCL application (pseudocode):
# KinesisClientLibConfiguration config = new KinesisClientLibConfiguration(
#   'iot-app', 'iot-telemetry', credentialsProvider, 'worker-1');
# Worker worker = new Worker.Builder().config(config).recordProcessorFactory(factory).build();
# worker.run();

Data Retention and Replay

Kinesis Data Streams stores records for 24 hours by default (extendable to 7 days or up to 365 days with Long-Term Retention at additional cost). Unlike SQS, consumed records are not deleted after consumption — they remain available until retention expires. This enables multiple consumers to read the same records independently, and allows replay by resetting a consumer's checkpoint to an earlier sequence number — invaluable for bug fixes or backfilling new services.

# Extend stream retention to 7 days
aws kinesis increase-stream-retention-period \
  --stream-name iot-telemetry \
  --retention-period-hours 168

# Get records from the oldest available record (replay)
SHARD_ITERATOR=$(aws kinesis get-shard-iterator \
  --stream-name iot-telemetry \
  --shard-id shardId-000000000000 \
  --shard-iterator-type TRIM_HORIZON \
  --query 'ShardIterator' --output text)

aws kinesis get-records --shard-iterator $SHARD_ITERATOR --limit 100

On-Demand Capacity Mode

Kinesis Data Streams supports two capacity modes. Provisioned mode: you manage the number of shards manually and pay per shard-hour. On-Demand mode: Kinesis automatically scales shard capacity based on incoming throughput (up to 200 MB/s write and 400 MB/s read by default) and you pay per GB of data written and retrieved. On-demand mode is ideal for variable or unpredictable traffic patterns where you do not want to manage shard scaling.

# Switch an existing stream to On-Demand mode
aws kinesis update-stream-mode \
  --stream-arn arn:aws:kinesis:us-east-1:123456789012:stream/iot-telemetry \
  --stream-mode-details StreamMode=ON_DEMAND

Ordering Guarantees Within Shards

Kinesis guarantees ordering within a shard — records with the same partition key always go to the same shard and are read in the order they were written. However, there is no ordering guarantee across shards. If your application requires global ordering across all records, use a single shard (limiting throughput to 1 MB/s) or redesign so ordering is only needed within a partition key group (e.g., per-device ordering). This is a common exam distinction versus SQS FIFO which provides strict deduplication and ordering.

Security: Encryption and VPC

Kinesis Data Streams encrypts records server-side at rest using AWS KMS (CMK or AWS-managed key) when server-side encryption is enabled. All data in transit is encrypted with TLS. For applications running in a VPC that should not send stream data over the public internet, use a VPC Interface Endpoint (PrivateLink) for Kinesis so traffic stays entirely within the AWS network backbone — important for regulated workloads.

# Enable server-side encryption on a Kinesis stream
aws kinesis start-stream-encryption \
  --stream-name iot-telemetry \
  --encryption-type KMS \
  --key-id arn:aws:kms:us-east-1:123456789012:key/mrk-abc123

Kinesis Data Streams vs SQS vs Kafka

For the SAA-C03 exam, compare Kinesis Data Streams with alternatives. Kinesis vs SQS: Kinesis preserves order within a shard and supports multiple consumers reading the same data; SQS deletes messages after consumption. Kinesis vs MSK (Kafka): MSK is managed Apache Kafka — use it when you need Kafka protocol compatibility, advanced topic configurations, or are migrating from on-premises Kafka. Use Kinesis for AWS-native streaming with tighter integration to Lambda, Firehose, and Flink. Choose Kinesis unless the question specifically mentions Kafka or Kafka compatibility requirements.

Quick Check

Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.

Lesson Recap

In this lesson you learned: Kinesis Data Streams provides sharded, ordered, durable streams with 24-hour default retention and replay capability, Enhanced Fan-Out gives each consumer dedicated 2 MB/s per shard eliminating read contention, and On-Demand mode auto-scales shards for unpredictable traffic. Next up we explore the choreography versus orchestration patterns in event-driven architecture.

Frequently asked questions

Is the “Kinesis Data Streams for Real-Time Event Processing” lesson free?

Yes — the full text of “Kinesis Data Streams for Real-Time Event Processing” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.

What will I learn in “Kinesis Data Streams for Real-Time Event Processing”?

Produce and consume high-throughput event streams with Kinesis Data Streams, manage shards for throughput, and use Lambda as a consumer. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AWS Solutions Architect?

No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Kinesis Data Streams for Real-Time Event Processing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AWS Solutions Architect lesson?

Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. EventBridge: Event Bus and Rules
  2. Step Functions: Orchestrating Serverless Workflows
  3. Kinesis Data Streams for Real-Time Event Processing
  4. Choreography vs Orchestration Patterns
← Back to AWS Solutions Architect