Skip to content

Commit

Permalink
SQS fair queue
Browse files Browse the repository at this point in the history
  • Loading branch information
manhtai committed Jun 16, 2023
1 parent 398607d commit 04fe02e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions content/posts/sqs-simple-fair-queuing.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 04fe02e

Please sign in to comment.