AWS Learning
Messaging & Streaming

Amazon Kinesis

Data Streams, Firehose, Analytics, Real-time Streaming

Tổng quan

Amazon Kinesis là platform để thu thập, xử lý và phân tích real-time streaming data ở bất kỳ quy mô nào.

┌─────────────────────────────────────────────────────────────────┐
│                    KINESIS FAMILY                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────┐  ┌─────────────────┐                      │
│   │ Kinesis Data    │  │ Kinesis Data    │                      │
│   │ Streams         │  │ Firehose        │                      │
│   │ (Real-time)     │  │ (Near real-time)│                      │
│   └─────────────────┘  └─────────────────┘                      │
│                                                                 │
│   ┌─────────────────┐  ┌─────────────────┐                      │
│   │ Kinesis Data    │  │ Kinesis Video   │                      │
│   │ Analytics       │  │ Streams         │                      │
│   │ (SQL on stream) │  │ (Video streams) │                      │
│   └─────────────────┘  └─────────────────┘                      │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Kinesis Services Overview

ServiceMục đíchLatencyUse Case
Data StreamsIngest & store streaming dataReal-time (~200ms)Custom processing
Data FirehoseLoad data to destinationsNear real-time (60s+)ETL to S3/Redshift
Data AnalyticsSQL queries on streamsReal-timeAnalytics, dashboards
Video StreamsStream video for MLReal-timeVideo analytics

  1. Kinesis Data Streams
  2. Kinesis Data Firehose
  3. Kinesis Data Analytics
  4. So sánh Kinesis vs SQS vs Kafka
  5. Best Practices

1. Kinesis Data Streams

1.1 Kiến trúc tổng quan

┌───────────────────────────────────────────────────────────────────┐
│               KINESIS DATA STREAMS ARCHITECTURE                   │
├───────────────────────────────────────────────────────────────────┤
│                                                                   │
│   Producers                                                       │
│   ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐                     │
│   │  App   │ │  IoT   │ │ Logs   │ │ Clicks │                     │
│   └────┬───┘ └────┬───┘ └────┬───┘ └────┬───┘                     │
│        │          │          │            │                       │
│        └──────────┴────┬─────┴──────────┘                         │
│                        ↓                                          │
│   ┌─────────────────────────────────────────────────────────┐     │
│   │              KINESIS DATA STREAM                        │     │
│   │  ┌─────────────────────────────────────────────────┐       │  │
│   │  │ Shard 1: [rec1] [rec4] [rec7] [rec10]           │       │  │
│   │  │ Shard 2: [rec2] [rec5] [rec8] [rec11]           │       │  │
│   │  │ Shard 3: [rec3] [rec6] [rec9] [rec12]           │       │  │
│   │  └─────────────────────────────────────────────────┘       │  │
│   │                                                         │     │
│   │  Data Retention: 1 day (default) - 365 days             │     │
│   └─────────────────────────────────────────────────────────┘     │
│                          │                                        │
│        ┌───────────────┼───────────────┐                          │
│        ↓               ↓               ↓                          │
│   ┌────────┐      ┌────────┐      ┌────────┐                      │
│   │ Lambda │      │  EC2   │      │Kinesis │                      │
│   │        │      │  App   │      │Analytics │                    │
│   └────────┘      └────────┘      └────────┘                      │
│   Consumers (có thể đọc lại - replay!)                            │
│                                                                   │
└───────────────────────────────────────────────────────────────────┘

1.2 Shard - Đơn vị cơ bản

┌─────────────────────────────────────────────────────────────────┐
│                         SHARD                                   │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Mỗi Shard có capacity cố định:                                │
│                                                                 │
│   WRITE (Ingestion):                                            │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  1 MB/second  OR  1000 records/second                   │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   READ (Consumption):                                           │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Standard: 2 MB/second (shared across consumers)        │   │
│   │  Enhanced Fan-out: 2 MB/second PER consumer             │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   Billing: Pay per shard hour + data transfer                   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Ví dụ tính Shard:

  • Bạn cần ingest 5 MB/s → Cần ít nhất 5 shards
  • Bạn có 10 consumers với enhanced fan-out → Mỗi consumer nhận full 2 MB/s

