Skip to main content

Amazon Kinesis

Core concept: Kinesis handles real-time streaming data โ€” logs, metrics, IoT events, clickstreams โ€” with multiple consumers reading the same data independently.


๐Ÿ”ฐ What Is Kinesis?โ€‹

Kinesis is like a conveyor belt at an airport. Bags (data records) flow through in order, and multiple workers (consumers) can watch the same belt independently. Unlike SQS where a message goes to one consumer and is deleted, Kinesis keeps records for replay.


Kinesis Services Comparisonโ€‹

ServicePurposeRetentionManagementConsumers
Data StreamsReal-time processing1โ€“365 daysYou manage shardsCustom (Lambda, KCL, SDK)
Data FirehoseLoad to destinationsNo retention (buffer only)Fully managedS3, Redshift, OpenSearch, Splunk, HTTP
Data AnalyticsSQL/Flink on streamsN/AFully managedOutput to streams/destinations

Kinesis Data Streamsโ€‹

Shards (Capacity Units)โ€‹

MetricPer Shard
Write1 MB/s or 1,000 records/s
Read (standard)2 MB/s shared across all consumers
Read (enhanced fan-out)2 MB/s per consumer per shard

Example: 5 MB/s ingest โ†’ need at least 5 shards

Partition Keysโ€‹

Records with the same partition key โ†’ same shard โ†’ ordered within that shard:

KinesisClient kinesis = KinesisClient.create();

kinesis.putRecord(PutRecordRequest.builder()
.streamName("clickstream")
.data(SdkBytes.fromUtf8String("{\"userId\": \"U-123\", \"page\": \"/checkout\"}"))
.partitionKey("U-123") // All events for U-123 โ†’ same shard โ†’ ordered
.build());
Hot Shard

If one partition key has disproportionate traffic, it creates a hot shard. Use high-cardinality keys (userId, deviceId) not low-cardinality (country, status).

Consumer Typesโ€‹

TypeThroughputLatencyUse Case
Standard (GetRecords)2 MB/s shared per shard~200msCost-effective, fewer consumers
Enhanced Fan-Out (SubscribeToShard)2 MB/s dedicated per consumer per shard~70msMultiple consumers, low latency

Example: 3 consumers on standard = 2 MB/s รท 3 = ~667 KB/s each. With enhanced fan-out = 2 MB/s each.

Lambda ESM for Kinesisโ€‹

MyFunction:
Type: AWS::Serverless::Function
Properties:
Events:
KinesisEvent:
Type: Kinesis
Properties:
Stream: !GetAtt MyStream.Arn
StartingPosition: TRIM_HORIZON # From beginning of stream
BatchSize: 100
MaximumBatchingWindowInSeconds: 5
BisectBatchOnFunctionError: true # Split batch on error
MaximumRetryAttempts: 3
DestinationConfig:
OnFailure:
Destination: !GetAtt FailureSNS.Arn
ParallelizationFactor: 10 # Up to 10 Lambda per shard
TumblingWindowInSeconds: 60 # Aggregate over 1-min windows
FeatureDescription
StartingPositionTRIM_HORIZON (all records) or LATEST (new only)
BisectBatchOnFunctionErrorSplit failed batch in half to isolate bad record
ParallelizationFactorUp to 10 concurrent Lambdas per shard
TumblingWindowInSecondsStateful aggregation over time windows
MaximumRetryAttemptsRetry count before sending to failure destination

Kinesis Data Firehoseโ€‹

Data Sources โ†’ Firehose โ†’ (Optional Lambda Transform) โ†’ Destination
โ†“
S3, Redshift, OpenSearch, Splunk, HTTP
PropertyValue
Buffer size1โ€“128 MB
Buffer time60โ€“900 seconds
TransformOptional Lambda function
CompressionGZIP, Snappy, ZIP (for S3)
Format conversionJSON โ†’ Parquet/ORC (for S3/Athena)
Real-time?Near real-time (has buffer delay, minimum ~60s)

Kinesis vs SQS vs SNSโ€‹

