Bỏ qua tới nội dung
CloudDojo
Phần 2/5 · 6 phút

Vòng đời message trong Amazon SQS queue

0/3
Từ khóa của phần nàyChọn một từ để xem nghĩa trước khi đọc

Message trong SQS queue chỉ bị xoá sau khi consumer xử lý thành công; cơ chế này bảo đảm tác vụ không bị mất khi consumer gặp sự cố trong quá trình xử lý.

Khi worker nhận một message, message chưa bị xoá khỏi queue mà chỉ bị ẩn trong một khoảng thời gian gọi là visibility timeout. Sau khi xử lý xong, worker phải gọi DeleteMessage. Nếu worker gặp sự cố trong quá trình xử lý, message hiển thị trở lại để worker khác xử lý. Các tính năng của SQS đều xoay quanh vòng đời này. Chọn từng tình huống của MFTECH và chạy mô phỏng:

3 message trong queue · visibility timeout 60 s
t = 0 sWebQueue đơn hàng · visibility timeout 60 sWorker 1 · Spotsẵn sàngWorker 2 · Spotsẵn sàngđã xoá#1#2#3

Chọn Chạy mô phỏng để xem từng bước.

Theo một message qua bưu điện SQS: bị ẩn khi đang xử lý, hiện lại khi worker hỏng, sang dead-letter queue sau nhiều lần lỗi.

Một mô hình khác: ứng dụng gửi yêu cầu tính phí vận chuyển và chờ phản hồi (request-response). Việc tạo một queue thực cho mỗi yêu cầu tốn thời gian và chi phí. Temporary queue (thông qua Temporary Queue Client) tạo các virtual queue ngay trong ứng dụng, ghép chúng lên một queue SQS thực và tự động dọn dẹp khi hoàn tất.

Delay queue áp dụng cho mọi message của queue. Khi chỉ một số message cần trì hoãn (ví dụ email nhắc thanh toán gửi sau 10 phút), producer đặt message timer: tham số DelaySeconds trên từng message khi gửi, từ 0 giây (mặc định) tới tối đa 15 phút; giá trị này ghi đè DelaySeconds của delay queue. FIFO queue không hỗ trợ timer cho từng message. Cần lập lịch xa hơn 15 phút: AWS khuyến nghị EventBridge Scheduler. Message timer, delay queue và visibility timeout chỉ quyết định khi nào message được nhìn thấy; không tính năng nào trong số này là cách nhận message, và không tính năng nào giảm số lệnh ReceiveMessage trả về rỗng. Chỉ long polling (wait time tối đa 20 giây) giảm số phản hồi rỗng và do đó giảm chi phí.

Ghi nhớ:
  • Visibility timeout: ẩn message trong thời gian xử lý (mặc định 30 giây, tối đa 12 giờ).
  • Dead-letter queue: cô lập message xử lý lỗi nhiều lần.
  • Long polling: giảm số lần nhận rỗng và giảm chi phí.
  • Delay queue: trì hoãn mọi message mới gửi vào queue; message timer: trì hoãn từng message (0 tới 15 phút, không áp dụng cho FIFO queue).
  • Ưu tiên xử lý: hai queue, đọc queue ưu tiên cao trước.
  • Request-response: temporary queue.
Part 2/5 · 6 phút

Message lifecycle in an Amazon SQS queue

0/3
Từ khóa của phần nàyChọn một từ để xem nghĩa trước khi đọc

A message in an SQS queue is deleted only after the consumer has processed it successfully; this ensures no task is lost when a consumer fails during processing.

When a worker receives a message, the message is not yet deleted from the queue; it is only hidden for a period called the visibility timeout. After processing, the worker must call DeleteMessage. If the worker fails during processing, the message becomes visible again so that another worker can process it. SQS features all revolve around this life cycle. Choose each MFTECH scenario and run the simulation:

3 message trong queue · visibility timeout 60 s
t = 0 sWebQueue đơn hàng · visibility timeout 60 sWorker 1 · Spotsẵn sàngWorker 2 · Spotsẵn sàngđã xoá#1#2#3

Chọn Chạy mô phỏng để xem từng bước.

Theo một message qua bưu điện SQS: bị ẩn khi đang xử lý, hiện lại khi worker hỏng, sang dead-letter queue sau nhiều lần lỗi.

Another pattern: the application sends a request to calculate shipping fees and waits for the response (request-response). Creating a real queue for each request costs time and money. A temporary queue (through the Temporary Queue Client) creates virtual queues inside the application, multiplexes them onto one real SQS queue and cleans them up automatically when done.

A delay queue applies to every message in the queue. When only some messages need a delay (for example a payment reminder email sent after 10 minutes), the producer sets a message timer: the DelaySeconds parameter on an individual message when it is sent, from 0 seconds (the default) up to a maximum of 15 minutes; this value overrides the delay queue’s DelaySeconds. FIFO queues don’t support timers on individual messages. For scheduling further than 15 minutes ahead, AWS recommends EventBridge Scheduler. Message timers, delay queues and the visibility timeout only decide when a message is visible; none of them is a way to receive messages, and none of them reduces the number of ReceiveMessage calls that return empty. Only long polling (maximum wait time 20 seconds) reduces empty responses and therefore cost.

Ghi nhớ:
  • Visibility timeout: hide a message while it is being processed (default 30 seconds, maximum 12 hours).
  • Dead-letter queue: isolate messages that fail processing repeatedly.
  • Long polling: fewer empty receives and lower cost.
  • Delay queue: postpone every new message sent to the queue; message timer: postpone an individual message (0 to 15 minutes, not supported on FIFO queues).
  • Processing priority: two queues, read the high-priority queue first.
  • Request-response: temporary queues.

Kiểm tra nhanh

Trả lời để ghi nhận đã đọc phần này. Câu trả lời sai sẽ được nhắc ôn lại vào những ngày sau.

Worker nhận một message rồi bị terminate trước khi xử lý xong. Điều gì xảy ra với message đó?
Chi phí SQS của MFTECH cao vì worker nhận về quá nhiều phản hồi rỗng. Chọn gì?

Đã trả lời 0/2 câu