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?
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).