Electing a leader among a group of peer processes is a problem that needs to be solved time and again for distributed systems. This talk shows our solution for nodejs services; using Consul and a simple, event-based client library.
with locking. Comes with recommended algorithm for doing leader election 2. Nodes agree on common key in KV store. (eg “/locks/myservice”) 3. Each node: a. Create session using session API b. Attempt to acquire lock using session id. i. OK => LEADERSHIP! ii. Fail => Blocking and wait for changes on key, re-attempt (a.) if “session” field is blank.
and other nodes will be notifed and can claim leadership. • Network problems ◦ “error” event will be raised causing leader to crash/stop doing work ◦ session will timeout so that other nodes will be notifed and can claim leadership once network is stable. • Consul crash ◦ “error” event will be raised causing client to crash/stop doing work
needs. • HTTP based - stable but slow. Not for near-realtime use cases. • Guarantees only one leader elected at a time. • There will be gaps with zero leaders during recovery.