Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Amazon Kinesis (Real-Time Data Streaming)

Overview

Amazon Kinesis is a family of managed services for real-time data streaming at scale. It enables you to ingest, process, and analyze streaming data in real time — clickstreams, IoT telemetry, application logs, financial transactions, and more. Unlike batch processing, Kinesis processes data as it arrives, enabling sub-second insights.

Kinesis Family

graph TB
    subgraph "Kinesis Family"
        KS[Kinesis Data Streams<br/>Real-time data streaming]
        KF[Kinesis Data Firehose<br/>ETL + delivery to destinations]
        KA[Kinesis Data Analytics<br/>SQL-based stream processing]
        KV[Kinesis Video Streams<br/>Real-time video ingestion]
    end

    subgraph "Sources"
        App[Applications]
        IoT[IoT Devices]
        Log[Log Forwarders]
        DB[Database CDC]
    end

    subgraph "Destinations"
        S3[S3]
        Redshift[Redshift]
        OS[OpenSearch]
        Splunk[Splunk]
    end

    App --> KS
    IoT --> KS
    Log --> KF
    DB --> KF
    KS --> KA
    KA --> Redshift
    KS --> KF
    KF --> S3
    KF --> OS
    KF --> Splunk

Kinesis Data Streams

Core Architecture

graph TB
    Producers[Producers] -->|PutRecord/PutRecords| KS[Kinesis Data Stream]
    KS -->|GetRecords| C1[Consumer 1]
    KS -->|GetRecords| C2[Consumer 2]
    KS -->|GetRecords| C3[Consumer 3]

    subgraph "Stream Internals"
        KS --- Shard1[Shard 0]
        KS --- Shard2[Shard 1]
        KS --- ShardN[Shard N]
    end

Shards

A shard is the base throughput unit of a Kinesis stream:

PropertyValue
Write throughput1 MB/sec or 1,000 records/sec per shard
Read throughput2 MB/sec or 5 enhanced fan-out consumers per shard
Data retention1–365 days (configurable)
Max record size1 MB
Max put batch5 MB or 500 records

Partition key determines which shard a record lands on. Records with the same partition key are always read in order within that shard.

Data Record Structure

{
  "PartitionKey": "user-12345",
  "Data": "base64-encoded-payload",
  "SequenceNumber": "21269319989653737946712965403778482177",
  "ApproximateArrivalTimestamp": 1705312200.0
}

Consumer Types

TypePolling ModelLatencyThroughput Cost
StandardGetRecords polling (poll, 5 calls/sec/shard)~200msShared 2 MB/sec per shard
Enhanced Fan-OutPush-based via HTTP/2~70msDedicated 2 MB/sec per consumer

Resharding

You can split and merge shards to adjust capacity:

graph LR
    subgraph "Split (Increase Capacity)"
        S1[Shard 1] -->|split| S1a[Shard 1a: hash 0-49]
        S1 -->|split| S1b[Shard 1b: hash 50-99]
    end

    subgraph "Merge (Decrease Capacity)"
        S2a[Shard 2a: hash 0-49] -->|merge| S2[Shard 2: hash 0-99]
        S2b[Shard 2b: hash 50-99] -->|merge| S2
    end

Sequence Numbers and Ordering

Every record gets a monotonically increasing sequence number per shard. Consumers track their position using a checkpoint (sequence number of the last processed record). If a consumer fails, it resumes from the last checkpoint.

Kinesis Data Firehose

Firehose is a fully managed ETL service that delivers streaming data to destinations automatically:

graph LR
    Sources[Sources] --> Firehose[Delivery Stream]
    Firehose -->|Transform| Lambda[Inline Lambda]
    Lambda --> Buffer[Buffering<br/>1-900s, 1-128MB]
    Buffer --> S3[S3]
    Buffer --> RS[Redshift]
    Buffer --> OS[OpenSearch]
    Buffer --> Splunk[Splunk]
    Buffer --> HTTP[HTTP Endpoint]
FeatureDetails
BufferingTime (60–900s) or size (1–128 MB), whichever comes first
TransformationInline Lambda for data conversion, filtering, enrichment
Data format conversionJSON → Parquet, ORC; record aggregation
CompressionGzip, Snappy, ZIP
BackupCopy original records to S3 alongside transformed output

Firehose vs Direct Streams

ScenarioChoice
Simple delivery to S3/RedshiftFirehose (no code needed)
Custom processing logicData Streams + Lambda/KCL
Real-time analyticsData Streams + Kinesis Data Analytics
Multiple independent consumersData Streams (multiple consumer applications)

Kinesis Data Analytics

Process streaming data using standard SQL:

-- Create a pump from source stream
CREATE OR REPLACE STREAM "DESTINATION_SQL_STREAM" (
    "user_id" VARCHAR(16),
    "event_type" VARCHAR(32),
    "event_count" INTEGER
);