FeatureKinesis Data StreamsSQSSNS
ModelStream (ordered log)Queue (point-to-point)Pub/Sub (fan-out)
ConsumersMultiple (same data)One per messageAll subscribers
Replayโœ… (up to 365 days)โŒโŒ
OrderingPer-shardFIFO queues onlyFIFO topics only
ProvisioningManual (shards)AutomaticAutomatic
Throughput1 MB/s per shard writeUnlimited (standard)Nearly unlimited
Use caseAnalytics, real-time, logsJob queues, decouplingAlerts, fan-out
Exam Keyword Triggers
  • "Multiple consumers reading same data" โ†’ Kinesis
  • "Replay data from the past" โ†’ Kinesis
  • "Load streaming data to S3/Redshift" โ†’ Firehose
  • "Job processing, decoupling" โ†’ SQS
  • "Fan-out to multiple services" โ†’ SNS + SQS
  • "Exactly-once, no replay" โ†’ SQS FIFO

Shard Operationsโ€‹

Splitting (Scale Up)โ€‹

Shard 1 (hash range 0-50) โ†’ Split โ†’ Shard 2 (0-25) + Shard 3 (26-50)

Merging (Scale Down)โ€‹

Shard 2 (0-25) + Shard 3 (26-50) โ†’ Merge โ†’ Shard 4 (0-50)
  • Shards have a parent-child relationship
  • Old (parent) shards remain until retention expires
  • On-Demand mode auto-scales shards (up to 200 MB/s write default)

๐Ÿ† Best Practicesโ€‹

  1. High-cardinality partition keys to avoid hot shards
  2. Enhanced Fan-Out for multiple consumers needing dedicated throughput
  3. Firehose instead of custom consumers for simple S3/Redshift loading
  4. BisectBatchOnFunctionError with Lambda to isolate poison pills
  5. On-Demand capacity mode to avoid manual shard management
  6. ParallelizationFactor for Lambda to increase per-shard parallelism

๐ŸŽฏ DVA-C02 Exam Tipsโ€‹

Kinesis Exam Cheat Sheet
  1. 1 shard = 1 MB/s write, 2 MB/s read
  2. Same partition key โ†’ same shard โ†’ ordered
  3. Enhanced Fan-Out = dedicated 2 MB/s per consumer
  4. Firehose = fully managed, near-real-time delivery to S3/Redshift
  5. Data Streams retain data; Firehose does not
  6. Replay = only Kinesis Data Streams (not SQS, not Firehose)
  7. BisectBatchOnFunctionError = split batch to find bad record
  8. ParallelizationFactor = up to 10 concurrent Lambdas per shard
  9. On-Demand auto-scales shards (no manual management)
  10. Retention: default 24h, max 365 days

๐Ÿงช Practice Questionsโ€‹

Q1. IoT platform: 5 MB/s sensor data, multiple analytics apps need same data, replay last 7 days. Best service?

A) SQS Standard
B) SQS FIFO
C) Kinesis Data Streams
D) SNS

โœ… Answer & Explanation

C โ€” Kinesis supports multiple consumers, data retention for replay, and ordered processing. Need โ‰ฅ5 shards for 5 MB/s.


Q2. Load clickstream data to S3 every 5 minutes. Least operational overhead?

A) Kinesis Data Streams + Lambda
B) Kinesis Data Firehose
C) SQS + Lambda
D) Kinesis Data Analytics

โœ… Answer & Explanation

B โ€” Firehose is fully managed, handles buffering and S3 delivery natively. No shards or consumers to manage.


Q3. Lambda processes Kinesis records. One poison-pill record causes the entire batch to fail. How to isolate it?

A) Increase batch size
B) Enable BisectBatchOnFunctionError
C) Use FIFO
D) Increase shard count

โœ… Answer & Explanation

B โ€” BisectBatchOnFunctionError splits the failed batch in half recursively until the bad record is isolated.


Q4. 3 consumers read from a 5-shard stream. Each consumer needs at least 2 MB/s per shard. What to enable?

A) Add more shards
B) Enhanced Fan-Out
C) Increase buffer size
D) ParallelizationFactor

โœ… Answer & Explanation

B โ€” Standard mode shares 2 MB/s across all consumers. Enhanced Fan-Out gives each consumer dedicated 2 MB/s per shard.


๐Ÿ”— Resourcesโ€‹