NetworkingSection 118 min read8 questions

Cloud Integration

SQS, SNS, Kinesis & Amazon MQ

Cloud Integration คือกลุ่มบริการที่ช่วยให้ applications สื่อสารกันได้แบบ decoupled — ไม่ต้องรอกัน, scale แยกกัน, และไม่พังพร้อมกัน ครอบคลุม SQS (queue), SNS (pub/sub), Kinesis (streaming), Amazon MQ (traditional protocols) ซึ่งเป็นพื้นฐานของ event-driven architecture บน AWS

ในหน้านี้9 sections
  1. 01Section Introduction
  2. 02Synchronous vs Asynchronous Communication
  3. 03Amazon SQS (Simple Queue Service)
  4. 04Amazon SNS (Simple Notification Service)
  5. 05SNS vs SQS Comparison
  6. 06Amazon Kinesis
  7. 07Amazon MQ
  8. 08When to Use Which?
  9. 09Summary
01

Section Introduction

ใน cloud applications ที่มีหลาย services การสื่อสารระหว่างกันสามารถทำได้ 2 รูปแบบหลัก — แต่ละแบบมีข้อดีข้อเสียและเหมาะกับสถานการณ์ต่างกัน

  • Synchronous communication: app-to-app โดยตรง (ผู้ส่งต้องรอผู้รับตอบ)
  • Asynchronous / Event-based communication: สื่อสารผ่านตัวกลาง (queue, topic, stream) — ทั้งสองฝั่งทำงานอิสระกัน

ทางออก: Decouple ระบบด้วย integration services — ทำให้แต่ละส่วน scale ได้อย่างอิสระ และไม่พึ่งพากันโดยตรง

SQS — Queue Model

Decouple producer-consumer ผ่าน queue ผู้ส่งทิ้ง message ไว้ ผู้รับมาดึงตอนพร้อม ไม่ต้องรอกัน

SNS — Pub/Sub Model

Publisher ส่งครั้งเดียว Subscribers ได้รับทุกคน เหมาะกับ notification และ fan-out

Kinesis — Streaming Model

Real-time data streaming สำหรับ big data, analytics, และ IoT scale ได้สูงมาก

02

Synchronous vs Asynchronous Communication

เข้าใจความแตกต่างระหว่าง sync และ async เป็นพื้นฐานสำคัญในการเลือก integration pattern ที่เหมาะสม

Synchronous Communication
App A  ──── HTTP request ────►  App B
       ◄─── HTTP response ────

  - App A ต้อง "รอ" จนกว่า App B จะตอบกลับ
  - ถ้า App B ช้า → App A ก็ช้าตาม
  - ถ้า App B ล่ม → App A error ทันที
  - Traffic spike → App B รับไม่ไหว → ทั้งระบบพัง
Asynchronous / Event-based Communication
App A  ──► [SQS Queue / SNS Topic / Kinesis] ──► App B
                       ▲ ตัวกลาง (buffer)

  - App A ส่งแล้วจบ ไม่ต้องรอ App B
  - App B ค่อย consume เมื่อพร้อม (rate ของตัวเอง)
  - App B ล่ม → message รออยู่ใน queue ไม่หาย
  - Traffic spike → queue ดูดซับไว้ → App B ไม่พัง
  • Sync เหมาะกับ: งานที่ต้องการคำตอบทันที เช่น login, payment confirmation, query data
  • Async เหมาะกับ: งานที่ไม่ต้องรอผล เช่น send email, process order, generate report, log analytics
  • Decoupling = Resilience: services ไม่พึ่งพากันโดยตรง → ตัวหนึ่งล่ม ตัวอื่นยังทำงานได้
  • Independent scaling: producer และ consumer scale ได้คนละ rate
03

Amazon SQS (Simple Queue Service)

Amazon SQS เป็น service ที่เก่าแก่ที่สุดของ AWS (เปิดตัวกว่า 10 ปีแล้ว) เป็น fully managed message queue (~serverless) ใช้ decouple applications ระหว่างกัน

  • Scale อัตโนมัติ: 1 message/วินาที → 10,000+ messages/วินาที
  • Default retention: 4 วัน, maximum: 14 วัน
  • ไม่จำกัดจำนวน messages ใน queue
  • Messages ถูก ลบหลัง consumer อ่านสำเร็จ (consumer ต้องเรียก delete)
  • Low latency: < 10ms ในการส่งและรับ message
  • Message size สูงสุด 256 KB ต่อ message
  • Consumers สามารถ แชร์งานกัน (horizontal scaling — เพิ่ม consumer = เพิ่ม throughput)
