Scheduling load with a round robin algorithm?

Nevin Mathai picture Nevin Mathai · Apr 21, 2010 · Viewed 16.6k times · Source

I need to write a round robin algorithm to schedule load to n endpoints?

So if I have servers A, B and C

I wanted to make sure to round-robin through them for each request I get. How do I do this in C#?

Answer

kemiller2002 picture kemiller2002 · Apr 21, 2010

Just for the record, definition of round robin:

http://en.wikipedia.org/wiki/Round-robin_scheduling

Just use a queue. Take one off of the top, use it and put it back. This ensures that the most recent one used will always be the last one to be picked up.

Queue<Server> q = new Queue<Server>();

//get the next one up
Server s = q.DeQueue();


//Use s;


//put s back for later use.
q.Enqueue(s);

Link to the queue class:

http://msdn.microsoft.com/en-us/library/7977ey2c.aspx