AWS Learning
Messaging & Streaming

Amazon MSK (Managed Streaming for Apache Kafka)

Managed Streaming for Apache Kafka, Serverless, MSK Connect

Tổng quan

Amazon MSK là dịch vụ fully managed Apache Kafka cho phép build và run applications sử dụng Kafka để xử lý streaming data.

┌──────────────────────────────────────────────────────────────────────────────┐
│                         Amazon MSK là gì?                                    │
├──────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│   Apache Kafka = Open-source streaming platform                              │
│                  (pub/sub, message queue, real-time processing)              │
│                                                                              │
│   ┌─────────────────────────────────────────────────────────────────────┐    │
│   │  Self-managed Kafka trên EC2                                        │    │
│   │  ───────────────────────────                                        │    │
│   │  ❌ Phải quản lý cluster, brokers, ZooKeeper                        │    │
│   │  ❌ Phải lo patching, scaling, monitoring                           │    │
│   │  ❌ Availability & durability tự setup                              │    │
│   └─────────────────────────────────────────────────────────────────────┘    │
│                              ▼                                               │
│   ┌─────────────────────────────────────────────────────────────────────┐    │
│   │  Amazon MSK (Fully Managed)                                         │    │
│   │  ──────────────────────────                                         │    │
│   │  ✅ AWS quản lý brokers, ZooKeeper (hoặc KRaft mode)                │    │
│   │  ✅ Auto patching, monitoring tích hợp                              │    │
│   │  ✅ Multi-AZ high availability                                      │    │
│   │  ✅ Data replication tự động                                        │    │
│   └─────────────────────────────────────────────────────────────────────┘    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Lợi ích chính

Lợi íchMô tả
Fully managedAWS lo brokers, patching, monitoring
Compatible100% tương thích Apache Kafka APIs
Highly availableMulti-AZ replication
SecureVPC, encryption, IAM, TLS
ScalableScale brokers và storage dễ dàng

Kiến trúc

┌──────────────────────────────────────────────────────────────────────────────┐
│                           MSK Cluster Architecture                           │
├──────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│         PRODUCERS                           CONSUMERS                        │
│    ┌──────────────────┐                ┌──────────────────┐                  │
│    │  Application 1   │                │  Application A   │                  │
│    │  Application 2   │                │  Lambda          │                  │
│    │  IoT Devices     │                │  Kinesis Firehose│                  │
│    └────────┬─────────┘                └────────▲─────────┘                  │
│             │                                   │                            │
│             ▼                                   │                            │
│    ┌────────────────────────────────────────────┴──────────────────────┐     │
│    │                        MSK CLUSTER                                │     │
│    │                                                                   │     │
│    │   AZ-a              AZ-b              AZ-c                        │     │
│    │  ┌────────┐        ┌────────┐        ┌────────┐                    │    │
│    │  │Broker 1│        │Broker 2│        │Broker 3│                    │    │
│    │  │        │◄──────►│        │◄──────►│        │                    │    │
│    │  │ Topic  │        │ Topic  │        │ Topic  │                    │    │
│    │  │Replica │        │Replica │        │Replica │                    │    │
│    │  └────────┘        └────────┘        └────────┘                    │    │
│    │       │                 │                 │                        │    │
│    │  ┌────────┐        ┌────────┐        ┌────────┐                    │    │
│    │  │ZooKeeper│       │ZooKeeper│       │ZooKeeper│  (hoặc KRaft)     │    │
│    │  └────────┘        └────────┘        └────────┘                    │    │
│    │                                                                   │     │
│    └───────────────────────────────────────────────────────────────────┘     │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Data Flow

Producer ──publish──► Topic ──subscribe──► Consumer

Topic được chia thành PARTITIONS:
┌─────────────────────────────────────────────────────────────────┐
│  Topic: "orders"                                                │
│  ┌──────────────┐ ┌──────────────┐ ┌──────────────┐             │
│  │ Partition 0  │ │ Partition 1  │ │ Partition 2  │             │
│  │ [1][2][3][4] │ │ [1][2][3]    │ │ [1][2][3][4] │             │
│  │ Broker 1     │ │ Broker 2     │ │ Broker 3     │             │
│  └──────────────┘ └──────────────┘ └──────────────┘             │
│                                                                 │
│  • Messages ordered WITHIN partition                            │
│  • Consumer groups đọc song song từ partitions                  │
└─────────────────────────────────────────────────────────────────┘

Các thành phần chính

1. Brokers

Đặc điểmMô tả
Là gìKafka servers xử lý read/write
Số lượngTối thiểu 2, recommended 3+ (multi-AZ)
Instance typeskafka.m5.large → kafka.m5.24xlarge
StorageEBS volumes, auto-scaling available

2. Topics & Partitions

Thành phầnMô tả
TopicCategory/feed của messages
PartitionSubset của topic, distributed across brokers
ReplicationCopies of partitions (default: 3)
RetentionBao lâu giữ messages (default: 7 days, max: unlimited)

3. ZooKeeper vs KRaft

ModeMô tả
ZooKeeperLegacy, AWS quản lý ZooKeeper nodes riêng
KRaftMới, không cần ZooKeeper, metadata trong Kafka brokers

[!TIP] KRaft mode được recommend cho clusters mới - đơn giản hơn và performance tốt hơn.


MSK Serverless vs Provisioned