-- Sliding window aggregation
CREATE OR REPLACE PUMP "STREAM_PUMP" AS
INSERT INTO "DESTINATION_SQL_STREAM"
SELECT STREAM
    "user_id",
    "event_type",
    COUNT(*) AS "event_count"
FROM "SOURCE_SQL_STREAM_001"
GROUP BY
    "user_id",
    "event_type",
    WINDOW SESSION BETWEEN 10 SECOND PRECEDING AND CURRENT ROW;

Window Types

Window TypeDescriptionUse Case
TumblingFixed-size, non-overlappingHourly aggregations
SlidingFixed-size, overlappingMoving averages
SessionDynamic, activity-drivenUser sessions

Kinesis Client Library (KCL)

KCL simplifies consumer development by handling:

  • Shard distribution across workers
  • Checkpointing and fault tolerance
  • Load balancing when shards are added/removed
  • Coordination via a DynamoDB lease table
// KCL 2.x Worker
KinesisClient kinesisClient = KinesisClient.create();
DynamoDbClient dynamoClient = DynamoDbClient.create();
CloudWatchClient cwClient = CloudWatchClient.create();

Scheduler scheduler = new Scheduler.Builder()
    .recordProcessorFactory(new MyRecordProcessorFactory())
    .streamName("my-stream")
    .kinesisClient(kinesisClient)
    .dynamoDbClient(dynamoClient)
    .cloudWatchClient(cwClient)
    .build();

scheduler.start();

Comparison: Kinesis vs Kafka

FeatureKinesis Data StreamsApache Kafka
ManagementFully managedSelf-managed or Confluent Cloud
ScalingShard splitting/mergingPartition rebalancing
Retention1–365 daysConfigurable (disk-based)
Consumer modelPull (standard) / Push (EFO)Pull only
OrderingPer-partition keyPer-partition
Throughput1 MB/s write per shardHigher per broker
Cost modelPer shard-hour + PUTPer broker (self-hosted) or per CKU
EcosystemAWS-nativeRich open-source ecosystem
Exactly-onceKCL idempotent processingKafka Streams / Transactions
Multi-regionWith Kinesis Data Streams cross-region replicationMirrorMaker 2

Pricing

ComponentCost
Data Streams (shard-hour)$0.015/hour per shard ($~$11/mo)
Data Streams (PUT)$0.014 per million records
Enhanced Fan-Out$0.015/hour per consumer-shard + $0.014/million records
Firehose$0.029/hour per delivery stream + data volume pricing
Data AnalyticsStarting at $0.11/hour per KPU

Limits

ResourceDefault LimitAdjustable
Shards per account (us-east-1)500Yes (up to 50,000)
Records per PutRecords call500No
Max record data size1 MBNo
Max PutRecords payload5 MBNo
Retention period1–365 daysN/A (configurable)
GetRecords calls/sec/shard (standard)5No
Consumers with EFO per shard5Yes

Common Use Cases

Use CaseKinesis ServiceArchitecture
Clickstream analyticsStreams + AnalyticsReal-time funnel analysis
Log aggregationFirehoseDeliver to S3/OpenSearch
IoT telemetryStreamsLambda consumers for alerting
Real-time fraud detectionStreams + AnalyticsSliding window anomaly detection
ETL into data warehouseFirehoseTransform + deliver to Redshift
Application metricsStreamsTime-series aggregation

Interview Questions

  1. How do you choose the number of shards? Estimate peak throughput: shards = ceil(max(peak_write_MB/s, peak_records/s / 1000)). Plan for headroom. Remember read throughput is 2x write.

  2. What happens when a shard is split? Existing records remain on the parent shard. New records route to child shards based on the hash key range. Consumers see both parent and child shards during the transition.

  3. How does KCL handle failover? KCL uses a DynamoDB table to track lease ownership. If a worker dies, its lease expires (default 60s) and another worker claims it, resuming from the last checkpoint.

  4. When would you use Firehose over Streams? Use Firehose when you need simple, managed delivery to AWS destinations without custom processing. Use Streams when you need multiple consumers, custom processing, or sub-second latency.

  5. How do you achieve exactly-once processing? Kinesis provides at-least-once delivery. For exactly-once, use idempotent consumers (deduplication by sequence number) or Kinesis Data Analytics with checkpointing.

Key Takeaways

  • Kinesis Streams provides managed real-time data streaming with shard-based scaling
  • Firehose is the zero-code ETL pipeline for delivering streaming data to destinations
  • Kinesis Data Analytics enables SQL-based stream processing with windowing
  • KCL handles consumer coordination, checkpointing, and fault tolerance
  • Shard count directly determines cost and throughput — plan capacity carefully
  • Enhanced Fan-Out provides dedicated throughput per consumer at lower latency
  • Compare with Kafka: Kinesis for AWS-native simplicity, Kafka for ecosystem richness

Cross-References