Amazon Kinesis Data Streams

Durable, replayable pipe for high-volume real-time records. Producers write into shards, consumers read from them independently, everything is kept for 1–365 days. Compared to SQS/SNS it's the tool that persists streaming data with ordering per shard, and lets many consumers read the same data at their own pace.

Ống dẫn record real-time bền, có thể replay. Producer ghi vào shard, consumer đọc độc lập, dữ liệu giữ 1–365 ngày. So với SQS/SNS, đây là công cụ lưu dữ liệu streaming có thứ tự trên từng shard, cho nhiều consumer đọc cùng data theo tốc độ riêng.

Reach for itNên dùng

  • High-volume real-time telemetry: click streams, IoT, app logs, metricsTelemetry real-time volume lớn: click stream, IoT, log app, metric
  • Multiple independent consumers on the same stream (analytics + alerting + archive)Nhiều consumer độc lập trên cùng stream (analytics + alerting + archive)
  • Need replay after a bug or a schema changeCần replay sau khi có bug hoặc đổi schema
  • Ordering matters within a key (user-id, device-id) — pick that as PartitionKeyCần ordering theo key (user-id, device-id) — chọn key đó làm PartitionKey

Not the right toolKhông hợp

  • Simple decoupling with per-message ack/retry → SQSDecouple đơn giản có ack/retry mỗi message → SQS
  • One event, many arbitrary subscribers (email, SMS…) → SNSMột event, nhiều subscriber tuỳ ý (email, SMS…) → SNS
  • "Just get this into S3/Redshift/OpenSearch, no code" → Firehose"Chỉ cần đổ vào S3/Redshift/OpenSearch, không code" → Firehose
  • Cross-account event bus with rules and targets → EventBridgeEvent bus cross-account có rule và target → EventBridge

Shards, partition keys, orderingShard, partition key, ordering

A stream is a bunch of shards in parallel. Each record's PartitionKey is hashed to pick which shard. Ordering only holds inside a shard — so records with the same key stay in order, across keys they don't. Stream = nhiều shard chạy song song. PartitionKey của record được hash để chọn shard. Ordering chỉ đảm bảo trong 1 shard — cùng key thì đúng thứ tự, khác key thì không.

  • Per-shard limits: 1 MB/s or 1,000 records/s in · 2 MB/s out (classic, shared across all consumers of that shard).Giới hạn mỗi shard: 1 MB/s hoặc 1,000 record/s vào · 2 MB/s ra (classic, chia cho tất cả consumer của shard đó).
  • Throughput scales linearly with shard count: 2 shards = 2 MB/s in / 4 MB/s out, 5 shards = 5 MB/s in / 10 MB/s out, N shards = N MB/s in / 2N MB/s out. That's the whole capacity model in provisioned mode.Throughput scale tuyến tính theo số shard: 2 shard = 2 MB/s vào / 4 MB/s ra, 5 shard = 5 MB/s vào / 10 MB/s ra, N shard = N MB/s vào / 2N MB/s ra. Đó là toàn bộ mô hình capacity ở chế độ provisioned.
  • Hot shard = one PartitionKey gets a disproportionate share of traffic. Fix by choosing a higher-cardinality key, salting, or using on-demand mode.Hot shard = 1 PartitionKey ăn quá nhiều traffic. Fix bằng chọn key cardinality cao hơn, thêm salt, hoặc dùng on-demand.
  • Scale by splitting a shard (its throughput becomes two child shards, so overall in/out doubles for that key range) or merging two adjacent shards (halves the cost of that range). Old shards go closed — consumers must drain them before deleting the last checkpoint.Scale bằng split shard (throughput chia thành 2 shard con, tổng vào/ra của range đó gấp đôi) hoặc merge 2 shard liền kề (chi phí range đó giảm nửa). Shard cũ thành closed — consumer phải đọc hết trước khi xoá checkpoint cuối.
  • A PutRecords batch is up to 500 records / 5 MB; single PutRecord is one round-trip and one billed unit — batch when you can.Batch PutRecords tối đa 500 record / 5 MB; PutRecord đơn = 1 round-trip và 1 đơn vị tính phí — nên batch.

Provisioned vs on-demandProvisioned vs on-demand

Provisioned On-demand
You doBạn làm Pick shard count, split/merge to scaleChọn số shard, split/merge để scale Nothing — AWS scales itKhông gì — AWS lo scale
Start throughputThroughput khởi điểm 1 MB/s in · 2 MB/s out per shardmỗi shard 4 MB/s in · 4,000 rps baseline; scales to 2× the last 30-day peakbaseline; tự scale lên 2× đỉnh 30 ngày qua
BillingTính phí Per shard-hour + PUT payload unitsMỗi shard-giờ + đơn vị PUT payload Per stream-hour + GB in/outMỗi stream-giờ + GB vào/ra
Best forHợp với Predictable, steady workloads — cheaper at scaleWorkload đều, dự đoán được — rẻ hơn khi lớn Spiky/unknown traffic, "just make it work"Traffic spiky/không đoán, "chỉ cần chạy được"
Switching modesĐổi mode You can flip a stream between provisioned and on-demand — but only twice per 24 hours. Practical pattern: start on-demand, observe, then move to provisioned once traffic is predictable. Có thể đổi stream giữa provisioned và on-demand — nhưng chỉ 2 lần trong 24 giờ. Pattern thực tế: bắt đầu on-demand, quan sát, rồi chuyển sang provisioned khi traffic đã ổn định.

ProducersProducer