┌──────────────────────────────────────────────────────────────────────────────┐
│                    MSK Serverless vs MSK Provisioned                         │
├──────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│   MSK SERVERLESS                           MSK PROVISIONED                   │
│   ─────────────                            ───────────────                   │
│   ✅ Auto-scales                           ✅ Full control                   │
│   ✅ No capacity planning                  ✅ Custom broker configs          │
│   ✅ Pay per use                           ✅ Predictable pricing            │
│   ❌ Limited configs                       ❌ Manual scaling                 │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
Tiêu chíMSK ServerlessMSK Provisioned
Quản lý capacityTự độngManual
PricingPer partition-hour + dataPer broker-hour + storage
Max partitions120 per clusterUnlimited
Custom configsLimitedFull access
Use caseVariable/unpredictable workloadsSteady, predictable workloads

MSK Connect

MSK Connect cho phép stream data GIỮA MSK và các services khác mà không cần code.

┌──────────────────────────────────────────────────────────────────────────────┐
│                           MSK Connect                                        │
├──────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│   SOURCE CONNECTORS              MSK              SINK CONNECTORS            │
│   (Đưa data VÀO Kafka)          CLUSTER          (Lấy data RA từ Kafka)      │
│                                                                              │
│   ┌─────────────┐                                 ┌─────────────┐            │
│   │   S3        │──────►                   ──────►│   S3        │            │
│   │   RDS       │──────►  ┌─────────────┐  ──────►│   Redshift  │            │
│   │   DynamoDB  │──────►  │    MSK      │  ──────►│ OpenSearch  │            │
│   │   MongoDB   │──────►  │   Cluster   │  ──────►│   Splunk    │            │
│   │   PostgreSQL│──────►  └─────────────┘  ──────►│ Snowflake   │            │
│   └─────────────┘                                 └─────────────┘            │
│                                                                              │
│   Dùng Kafka Connect plugins (open-source hoặc commercial)                   │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Security

1. Network Security

LayerOptions
VPCMSK cluster trong private subnets
Security GroupsControl traffic vào brokers
Private connectivityVPC endpoints, PrivateLink

2. Encryption

TypeMô tả
At-restEBS encryption với KMS
In-transitTLS between clients và brokers
In-clusterTLS between brokers

3. Authentication & Authorization

MethodMô tả
IAMAWS IAM policies cho Kafka APIs
SASL/SCRAMUsername/password authentication
TLS mutual authClient certificates
ACLsKafka Access Control Lists

So sánh MSK vs Kinesis

┌──────────────────────────────────────────────────────────────────────────────┐
│                          MSK vs Kinesis Data Streams                         │
├──────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│   AMAZON MSK                               KINESIS DATA STREAMS              │
│   ──────────                               ────────────────────              │
│   Apache Kafka                             AWS Proprietary                   │
│   Open-source compatible                   AWS SDK only                      │
│                                                                              │
│   ┌─────────────────────────────┐         ┌─────────────────────────────┐    │
│   │  Kafka Ecosystem            │         │  AWS Ecosystem              │    │
│   │  • Kafka clients (any lang) │         │  • AWS SDK                  │    │
│   │  • Kafka Connect            │         │  • Kinesis Firehose         │    │
│   │  • Kafka Streams            │         │  • Kinesis Analytics        │    │
│   │  • Schema Registry          │         │  • Lambda integration       │    │
│   └─────────────────────────────┘         └─────────────────────────────┘    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘
Tiêu chíAmazon MSKKinesis Data Streams
ProtocolApache KafkaAWS proprietary
Message sizeDefault 1MB, configurableMax 1MB
RetentionUnlimitedMax 365 days
ReplicationConfigurable3 AZs (fixed)
Partition scalingManual/add brokersShard splitting (auto available)
OrderingPer partitionPer shard
Pricing modelPer broker + storagePer shard-hour + data
Serverless✅ MSK Serverless✅ On-demand mode
Migration from Kafka✅ Easy (same APIs)❌ Code changes needed

Khi nào dùng MSK vs Kinesis?

ScenarioChọn
Đã có Kafka on-premises, muốn migrateMSK
Team đã biết Kafka ecosystemMSK
Muốn AWS native, ít operational overheadKinesis
Cần Lambda trigger dễ dàngKinesis
Cần message retention vô hạnMSK
Cần Kafka Connect pluginsMSK

Pricing

MSK Provisioned

ComponentPricing
Broker hoursPer instance type per hour
StoragePer GB-month
Data transferStandard AWS rates

MSK Serverless

ComponentPricing
Cluster hoursPer hour cluster runs
Partition hoursPer partition per hour
Data in/outPer GB
StoragePer GB-month

Use Cases

Use CaseMô tả
Log aggregationCollect logs từ nhiều sources
Real-time analyticsProcess streaming data
Event-driven architectureDecouple microservices
Data pipelineETL, data lake ingestion
IoT data streamingSensor data processing
Clickstream analysisUser behavior tracking

Exam Tips

[!IMPORTANT] Các điểm quan trọng cho AWS Certification:

1. MSK vs Kinesis

  • Đã có Kafka → MSK (zero code changes)
  • AWS native → Kinesis
  • Unlimited retention → MSK

2. MSK Serverless vs Provisioned

  • Variable workload → Serverless
  • Predictable, high throughput → Provisioned
  • Max customization → Provisioned

3. Security

  • MSK trong VPC private subnets
  • TLS + KMS cho encryption
  • IAM auth cho managed authentication

4. Integration patterns

Common pattern: MSK → Lambda (via Event Source Mapping)
Common pattern: MSK → Kinesis Firehose → S3
Common pattern: MSK Connect → OpenSearch

5. Câu hỏi thường gặp

Câu hỏiĐáp án
Migrate Kafka to AWS?MSK (Kafka compatible)
Real-time + AWS native?Kinesis
Kafka Connect plugins?MSK Connect
Serverless Kafka?MSK Serverless

Tài liệu tham khảo