NATS Key-Value Store based Leader Election

What?

A Leader Election system that uses keys in a NATS Key-Value Store to perform leader election.

How?

NATS KV Buckets have a TTL, creating a bucket with a TTL of 1 minute will imply that a leader has to maintain his leadership by updating the bucket more frequently than 1 minute.

The NATS KV Interface has a Create() function that will only succeed if the key does not already exist, thus a worker who is campaigning for leadership will regularly attempt to create data in the key, whoever manages to do this becomes the leader.

A leader will then regularly, around 75% of TTL time, do a Update() on the key ensuring that the sequence number is the sequence number of his last Update(), as long as this series is maintained he knows he is still the leader. Failure to Update() is a leadership loss.

If the leader fails to update the key within TTL, the key will expire and one of the campaigners will have a successful Create() call, so leadership is switched.

Limitations?

We have some hard-coded limits just to keep things from being too aggressive:

  • Shortest bucket TTL is 30 seconds
  • Longest bucket TTL is 1 hour
  • Shortest campaign interval is 5 seconds
  • Shortest delta from campaign interval to bucket TTL is 5 seconds

When a leadership is gained the leader is only notified that it is won after the campaign interval - 75% of the TTL - this ensures that any previous leader had a chance to stand down.

Additionally, a back-off is supported that can slow down campaigns by non leader candidates. Using this the TTL can be kept low, leadership switches be handled without a long sleep and campaigns by candidates do not need to be too aggressive. It also implies that if a leadership is lost that leader will, initially, re-campaign aggressively, in practise this results in leadership staying relatively stable in periods of network uncertainty or cluster reboots.

Usage?

$ nats kv add --ttl 5m ELECTIONS 
kv, _ := js.KeyValue("ELECTIONS")

elect, _ := NewElection("member 1", "election", kv,
	OnWon(handleBecomingLeader),
	OnLost(handleLosingLeadership)))

// blocks until stopped, calls the handleBecomingLeader() and handleLosingLeadership() functions on change
elect.Start(ctx)

Contact?

R.I.Pienaar / @ripienaar / [email protected]

Owner
R.I.Pienaar
Systems Architect, Automator, Coder.
R.I.Pienaar
Similar Resources

⚡️ A lightweight service that will build and store your go projects binaries, Integrated with Github, Gitlab, Bitbucket and Bitbucket Server.

⚡️ A lightweight service that will build and store your go projects binaries, Integrated with Github, Gitlab, Bitbucket and  Bitbucket Server.

Rabbit A lightweight service that will build and store your go projects binaries. Rabbit is a lightweight service that will build and store your go pr

Nov 19, 2022

Go client to reliable queues based on Redis Cluster Streams

Ami Go client to reliable queues based on Redis Cluster Streams. Consume/produce performance Performance is dependent from: Redis Cluster nodes count;

Dec 12, 2022

websocket based messaging server written in golang

Guble Messaging Server Guble is a simple user-facing messaging and data replication server written in Go. Overview Guble is in an early state (release

Oct 19, 2022

Machinery is an asynchronous task queue/job queue based on distributed message passing.

Machinery is an asynchronous task queue/job queue based on distributed message passing.

Machinery Machinery is an asynchronous task queue/job queue based on distributed message passing. V2 Experiment First Steps Configuration Lock Broker

Jan 7, 2023

ChizBroker is a fast and simple GRPC based implementation of kafka.

ChizBroker is a fast and simple GRPC based implementation of kafka.

Chiz Broker: a broker for fun ChizBroker is a fast and simple GRPC based implementation of kafka. Features: Ready to be deployed on kubernetes Prometh

Oct 30, 2022

Persistent queue in Go based on BBolt

Persistent queue Persistent queue based on bbolt DB. Supposed to be used as embeddable persistent queue to any Go application. Features: messages are

Jun 30, 2022

A lightweight, distributed and reliable message queue based on Redis

nmq A lightweight, distributed and reliable message queue based on Redis Get Started Download go get github.com/inuggets/nmq Usage import "github.com

Nov 22, 2021

The Bhojpur MDM is a software-as-a-service product used as a Mobile Device Manager based on Bhojpur.NET Platform for application delivery.

Bhojpur MDM - Mobile Device Manager The Bhojpur MDM is a software-as-a-service product used as a Mobile Device Manager based on Bhojpur.NET Platform f

Dec 31, 2021

Go-jdeque - Chunk based deque for Go

go-jqueue This queue is implemented with a linked list of chunks. It is a deque

Dec 23, 2022
Comments
  • KeyValue Replicas > 1

    KeyValue Replicas > 1

    Hi! Should it work with nats.KeyValueConfig{Replicas: 3}? Even with Replicas: 2 Nats log flooding

    Resetting stream cluster state for '$G > KV_election'
    

    Update(key, ...) method return timeout. But everything fine with Replicas: 1

Nats-subject-profiler - NATS Subject Profiler With Golang

NATS Subject Profiler Example Connect it to the demo NATS server. nats-subject-p

Feb 7, 2022
Basic kick the tires on NATS Key-Value API (Go)

nats-kv-101 Basic kick the tires on NATS Key-Value API (Go) Usage # Get ./mybucket -s "nats://vbox1.tinghus.net" -creds "/home/todd/lab/nats-cluster1/

Feb 15, 2022
NATS example to store and retrieve large file assets from JetStream.

njs-xfer njs-xfer is a sample application that demonstrates the ability to use NATS and JetStream to store and retrieve large file assets. This sample

Dec 26, 2022
Golang client for NATS, the cloud native messaging system.

NATS - Go Client A Go client for the NATS messaging system. Installation # Go client go get github.com/nats-io/nats.go/ # Server go get github.com/na

Jan 5, 2023
High-Performance server for NATS, the cloud native messaging system.
High-Performance server for NATS, the cloud native messaging system.

NATS is a simple, secure and performant communications system for digital systems, services and devices. NATS is part of the Cloud Native Computing Fo

Jan 2, 2023
Queue with NATS Jetstream to remove all the erlangs from cloud

Saf in Persian means Queue. One of the problems, that we face on projects with queues is deploying RabbitMQ on the cloud which brings us many challenges for CPU load, etc. I want to see how NATS with Jetstream can work as the queue to replace RabbitMQ.

Dec 15, 2022
Мост между NATS streaming и MQ Series

Мост между NATS streaming и MQ Series Оригинальный репозиторий https://github.com/nats-io/nats-mq NATS-MQ Bridge This project implements a simple, but

Nov 26, 2021
A basic pub-sub project using NATS

NATS Simple Pub-Sub Mechanism This is a basic pub-sub project using NATS. There is one publisher who publishes a "Hello World" message to a subject ca

Dec 13, 2021
Vigia-go-nats - Program for processing camera metadata

VIGIA MIGRAR O HOUSEKEEPER PARA O PYTHON Programa para processamento de metadado

Jan 10, 2022
Kafka tool to emit tombstones for messages based on header value matches

Langolier Langolier is a CLI tool to consume a Kafka topic and emit tombstones for messages matched by header name/value pairs. Usage Usage of langoli

Sep 22, 2021