Anything that writes: SDK apps, the KPL (Java/C++ batching + retry + partial-failure handling), the Kinesis Agent (log tailer), CloudWatch Logs subscriptions, IoT rules. All eventually call PutRecord(s). Gì cũng ghi được: app SDK, KPL (Java/C++ có batching + retry + xử lý partial-failure), Kinesis Agent (tail log), CloudWatch Logs subscription, IoT rule. Cuối cùng đều gọi PutRecord(s).

  • KPL aggregates many user records into fewer Kinesis records to hit the 1 MB payload cap and cut per-record cost. Uses Cloud native aggregation — a matching KCL consumer (or the KPL library on the reader) is needed to un-aggregate.KPL gộp nhiều user record thành ít Kinesis record hơn để tận dụng 1 MB payload và giảm chi phí. Dùng Cloud native aggregation — consumer KCL (hoặc thư viện KPL bên reader) mới giải nén được.
  • Kinesis Agent = Java daemon that tails files (nginx, syslog, app logs) and pushes to KDS or KDF. Handles rotation, checkpoints, retries.Kinesis Agent = daemon Java tail file (nginx, syslog, log app) và đẩy lên KDS hoặc KDF. Tự xử lý rotate, checkpoint, retry.
  • ProvisionedThroughputExceededException = you hit the shard limit. Retry with backoff, add shards, spread partition keys, or move to on-demand.ProvisionedThroughputExceededException = quá giới hạn shard. Retry có backoff, thêm shard, phân tán partition key, hoặc chuyển on-demand.

ConsumersConsumer

Every consumer reads independently and keeps its own checkpoint. Two read models: classic (pull, shared 2 MB/s) and enhanced fan-out (push, 2 MB/s per consumer). Mỗi consumer đọc độc lập và giữ checkpoint riêng. Hai model đọc: classic (pull, chia 2 MB/s) và enhanced fan-out (push, 2 MB/s mỗi consumer).

Classic (shared)Classic (shared) Enhanced fan-outEnhanced fan-out
Throughput per shardThroughput mỗi shard 2 MB/s shared across all consumerschia cho mọi consumer 2 MB/s per consumer (no sharing)(không chia)
ModelModel Pull (GetRecords, 5 tps per shard)Pull (GetRecords, 5 lệnh/s mỗi shard) Push (SubscribeToShard, HTTP/2)Push (SubscribeToShard, HTTP/2)
LatencyLatency ~200 ms ~70 ms
Cap on consumersGiới hạn consumer Recommended ≤ 5 before they starve each otherKhuyến nghị ≤ 5 trước khi cạnh tranh nhau 20 registered consumers per stream
CostChi phí Included in shard-hour / on-demandTính trong shard-giờ / on-demand Extra: per consumer-shard-hour + per GB retrievedThêm: mỗi consumer-shard-giờ + mỗi GB đọc
  • Lambda ESM reads one shard = one concurrent invocation. Scale by adding shards or turning on parallelization factor (up to 10 batches per shard in parallel; breaks strict per-shard ordering).Lambda ESM: 1 shard = 1 invocation đồng thời. Scale bằng thêm shard hoặc bật parallelization factor (tối đa 10 batch/shard chạy song song; phá ordering chặt trên shard).
  • The KCL library handles shard leases, load balancing across worker EC2 instances, and stores checkpoints in a DynamoDB table (one row per shard). Deleting that table = full replay from stream start.Thư viện KCL lo lease shard, cân tải giữa worker EC2, và lưu checkpoint vào bảng DynamoDB (1 dòng/shard). Xoá bảng đó = replay từ đầu stream.
  • Iterator types: TRIM_HORIZON (oldest), LATEST (only new), AT_TIMESTAMP, AT/AFTER_SEQUENCE_NUMBER. Pick AT_TIMESTAMP for a point-in-time replay.Loại iterator: TRIM_HORIZON (cũ nhất), LATEST (chỉ mới), AT_TIMESTAMP, AT/AFTER_SEQUENCE_NUMBER. Chọn AT_TIMESTAMP để replay từ một mốc thời gian.

Delivery to other servicesGiao dữ liệu ra service khác

KDS itself only stores. To land data in S3/Redshift/OpenSearch you either pair it with Firehose (no code) or write a Lambda/Flink consumer. To transform in-flight, put Managed Apache Flink between the stream and the sink. KDS chỉ lưu. Muốn đổ vào S3/Redshift/OpenSearch thì ghép với Firehose (không cần code) hoặc viết consumer Lambda/Flink. Muốn transform giữa đường, chèn Managed Apache Flink giữa stream và đích.

  • Firehose reads KDS; the reverse is not true — Firehose is delivery-only, no consumer API.Firehose đọc được KDS; ngược lại không — Firehose chỉ deliver, không có API consumer.
  • Managed Flink reads KDS/MSK only. It does not read from Firehose (same reason).Managed Flink chỉ đọc KDS/MSK. Không đọc từ Firehose (cùng lý do).
  • SNS can subscribe an SQS queue but not a Kinesis stream directly — go via SNS → Firehose or SNS → Lambda → KDS.SNS subscribe được queue SQS nhưng không subscribe thẳng vào Kinesis stream — đi qua SNS → Firehose hoặc SNS → Lambda → KDS.

SecurityBảo mật

  • At-rest KMS, in-flight HTTPS/TLS. Encryption is applied by KDS itself; producers and consumers get plaintext.At-rest KMS, in-flight HTTPS/TLS. Mã hoá do KDS làm; producer/consumer thấy plaintext.
  • IAM policies on the stream resource for producer/consumer access. Cross-account works via resource-based policies (added 2020) or by assuming a role in the target account.IAM policy trên resource stream cho producer/consumer. Cross-account qua resource policy (thêm 2020) hoặc assume role sang account đích.
  • VPC endpoints (Interface) = keep producer/consumer traffic on private network, no NAT gateway.VPC endpoint (Interface) = giữ traffic producer/consumer trong mạng private, không cần NAT.