SQS Architecture
Producer 1 ──┐
Producer 2 ──┼──► [ SQS Queue ] ──► Consumer 1 (process & delete)
Producer 3 ──┘                  ──► Consumer 2 (process & delete)
                                ──► Consumer 3 (process & delete)

  - Producers ส่ง message เข้า queue
  - Consumers แต่ละตัวแย่งกันดึง message ไป process
  - Consumer หลายตัว = scale horizontally

SQS Standard Queue

Unlimited throughput, best-effort ordering, at-least-once delivery (อาจมี message ซ้ำ) — เหมาะกับ high volume ทั่วไป

SQS FIFO Queue

First-In-First-Out ordering, exactly-once delivery (ไม่ซ้ำ) แต่ throughput ต่ำกว่า (300-3,000 msg/s) — เหมาะกับงานที่ลำดับสำคัญ

Visibility Timeout: เมื่อ consumer อ่าน message — message จะ invisible สำหรับ consumer ตัวอื่นชั่วคราว (default 30 วินาที) เพื่อป้องกันการ process ซ้ำ ถ้า consumer ทำเสร็จก็ delete ถ้าไม่เสร็จในเวลา message กลับมา visible อีกครั้ง

Dead Letter Queue (DLQ): ถ้า message ถูก process ล้มเหลวซ้ำๆ (เกิน maxReceiveCount) → จะถูกย้ายไปยัง DLQ เพื่อให้ developer ตรวจสอบและ debug — แทนที่จะ retry ไปเรื่อยๆ และ block queue

04

Amazon SNS (Simple Notification Service)

Amazon SNS ใช้ Pub/Sub model — event publishers ส่ง message ไปที่ 1 Topic เดียว แล้ว subscribers ทุกตัวที่ subscribe topic นั้นจะได้รับ message ทุกอัน

  • Up to 10,000,000 subscriptions ต่อ 1 topic
  • Limit 100,000 topics ต่อ AWS account
  • Push-based — SNS push ไปหา subscribers ทันทีที่มี message ใหม่
  • ไม่มี message retention — ถ้า subscriber รับไม่ทัน message หาย (ยกเว้น HTTP retry)

HTTP / HTTPS

ส่ง HTTP POST ไปยัง URL ที่กำหนด มี delivery retries อัตโนมัติถ้า endpoint ล่ม (configurable retry policy)

Email

ส่ง notification เป็น email ตรงเข้า inbox เหมาะสำหรับ alert admin/team

SMS

ส่ง SMS ไปยังเบอร์โทรศัพท์ เหมาะกับ urgent alerts (รองรับหลายประเทศ)

Mobile Notifications

Push notification ไปยัง iOS, Android, Kindle Fire ผ่าน APNS, FCM, ADM

SQS Queues

ส่งเข้า SQS queue หลายตัวพร้อมกัน — ใช้ใน Fan-out pattern

Lambda Functions

Trigger Lambda function ทุกครั้งที่มี message — process notification แบบ serverless

Fan-out Pattern (SNS + SQS)
Publisher ──► SNS Topic ──┬──► SQS Queue A ──► Service A
                          ├──► SQS Queue B ──► Service B
                          ├──► SQS Queue C ──► Service C
                          └──► Lambda Function ──► Service D

  - Publisher ส่งครั้งเดียว → ทุก subscriber ได้รับ
  - แต่ละ consumer process อิสระกัน
  - เพิ่ม subscriber ใหม่ได้โดยไม่แก้ publisher
  - SQS ใน fan-out ทำหน้าที่ buffer + persistence

SNS Filtering: Subscriber แต่ละตัวสามารถกำหนด filter policy (JSON) เพื่อรับเฉพาะ messages ที่ตรงตามเงื่อนไข — ลด load และ cost (ไม่ต้อง process message ที่ไม่เกี่ยวข้อง)