1.3 Partition Key & Ordering

┌──────────────────────────────────────────────────────────────────┐
│                    PARTITION KEY                                 │
├──────────────────────────────────────────────────────────────────┤
│                                                                  │
│   Producer gửi record với Partition Key:                         │
│                                                                  │
│   Record { PartitionKey: "user-123", Data: "..." }               │
│                      ↓                                           │
│   Hash(PartitionKey) → Shard mapping                             │
│                      ↓                                           │
│   ┌─────────────────────────────────────────────────────────┐    │
│   │ Shard 1: user-123, user-456  (same key → same shard)    │    │
│   │ Shard 2: user-789, user-012                             │    │
│   │ Shard 3: user-345, user-678                             │    │
│   └─────────────────────────────────────────────────────────┘    │
│                                                                  │
│   ✅ Records với CÙNG partition key → CÙNG shard → ORDERING      │
│   ⚠️  Hot partition: quá nhiều records với cùng key              │
│                                                                  │
└──────────────────────────────────────────────────────────────────┘

1.4 Producers

Producer TypeMô tảUse Case
AWS SDKPutRecord, PutRecords APICustom apps
Kinesis Producer Library (KPL)Batching, compression, retryHigh throughput
Kinesis AgentPre-built agent for logsLog files to Kinesis
CloudWatch LogsDirect integrationLog streaming
AWS IoTIoT Rule actionIoT devices

Kinesis Producer Library (KPL):

┌─────────────────────────────────────────────────────────────────┐
│                    KPL FEATURES                                 │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                    Producer App                         │   │
│   │  ┌─────────────────────────────────────────────────┐     │  │
│   │  │ Record 1 ─┐                                     │     │  │
│   │  │ Record 2 ─┼──→ Batch → Compress → Send          │     │  │
│   │  │ Record 3 ─┘         ↓                           │     │  │
│   │  │                 Retry on failure                │     │  │
│   │  │                 Async & non-blocking            │     │  │
│   │  └─────────────────────────────────────────────────┘     │  │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   Batching: Aggregate user records → fewer API calls            │
│   Compression: Optional compression                             │
│   Retry: Automatic retry with backoff                           │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

1.5 Consumers

┌─────────────────────────────────────────────────────────────────┐
│                    CONSUMER TYPES                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   1. SHARED CONSUMER (Classic):                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Shard ←──── Poll ──── Consumer 1                       │   │
│   │  (2 MB/s)←── Poll ──── Consumer 2                       │   │
│   │        ←── Poll ──── Consumer 3                         │   │
│   │                                                         │   │
│   │  → 2 MB/s SHARED giữa tất cả consumers                  │   │
│   │  → GetRecords API (pull model)                          │   │
│   │  → Max 5 GetRecords calls/second per shard              │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   2. ENHANCED FAN-OUT (EFO):                                    │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Shard ────→ Push ────→ Consumer 1 (2 MB/s)             │   │
│   │        ────→ Push ────→ Consumer 2 (2 MB/s)             │   │
│   │        ────→ Push ────→ Consumer 3 (2 MB/s)             │   │
│   │                                                         │   │
│   │  → Mỗi consumer nhận RIÊNG 2 MB/s                       │   │
│   │  → SubscribeToShard API (push model via HTTP/2)         │   │
│   │  → ~70ms latency (vs ~200ms classic)                    │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

So sánh Shared vs Enhanced Fan-out:

FeatureShared (Classic)Enhanced Fan-out
Throughput2 MB/s shared2 MB/s per consumer
ModelPull (GetRecords)Push (SubscribeToShard)
Latency~200 ms~70 ms
CostLowerHigher
Max ConsumersLimited by throughputUp to 20 per stream

1.6 Kinesis Client Library (KCL)

