liveBook · Manning

Round-robin load balancing

As explained in Build and Orchestrator in Go (From Scratch)

A round-robin scheduler selects machines in a repeating circular order rather than ranking them by a calculated score.

Round-robin load balancing keeps task submission simple: users send work to a manager, which chooses a worker instead of requiring users to track worker capacity or task counts. section 7.0

Use a small mental model: the manager owns an ordered worker list and remembers the index of the last selected worker. Each selection advances that index; reaching the end wraps back to zero. section 7.3.1

Listing 7.4 Implementing a naive scheduling algorithm
func (m *Manager) SelectWorker() string {
    var newWorker int                         #1
    if m.LastWorker+1 < len(m.Workers) {      #2
        newWorker = m.LastWorker + 1          #3
        m.LastWorker++                        #4
    } else {                                  #5
        newWorker = 0                         #6
        m.LastWorker = 0                      #7
    }
 
    return m.Workers[newWorker]               #8
}

The nine-line Go method makes the state transition explicit: compare the previous index with the list boundary, advance or reset, then return the selected worker. section 7.3.1

A minimal implementation assigns these responsibilities:

  • Store workers as address strings, commonly in <hostname>:<port> form. section 7.3.1
  • Store LastWorker as an integer index into the worker slice. section 7.3.1
  • Return the next worker, or return the first worker after the final entry. section 7.3.1

Three workers in rotation

Create workers at localhost:5556, localhost:5557, and localhost:5558, pass their addresses to manager.New with the "roundrobin" scheduler, and submit tasks through the manager. The manager selects the workers in sequence, cycling back to the first after the third. section 10.7

This is a deliberately naive scheduler. Unlike a score-based scheduler, it does not calculate which candidate machine is best; it follows order and retains only its position in that order. section 1.5.3

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Implements selection state and wraparound in nine lines, while stopping short of capacity scoring or sophisticated scheduling.
Examples
Uses a Go manager with three localhost workers, incremented ports, submitted tasks, and observed worker selection.
What it assumes
Assumes manager and worker roles, then makes worker-list state and selection transitions explicit.
What you can do after
Build it from scratch by storing a worker index and implementing advancing, resetting, and returning the selected worker.
How it connects
Connects the manager, worker, scheduler, task queue, and score-based scheduling concepts.

Also explained in

Compare how 3 books explain round-robin load balancing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.