05

SNS vs SQS Comparison

SNS และ SQS มักถูกถามให้เปรียบเทียบในข้อสอบ — เข้าใจความแตกต่างเพื่อเลือกใช้ถูกตัว:

SNS — Pub/Sub Model

Push ไปหา subscribers หลายตัวพร้อมกัน — one message → many receivers (broadcast). ไม่มี retention (data loss ถ้า subscriber ล่ม) รองรับสูงสุด 10M subscribers/topic หลายแบบ (Email, SMS, HTTP, Lambda, SQS)

SQS — Message Queue

Pull-based: consumers แย่งกันดึง message — one message → one consumer (compete). Retention 4-14 วัน (data persist) มี DLQ สำหรับ failed messages เหมาะกับ decoupling + buffering

เปรียบเทียบรวม
Feature              | SNS                      | SQS
─────────────────────|──────────────────────────|─────────────────────────
Model                | Pub/Sub (push)           | Queue (pull)
Distribution         | One-to-many (broadcast)  | One-to-one (compete)
Retention            | ไม่เก็บ (no retention)     | 4 วัน default, 14 วัน max
Subscribers / Consumers | 10M subscribers/topic | unlimited consumers
DLQ                  | Yes (ผ่าน SQS)            | Yes (built-in)
Use Case             | Notifications, fan-out   | Decoupling, buffering
06

Amazon Kinesis

Amazon Kinesis เป็น managed service สำหรับ real-time big data streaming — เก็บ, process, วิเคราะห์ streaming data ที่ scale ได้ระดับ big data

  • Real-time ไม่ใช่ batch — process data ทันทีที่เข้ามา
  • Scale ได้ระดับ หลายแสน data sources พร้อมกัน
  • เหมาะสำหรับ application logs, metrics, IoT, clickstreams, social media data
  • เก็บข้อมูลใน stream ได้ 1-365 วัน สำหรับ replay และ multiple consumers

Kinesis Data Streams

Low-latency ingestion ที่ scale รับ data จากหลายแสน sources พร้อมกัน — เป็นตัวหลักของ Kinesis ใช้สำหรับ real-time data pipeline

Kinesis Data Firehose

Load streaming data ไปเก็บที่ S3, Redshift, ElasticSearch, Splunk — fully managed, ไม่ต้องเขียน code, near real-time (~60s buffer)

Kinesis Data Analytics

วิเคราะห์ streaming data แบบ real-time ด้วย SQL queries — ไม่ต้อง manage infrastructure

Kinesis Video Streams

Stream video จาก devices ไปเก็บและวิเคราะห์บน AWS เพื่อทำ analytics หรือ ML (เช่น face recognition, security cameras)

Kinesis Use Case Example
IoT Sensors ──► Kinesis Data Streams ──► Kinesis Data Analytics (SQL real-time)
   (100k sensors)                          ──► Kinesis Data Firehose ──► S3 (archive)
                                            ──► Lambda ──► DynamoDB (alerts)
07

Amazon MQ

Amazon MQ เป็น managed message broker สำหรับ traditional protocols ที่ใช้กันมานานก่อนยุค cloud-native — เช่น MQTT, AMQP, STOMP, Openwire, WSS

  • Managed Apache ActiveMQ และ RabbitMQ
  • รองรับ protocols: MQTT, AMQP, STOMP, Openwire, WSS
  • มีทั้ง queue feature (~SQS) และ topic feature (~SNS) ในตัวเดียว
  • ไม่ scale ได้แบบ SQS/SNS — รันบน dedicated server (ไม่ใช่ serverless)
  • เหมาะกับการ migrate on-premises applications มาที่ cloud โดยไม่ต้อง re-engineer code
เปรียบเทียบ Amazon MQ กับ SQS/SNS
                    | Amazon MQ          | SQS / SNS
────────────────────|────────────────────|───────────────────
Type                | Managed broker     | Cloud-native service
Protocols           | MQTT, AMQP, STOMP, | AWS proprietary API
                    | Openwire, WSS      |