┌─────────────────────────────────────────────────────────────────┐
│                    KCL ARCHITECTURE                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                  Kinesis Stream                         │   │
│   │  [Shard 1] [Shard 2] [Shard 3] [Shard 4]                │   │
│   └─────────────────────────────────────────────────────────┘   │
│         │          │          │          │                      │
│         ↓          ↓          ↓          ↓                      │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              KCL Application (2 instances)              │   │
│   │  ┌──────────────────┐  ┌──────────────────┐              │  │
│   │  │   Worker 1       │  │   Worker 2       │              │  │
│   │  │  Shard 1, 2      │  │  Shard 3, 4      │              │  │
│   │  └──────────────────┘  └──────────────────┘              │  │
│   └─────────────────────────────────────────────────────────┘   │
│                        │                                        │
│                        ↓                                        │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                  DynamoDB                               │   │
│   │  (Checkpoint tracking - which records processed)        │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   Rules:                                                        │
│   • 1 shard = max 1 KCL worker có thể read                      │
│   • 4 shards, 4 workers → 1:1 mapping                           │
│   • 4 shards, 2 workers → mỗi worker đọc 2 shards               │
│   • 4 shards, 6 workers → 2 workers sẽ idle!                    │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

KCL Features:

  • Checkpointing: Track vị trí đã xử lý (lưu trong DynamoDB)
  • Load Balancing: Tự động phân chia shards cho workers
  • Fault Tolerance: Worker fail → KCL reassign shards
  • Shard Splitting/Merging: Tự handle khi stream scale

1.7 Capacity Modes

┌─────────────────────────────────────────────────────────────────┐
│                    CAPACITY MODES                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   PROVISIONED MODE:                                             │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  • Bạn chọn số shards                                   │   │
│   │  • Manual scaling (UpdateShardCount API)                │   │
│   │  • Pay per shard per hour (~$0.015/shard/hour)          │   │
│   │  • Predictable cost                                     │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ON-DEMAND MODE:                                               │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  • Auto scales based on throughput                      │   │
│   │  • No capacity planning                                 │   │
│   │  • Pay per GB in/out                                    │   │
│   │  • Default: 4 MB/s write, tự scale lên 200 MB/s write   │   │
│   │  • Đắt hơn ~2-3x so với provisioned                     │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

1.8 Data Retention & Replay

┌─────────────────────────────────────────────────────────────────┐
│                 DATA RETENTION & REPLAY                         │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Retention Period:                                             │
│   • Default: 24 hours                                           │
│   • Maximum: 365 days                                           │
│                                                                 │
│   Replay Capability:                                            │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Timeline: ──────────────────────────────────────────── │   │
│   │            │ Hour 1 │ Hour 2 │ Hour 3 │ Hour 4 │ Now     │  │
│   │                                                         │   │
│   │  Consumer 1: Reading Hour 4 (current)                   │   │
│   │  Consumer 2: Replaying from Hour 1 (reprocess all)      │   │
│   │                                                         │   │
│   │  → Same stream, different consumers, different positions│   │
│   │  → Mỗi consumer có riêng checkpoint                     │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   Iterator Types:                                               │
│   • TRIM_HORIZON: Đọc từ oldest record                          │
│   • LATEST: Chỉ đọc new records                                 │
│   • AT_TIMESTAMP: Đọc từ một thời điểm cụ thể                   │
│   • AT_SEQUENCE_NUMBER: Đọc từ một sequence cụ thể              │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

2. Kinesis Data Firehose

2.1 Tổng quan

┌─────────────────────────────────────────────────────────────────┐
│              KINESIS DATA FIREHOSE                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Fully managed, auto-scaling, NO administration                │
│                                                                 │
│   Sources               Firehose                 Destinations   │
│   ┌────────┐         ┌─────────────┐           ┌────────────┐   │
│   │Producer│────────→│             │──────────→│    S3      │   │
│   │  SDK   │         │  Transform  │           │ Redshift   │   │
│   └────────┘         │  (Lambda)   │           │ OpenSearch │   │
│   ┌────────┐         │             │           │ Splunk      │  │
│   │Kinesis │────────→│  Buffer     │           │ HTTP        │  │
│   │Streams │         │  (size/time)│           │ Endpoint    │  │
│   └────────┘         └─────────────┘           └─────────────┘  │
│   ┌────────┐                │                                   │
│   │  IoT   │────────────────┘                                   │
│   └────────┘                                                    │
│                                                                 │
│   Near Real-time: Min 60 seconds buffer (or 1MB)                │
│   → KHÔNG real-time như Data Streams!                           │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

