0
votes

I'm implementing a service for tasks processing and I would like to manage the quality of the service giving greater priority to certain types of tasks. There are four types of tasks, and for this reason I would use four queues, one for each type of task.

  • Could be it convenient to create four processing threads (one for each queue) and assign to them different priorities?
  • Or should I should make the processing thread take care mainly with the higher priority queue?
  • Are there other approaches?
1
There's really no universal answer to this kind of thing. Too much depends on questions about what kind of hardware you're going to run this on (e.g. how many processors/cores) and how parallelizable each of those tasks is. - DGH
The tasks are independent of each other (even those ones of the same type). Using the priority I should simply make sure that in a certain time interval, the number of tasks processed (of a certain type) is directly proportional to their priorities (assumed that there is the same number of tasks in the queues). - enzom83

1 Answers

1
votes

I would suggest having a single thread that is responsible for grabbing tasks.

There are many, many possible strategies. One is simply to have 4 queues, and try to cycle between them. Another is to stick tasks into a priority queue (typically implemented with a heap data structure), but if you do that then be aware that all of the higher priority tasks will be taken before any lower priority tasks. A third is to use a priority queue based on age so you can take the oldest request first - then make high priority requests artificially old. (I could suggest the age of the oldest thing in the queue plus a constant term.)

One general point to keep in mind. If you assign sufficient capacity, your queues will likely remain reasonably short. If your capacity is insufficient, then queues will grow without bound and in the long run you can think of your queueing problem as one of triage rather than prioritization. But if possible, it often works well to try to increase capacity instead of being clever in prioritization.