Scale               | Limited (server)   | Massive (serverless)
Architecture        | Dedicated machines | Fully serverless
Features            | Queue + Topic      | SQS=queue, SNS=topic
Use case            | Migrate on-prem    | Cloud-native apps

Note: Amazon MQ ทำงานบน dedicated machines (instance-based) จึงต้อง provision capacity ล่วงหน้า ต่างจาก SQS/SNS ที่เป็น serverless และ scale อัตโนมัติ

08

When to Use Which?

Decision matrix สำหรับเลือก integration service ที่เหมาะกับงาน — เป็นจุดที่ข้อสอบมักถามบ่อย:

ใช้ SQS เมื่อ...

ต้องการ decouple producer-consumer แบบง่ายๆ — async processing เช่น background jobs ต้องการ retry logic + DLQ สำหรับ error handling buffer load ระหว่าง services

ใช้ SNS เมื่อ...

ต้องส่ง notifications (email, SMS, push) — fan-out ไปหลาย consumers พร้อมกัน trigger Lambda จาก events broadcast events ให้หลาย services

ใช้ Kinesis เมื่อ...

Real-time streaming data ปริมาณมหาศาล — real-time analytics (SQL on stream) log/metric ingestion ระดับ big data ต้อง replay data ได้ + multiple consumers

ใช้ Amazon MQ เมื่อ...

Migrate on-premises application มา cloud — application ใช้ traditional protocols (MQTT, AMQP, STOMP) ไม่อยาก re-engineer code เพื่อใช้ AWS API

Quick Decision Guide
"async background job processing"          → SQS
"send email/SMS notification"               → SNS
"broadcast event ไป 5 services พร้อมกัน"     → SNS + SQS (Fan-out)
"real-time IoT sensor data + analytics"     → Kinesis
"clickstream / log streaming + S3 archive"  → Kinesis Data Firehose
"migrate Java app ที่ใช้ AMQP จาก on-prem"  → Amazon MQ
"order processing — ห้ามหาย ถ้า consumer ล่ม" → SQS
"trigger Lambda จาก S3 upload"              → SNS (หรือ S3 event direct)
09

Summary

สรุปบริการในกลุ่ม Cloud Integration ที่ออกข้อสอบ CLF-C02 บ่อย — แต่ละตัวมีจุดเด่นและ use case ของตัวเอง:

Amazon SQS

Message queue (pull-based) — retention 4-14 วัน, มี Visibility Timeout + DLQ, message size 256 KB เหมาะกับ decoupling + buffering

Amazon SNS

Pub/Sub (push-based) — สูงสุด 10M subscribers/topic, รองรับ Email, SMS, HTTP, SQS, Lambda เหมาะกับ notifications + fan-out

Fan-out Pattern

SNS + multiple SQS — publisher ส่งครั้งเดียว แต่ละ subscriber queue ได้รับ + process อิสระ ตัวหนึ่งล่มไม่กระทบตัวอื่น

Amazon Kinesis

Real-time streaming big data — Streams, Firehose, Analytics, Video Streams เหมาะกับ IoT, logs, clickstreams ระดับ big data

Amazon MQ

Managed broker (ActiveMQ / RabbitMQ) สำหรับ MQTT, AMQP, STOMP, Openwire ใช้ migrate on-prem app ที่ผูกกับ traditional protocols

Decoupling = หัวใจ

Resilience + Independent Scaling — services ทำงานอิสระจากกัน เป็นหัวใจของ event-driven architecture

  • SQS: queue, pull-based, retention 4-14 วัน, มี Visibility Timeout + DLQ, message size 256 KB
  • SNS: pub/sub, push-based, สูงสุด 10M subscribers/topic, รองรับ Email/SMS/HTTP/SQS/Lambda
  • Kinesis: real-time streaming big data — Streams, Firehose, Analytics, Video Streams
  • Amazon MQ: managed broker (ActiveMQ/RabbitMQ) สำหรับ MQTT/AMQP/STOMP — ใช้ migrate on-prem
ทดสอบ

คำถามทบทวน

8 ข้อ — เลือกคำตอบเพื่อดูเฉลยและคำอธิบาย

ข้อ 1 / 8คะแนน 0

ข้อใดอธิบายความแตกต่างระหว่าง SQS กับ SNS ได้ถูกต้อง?