2.2 Firehose Destinations

DestinationMô tả
Amazon S3Most common, any format
Amazon RedshiftVia S3 COPY command
Amazon OpenSearchLog analytics
SplunkSIEM integration
HTTP EndpointCustom destinations
3rd PartyDatadog, New Relic, MongoDB...

2.3 Data Transformation

┌─────────────────────────────────────────────────────────────────┐
│              FIREHOSE TRANSFORMATION                            │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌────────────┐    ┌────────────┐    ┌────────────┐            │
│   │   Source   │───→│   Lambda   │───→│   S3       │            │
│   │   Records  │    │ Transform  │    │ (output)   │            │
│   └────────────┘    └────────────┘    └────────────┘            │
│                           │                                     │
│                           ↓                                     │
│                    ┌────────────┐                               │
│                    │ S3 Backup  │                               │
│                    │ (original) │                               │
│                    └────────────┘                               │
│                                                                 │
│   Lambda can:                                                   │
│   • Convert formats (CSV → JSON)                                │
│   • Enrich data (add metadata)                                  │
│   • Filter records                                              │
│   • Compress data                                               │
│                                                                 │
│   Built-in conversions:                                         │
│   • JSON → Parquet/ORC (for Athena/Redshift)                    │
│   • GZIP, Snappy compression                                    │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

2.4 Buffering

SettingMinMaxDefault
Buffer Size1 MB128 MB5 MB
Buffer Interval60 sec900 sec300 sec

Firehose delivers khi một trong hai điều kiện đạt (size OR time)

2.5 Data Streams vs Firehose

┌─────────────────────────────────────────────────────────────────┐
│           DATA STREAMS vs FIREHOSE                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   DATA STREAMS:                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  • Real-time (~200ms latency)                           │   │
│   │  • Custom consumers (Lambda, EC2, KCL)                  │   │
│   │  • Data replay ✅                                       │   │
│   │  • Manage scaling (shards)                              │   │
│   │  • Multiple consumers on same data                      │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   FIREHOSE:                                                     │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  • Near real-time (60s+ latency)                        │   │
│   │  • Managed destinations (S3, Redshift, OpenSearch)      │   │
│   │  • NO replay (data delivered then gone)                 │   │
│   │  • Auto scaling                                         │   │
│   │  • Data transformation (Lambda, format conversion)      │   │
│   │  • Serverless, no administration                        │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
FeatureData StreamsFirehose
LatencyReal-time (~200ms)Near real-time (60s+)
ScalingManual (shards)Automatic
ProcessingCustom codeBuilt-in + Lambda
Replay✅ Yes❌ No
DestinationsAny (custom)AWS services, HTTP
AdministrationManage shardsServerless

3. Kinesis Data Analytics

3.1 Tổng quan

┌─────────────────────────────────────────────────────────────────┐
│              KINESIS DATA ANALYTICS                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Real-time analytics using SQL or Apache Flink                 │
│                                                                 │
│   ┌────────────┐    ┌────────────────┐    ┌────────────┐        │
│   │  Kinesis   │───→│                │───→│  Kinesis   │        │
│   │  Streams   │    │ SQL / Flink    │    │  Streams   │        │
│   └────────────┘    │                │    └────────────┘        │
│   ┌────────────┐    │ Process &      │    ┌────────────┐        │
│   │  Firehose  │───→│ Analyze        │───→│  Firehose  │        │
│   └────────────┘    │                │    └────────────┘        │
│                     └────────────────┘    ┌────────────┐        │
│                                           │   Lambda    │       │
│                                           └────────────┘        │
│                                                                 │
│   Use Cases:                                                    │
│   • Real-time dashboards                                        │
│   • Anomaly detection                                           │
│   • Streaming ETL                                               │
│   • Real-time metrics                                           │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

3.2 Two Flavors

SQL ApplicationApache Flink
LanguageSQLJava, Scala, Python
ComplexitySimple queriesComplex processing
Use CaseQuick analyticsProduction apps
Learning CurveLowHigher

