Decoupling hai tầng ứng dụng bằng Amazon SQS queue
Khi tầng tiếp nhận đơn hàng có thông lượng cao hơn tầng xử lý, một SQS queue đặt giữa hai tầng cho phép producer không phải chờ, còn consumer xử lý message theo năng lực của mình.
Flash sale bắt đầu lúc 20h. Web tier tiếp nhận đơn hàng với tốc độ cao, trong khi tầng xử lý (thanh toán, trừ tồn kho) có thông lượng thấp hơn nhiều. Mô phỏng dưới đây so sánh ba phương án kết nối hai tầng này:
Chọn phương án kết nối, sau đó chọn Chạy mô phỏng flash sale. So sánh kết quả của cả ba phương án.
Amazon SQS là dịch vụ hàng đợi được AWS quản lý. Bên gửi (producer) đưa message vào queue và tiếp tục xử lý tác vụ khác; bên nhận (consumer) chủ động poll queue để lấy message và xử lý theo năng lực của mình. Tầng có thông lượng cao không phải chờ tầng có thông lượng thấp; mô hình này gọi là decoupling.
Để Auto Scaling group của worker scale tương ứng với khối lượng công việc đang chờ trong queue, tài liệu Amazon EC2 Auto Scaling khuyến nghị chỉ số backlog per instance: số message có thể nhận trong queue (ApproximateNumberOfMessages, metric CloudWatch ApproximateNumberOfMessagesVisible) chia cho số instance ở trạng thái InService, kết hợp target tracking để duy trì chỉ số này quanh giá trị chấp nhận được. Một scaling policy chỉ dựa trên số message trong queue không tính tới số instance hiện có, do đó có thể bổ sung nhiều instance hơn mức cần thiết trong khi queue đang giảm dần.
Một số kiến trúc tích hợp queue vào hệ thống MFTECH:
S3 Event Notification gửi một message vào queue cho mỗi ảnh mới. Lambda đọc queue theo batch và chỉ tính phí trong thời gian thực thi. Queue lưu giữ tác vụ khi lượng ảnh tải lên tăng đột biến.
- Kết nối tầng có thông lượng cao với tầng có thông lượng thấp bằng SQS: tác vụ không bị mất và mỗi tầng xử lý theo tốc độ riêng.
- Worker scale theo backlog per instance bằng target tracking.
- Truy cập SQS từ private subnet: interface VPC endpoint.
Decoupling two application tiers with an Amazon SQS queue
When the order intake tier has higher throughput than the processing tier, an SQS queue between the two tiers lets the producer stop waiting while the consumer processes messages at its own capacity.
The flash sale starts at 20:00. The web tier takes orders at a high rate, while the processing tier (payment, stock deduction) has much lower throughput. The simulation below compares three ways to connect these two tiers:
Chọn phương án kết nối, sau đó chọn Chạy mô phỏng flash sale. So sánh kết quả của cả ba phương án.
Amazon SQS is a queue service managed by AWS. The sender (producer) puts a message in the queue and continues with other work; the receiver (consumer) polls the queue to get messages and processes them at its own capacity. The high-throughput tier does not have to wait for the low-throughput tier; this model is called decoupling.
To scale the workers’ Auto Scaling group with the amount of work waiting in the queue, the Amazon EC2 Auto Scaling documentation recommends the backlog per instance metric: the number of messages available to retrieve from the queue (ApproximateNumberOfMessages, CloudWatch metric ApproximateNumberOfMessagesVisible) divided by the number of instances in the InService state, combined with target tracking to keep this metric near an acceptable value. A scaling policy based only on the number of messages in the queue does not account for the number of instances already running, so it can add more instances than needed while the queue is already shrinking.
Some architectures that integrate a queue into the MFTECH system:
S3 Event Notifications send one message to the queue for each new image. Lambda reads the queue in batches and is charged only for execution time. The queue holds the tasks when image uploads spike.
- Connect a high-throughput tier to a low-throughput tier with SQS: no work is lost and each tier processes at its own pace.
- Workers scale on backlog per instance with target tracking.
- Access SQS from a private subnet: interface VPC endpoint.