Sobes.tech
Senior

How to ensure exclusive execution of one of three worker processes on a single host (leader election)?

sobes.tech AI

Answer from AI

To ensure exclusive execution of one of the three worker processes on a single host (leader election), several approaches can be used:

  1. File lock: One process attempts to acquire a lock on a specific file. Only the process with the lock becomes the leader.

  2. Mutexes in the operating system: Using system mutexes or semaphores for synchronization.

  3. Using distributed storage systems: For example, Redis with the SETNX command, ZooKeeper, or Consul for leader coordination.

  4. Built-in libraries: In Go, there are libraries for leader election, for example, using etcd or Kubernetes leader election API.

Example using a file lock in Go:

package main

import (
	"fmt"
	"os"
	"syscall"
)

func main() {
	file, err := os.OpenFile("/tmp/leader.lock", os.O_CREATE|os.O_RDWR, 0666)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	// Attempt to acquire an exclusive lock
	err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
	if err != nil {
		fmt.Println("Failed to become leader, another process is already the leader")
		return
	}

	fmt.Println("This process has become the leader")
	// Leader performs its tasks
	select {} // block the process
}

This code attempts to acquire an exclusive lock on a file. If the lock is obtained, the process becomes the leader. If not — another process is already the leader.

How to ensure exclusive execution of one of three… - sobes.tech