4. So sánh Kinesis vs SQS vs Kafka

4.1 Queue vs Streaming - Khác biệt cốt lõi

┌─────────────────────────────────────────────────────────────────┐
│                        QUEUE (SQS)                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Producer → [A] [B] [C] [D] [E] → Consumer                     │
│                     ↓                                           │
│              Sau khi xử lý xong                                 │
│                     ↓                                           │
│              MESSAGE BỊ XÓA ❌                                  │
│                                                                 │
│   Giống như: HỘP THƯ                                            │
│   • Bạn nhận thư, đọc xong, VỨT ĐI                              │
│   • Người khác không thể đọc lại thư đó                         │
│   • 1 thư = 1 người đọc                                         │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────┐
│                     STREAMING (Kinesis/Kafka)                   │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Producer → [A] [B] [C] [D] [E] ← Consumer 1 đọc               │
│                                 ← Consumer 2 đọc                │
│                                 ← Consumer 3 đọc                │
│                     ↓                                           │
│              CÁC MESSAGE VẪN CÒN ĐÓ ✅                          │
│              (Lưu 1-365 ngày)                                   │
│                                                                 │
│   Giống như: YOUTUBE VIDEO / BĂNG GHI ÂM                        │
│   • Bạn xem video, video VẪN CÒN ĐÓ                             │
│   • Người khác có thể xem lại                                   │
│   • Bạn có thể tua lại xem từ đầu                               │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.2 Kinesis vs Kafka - So sánh chi tiết

┌─────────────────────────────────────────────────────────────────┐
│                KINESIS vs KAFKA ARCHITECTURE                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   KAFKA:                                                        │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Topic "orders"                                         │   │
│   │  ┌───────────────────────────────────────────────────┐  │   │
│   │  │ Partition 0: [A] [D] [G]                          │  │   │
│   │  │ Partition 1: [B] [E] [H]                          │  │   │
│   │  │ Partition 2: [C] [F] [I]                          │  │   │
│   │  └───────────────────────────────────────────────────┘  │   │
│   │           ↓                          ↓                  │   │
│   │  ┌───────────────────┐        ┌─────────────────┐       │   │
│   │  │Consumer Group A   │        │Consumer Group B │       │   │
│   │  │C1→P0, C2→P1, C3→P2│        │ C1→P0,P1,P2     │       │   │
│   │  └───────────────────┘        └─────────────────┘       │   │
│   │                                                         │   │
│   │  → Có Consumer Groups (native)                          │   │
│   │  → Trong 1 group: mỗi msg → 1 consumer                  │   │
│   │  → Nhiều groups: mỗi group nhận TẤT CẢ messages         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   KINESIS:                                                      │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Stream "orders"                                        │   │
│   │  ┌───────────────────────────────────────────────────┐  │   │
│   │  │ Shard 1: [A] [D] [G]                              │  │   │
│   │  │ Shard 2: [B] [E] [H]                              │  │   │
│   │  │ Shard 3: [C] [F] [I]                              │  │   │
│   │  └───────────────────────────────────────────────────┘  │   │
│   │           ↓                          ↓                  │   │
│   │  ┌─────────────────┐        ┌──────────────────┐        │   │
│   │  │KCL App A        │        │KCL App B         │        │   │
│   │  │(reads all shards)        │(reads all shards)│        │   │
│   │  └─────────────────┘        └──────────────────┘        │   │
│   │                                                         │   │
│   │  → KHÔNG có Consumer Groups (native)                    │   │
│   │  → Dùng Enhanced Fan-out hoặc KCL để làm tương tự       │   │
│   │  → Mỗi KCL app = tương đương 1 consumer group           │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.3 Consumer Group - Giải thích chi tiết

