From 04fe02e3cd44dae1c246608502ff9fda7d931a48 Mon Sep 17 00:00:00 2001 From: manhtai Date: Fri, 16 Jun 2023 11:07:36 +0700 Subject: [PATCH] SQS fair queue --- content/posts/sqs-simple-fair-queuing.md | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 content/posts/sqs-simple-fair-queuing.md diff --git a/content/posts/sqs-simple-fair-queuing.md b/content/posts/sqs-simple-fair-queuing.md new file mode 100644 index 0000000..01f06c4 --- /dev/null +++ b/content/posts/sqs-simple-fair-queuing.md @@ -0,0 +1,42 @@ +--- +title: "SQS Simple Fair Queuing" +date: 2023-06-16T10:23:06+07:00 +tags: ["AWS", "SQS", "Fair-queue"] +draft: false +--- + + +In a system with different user workloads, fairness is a must, unless +you honestly want the big fish to consume all the resources and let +the smaller ones wait in line. + +There are [many][0] fair-queuing algorithms out there, mostly constructed +to solve the fairness in network schedulers. But for simple web applications, +we only need a 2-queues system to solve the fairness problem: + +- One priority queue, which handles messages selectively, is rate-limited by +a user basis +- One regular queue, which handles all the messages sequentially + +Fortunately, AWS SQS comes with the dead-letter queues feature that fits +nicely in our use case. Let's create 2 queues, one dead-letter queue as the +regular queue, and another queue as the priority queue, with a redrive policy +that set max receive to 1 and dead-letter queue to the regular. + +The fairness logic should be clear now: + +- The first worker pulls messages from the priority queue, and checks if +it's rate limited. If not, go ahead and consume the message, otherwise, +put it back. Redrive policy will ensure the message is moved to the +dead-letter queue. + +- The second worker will work on the dead-letter queue, which consumes +messages one by one. + + + + + + +[0]: https://en.wikipedia.org/wiki/Fair_queuing +[1]: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html