bylijinnan
bylijinnan

Reputation: 766

how to implement a simple round-robin load balancer

I want to implement a simple round-robin load balancer and I want the 'getServer' method thread-safe:

private static int[] servers = new int[]{0,1,2};


private static int SERVER_INDEX = 0;

//not fast enough?
public synchronized static int getServer() {
    SERVER_INDEX++;
    if (SERVER_INDEX >= servers.length - 1) {
        SERVER_INDEX = 0;
    }
    return servers[SERVER_INDEX];
}


private static AtomicInteger SERVER_INDEX_2 = new AtomicInteger(0);

//not thread-safe and will get Exception
public static int getServer2() {
    int index = SERVER_INDEX_2.getAndIncrement();
    if (index >= servers.length - 1) {
        SERVER_INDEX_2.set(0);
    }
    return servers[index]; //ERROR! arrayIndexOutOfRange
}



private static AtomicLong SERVER_INDEX_3 = new AtomicLong(0);

//thread-safe but...
public static int getServer3() {
    long longIndex = SERVER_INDEX_3.getAndIncrement();
    long index = longIndex % servers.length;
    int intIndex = (int)index;
    return servers[intIndex]; //May overflow someday! 'intIndex can be negative'...
}

I think all above is not good enough. Is there a simple and elegant approach?

Upvotes: 4

Views: 6256

Answers (1)

Grisha Weintraub
Grisha Weintraub

Reputation: 7986

In Java 8 you can use getAndAccumulate as follows:

private static AtomicInteger ind = new AtomicInteger(0);

public static int getServer() {
    return servers[ind.getAndAccumulate(servers.length, (cur, n)->cur >= n-1 ? 0 : cur+1)];
}

Upvotes: 7

Related Questions