┌─────────────────────────────────────────────────────────────────┐
│              KAFKA CONSUMER GROUP DETAIL                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Topic "orders" với 3 partitions                               │
│                                                                 │
│   SCENARIO 1: 1 Consumer Group với 3 consumers                  │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Group "order-service"                                  │   │
│   │  ┌────────┐  ┌────────┐  ┌────────┐                      │  │
│   │  │  C1    │  │  C2    │  │  C3    │                      │  │
│   │  │  P0    │  │  P1    │  │  P2    │                      │  │
│   │  │ [A][D] │  │ [B][E] │  │ [C][F] │                      │  │
│   │  └────────┘  └────────┘  └────────┘                      │  │
│   │                                                         │   │
│   │  → Mỗi consumer nhận 1 phần messages (load balancing)   │   │
│   │  → GIỐNG SQS behavior trong 1 group!                    │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   SCENARIO 2: 2 Consumer Groups                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Group "order-service"        Group "analytics"         │   │
│   │  ┌────────────────────┐      ┌────────────────────┐      │  │
│   │  │ C1(P0) C2(P1) C3(P2)│      │     C1(ALL)        │     │  │
│   │  └────────────────────┘      └────────────────────┘      │  │
│   │  Nhận: A,B,C,D,E,F            Nhận: A,B,C,D,E,F         │   │
│   │  (chia cho 3 consumers)       (1 consumer nhận all)     │   │
│   │                                                         │   │
│   │  → CẢ HAI groups đều nhận TẤT CẢ messages!              │   │
│   │  → Khác SQS: SQS chỉ có 1 "group"                       │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.4 Kinesis tương đương Kafka Consumer Groups

┌──────────────────────────────────────────────────────────────────┐
│           KINESIS: EMULATING CONSUMER GROUPS                     │
├──────────────────────────────────────────────────────────────────┤
│                                                                  │
│   OPTION 1: Multiple KCL Applications                            │
│   ┌─────────────────────────────────────────────────────────┐    │
│   │  Stream "orders"                                        │    │
│   │         ↓                     ↓                         │    │
│   │  ┌─────────────────┐   ┌─────────────────┐                │  │
│   │  │KCL App A        │   │KCL App B        │                │  │
│   │  │"order-service"  │   │"analytics"      │                │  │
│   │  │DynamoDB Table A │   │DynamoDB Table B │                │  │
│   │  └─────────────────┘   └─────────────────┘                │  │
│   │                                                         │    │
│   │  → Mỗi KCL app có riêng checkpoint table                │    │
│   │  → Cả 2 apps đều đọc TẤT CẢ data                        │    │
│   │  → Giống Kafka 2 consumer groups!                       │    │
│   │  ⚠️ Shared throughput (2 MB/s per shard)                │    │
│   └─────────────────────────────────────────────────────────┘    │
│                                                                  │
│   OPTION 2: Enhanced Fan-out                                     │
│   ┌─────────────────────────────────────────────────────────┐    │
│   │  Stream "orders"                                        │    │
│   │         ↓ (push)          ↓ (push)                      │    │
│   │  ┌─────────────────┐   ┌─────────────────┐                │  │
│   │  │Consumer A       │   │Consumer B       │                │  │
│   │  │2 MB/s dedicated │   │2 MB/s dedicated │                │  │
│   │  └─────────────────┘   └─────────────────┘                │  │
│   │                                                         │    │
│   │  → Mỗi consumer nhận RIÊNG 2 MB/s                       │    │
│   │  → Up to 20 consumers per stream                        │    │
│   │  → Giống Kafka consumer groups hơn!                     │    │
│   └─────────────────────────────────────────────────────────┘    │
│                                                                  │
└──────────────────────────────────────────────────────────────────┘

4.5 Bảng so sánh Kinesis vs Kafka

FeatureKinesisKafka (MSK)
Managed✅ Fully serverless⚠️ Semi-managed (broker management)
Consumer GroupsVia KCL apps / Enhanced Fan-out✅ Native
PartitioningShards (manual/on-demand)Partitions
Retention1-365 daysUnlimited (disk-based)
Throughput per shard/partition1 MB/s write, 2 MB/s readDepends on broker config
Message Size1 MB max1 MB default (configurable)
ProtocolAWS SDK / HTTPKafka Protocol
EcosystemAWS integrationsKafka Connect, Schema Registry
Cost ModelPer shard hour + dataPer broker hour
ScalingShard split/mergeAdd partitions
Replay✅ Via iterator✅ Via offset
OrderingPer shardPer partition
Exactly-once❌ At-least-once✅ Transactions
Multi-Region❌ Single region✅ MirrorMaker

