Golang Singleton for all replicas of microservice

5/21/2021

I am using sync.once to implement singleton in my microservice. But I am not sure if this will ensure a single instance for all the replicas. How to create a Singleton object for distributed microservices in golang?

-- Vaibhav
go
kubernetes
microservices

1 Answer

5/21/2021

It cannot ensure a single instance between multiple processes running in different environments. sync is used to synchronise go routines, which are running against the main routine process.

To add locking that would work between multiple processes and multiple microservices, you need a single third-party instance, that would control it. It can be a queue message / cache broker (e.g. RabbitMQ, Redis) or database (e.g. PostgreSQL advisory locks).

-- VisioN
Source: StackOverflow