Vaibhav
Vaibhav

Reputation: 49

Golang Singleton for all replicas of microservice

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?

Upvotes: 0

Views: 415

Answers (1)

VisioN
VisioN

Reputation: 145368

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

Upvotes: 4

Related Questions