4.6 So sánh với SQS

FeatureKinesis/KafkaSQS
Message sau xử lýVẪN CÒNBỊ XÓA
Replay✅ Yes❌ No
Consumer Groups✅ Yes❌ No (1 "group" only)
OrderingPer shard/partitionFIFO queues only
Use caseEvent streaming, analyticsTask queue, job processing
ScalingProvision shardsUnlimited
Latency~200ms~10ms

4.7 Khi nào dùng gì?

┌─────────────────────────────────────────────────────────────────┐
│                    DECISION TREE                                │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Cần message replay?                                           │
│        │                                                        │
│   ┌────┴────┐                                                   │
│   ↓         ↓                                                   │
│  YES       NO                                                   │
│   │         │                                                   │
│   ↓         ↓                                                   │
│ Kinesis/   Cần real-time processing?                            │
│ Kafka          │                                                │
│           ┌────┴────┐                                           │
│           ↓         ↓                                           │
│          YES       NO                                           │
│           │         │                                           │
│           ↓         ↓                                           │
│       Kinesis     SQS                                           │
│       Streams  (simple queue)                                   │
│                                                                 │
│                                                                 │
│   Đang dùng Kafka on-prem? Cần Kafka ecosystem?                 │
│        │                                                        │
│   ┌────┴────┐                                                   │
│   ↓         ↓                                                   │
│  YES       NO                                                   │
│   │         │                                                   │
│   ↓         ↓                                                   │
│ Amazon    Kinesis                                               │
│  MSK      (AWS native, simpler)                                 │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
ScenarioRecommendation
New AWS project, need streamingKinesis
Migrate from Kafka on-premAmazon MSK
Need Kafka Connect ecosystemAmazon MSK
Simple task queueSQS
Fan-out to multiple servicesSNS + SQS
Real-time analyticsKinesis
Need exactly-once semanticsKafka/MSK

5. Best Practices

5.1 Kinesis Data Streams

PracticeRecommendation
Partition KeyUse high-cardinality keys to avoid hot shards
Shard CountStart with estimate, monitor CloudWatch
Enhanced Fan-outUse when > 2-3 consumers or need low latency
KPLUse for high-throughput producers (batching)
KCLUse for processing with checkpointing
RetentionIncrease if need replay capability

5.2 Hot Shard Prevention

┌─────────────────────────────────────────────────────────────────┐
│                  AVOID HOT SHARDS                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ❌ BAD: Low cardinality partition key                         │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  PartitionKey = "device-type"                           │   │
│   │  Values: "mobile", "web", "tablet" (only 3!)            │   │
│   │                                                         │   │
│   │  → 80% traffic might go to "mobile" shard               │   │
│   │  → Hot shard, throttling                                │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ✅ GOOD: High cardinality partition key                       │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  PartitionKey = "user-id" or "device-id"                │   │
│   │  Values: millions of unique values                      │   │
│   │                                                         │   │
│   │  → Even distribution across shards                      │   │
│   │  → Ordering maintained per user                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

5.3 Firehose

PracticeRecommendation
Buffer SizeLarger = fewer files, better for analytics
Buffer IntervalSmaller = more real-time
CompressionEnable GZIP for cost savings
FormatConvert to Parquet for Athena queries
BackupEnable source backup to S3

6. Common Exam Questions

Q1: Kinesis Data Streams vs Firehose?

A: Streams = real-time, custom processing, replay. Firehose = near real-time, managed delivery to S3/Redshift, no replay.

Q2: How many shards needed for 6 MB/s ingestion?

A: 6 shards (each shard = 1 MB/s write)

Q3: Shard iterator expired error?

A: Consumer too slow, record moved past retention. Increase retention or speed up processing.

Q4: Enhanced Fan-out vs Shared consumer?

A: Enhanced = dedicated 2 MB/s per consumer, push model, lower latency. Shared = 2 MB/s divided, pull model.

Q5: Data retention default/max?

A: Default: 24 hours. Max: 365 days.

Q6: Firehose minimum latency?

A: 60 seconds (buffer time minimum)


Tài liệu tham khảo