All of us are familiar with overflow bugs. However, sometimes you write code that counts on overflow. This is a story where overflow was supposed to happen but didn’t, hence the name underflow bug.
In our Java implementation of the round-robin algorithm, we store the number of connections in variable
sizeand then we call
index() % sizeto get the index of the chosen connection. The value of
index()follows the sequence 0, 1, 2, 3, … For example with
size = 3, we get
index() % sizeequal to 0, 1, 2, 0, 1, 2, 0, …, which is exactly how round-robin works. Continue