Service discovery
Consider a scenario where application instances are dynamically started and publish their endpoints, while other clients need to receive this list and respond to its changes.
This scenario can be implemented using semaphores in YDB coordination nodes as follows:
- Create a semaphore (for example, named
my-service-endpoints
) withLimit=Max<ui64>()
. - All application instances call
AcquireSemaphore
withCount=1
, specifying their endpoint in theData
field. - Since the semaphore limit is very high, all
AcquireSemaphore
calls should complete quickly. - At this point, publication is complete, and application instances only need to respond to session stops by republishing themselves through a new session.
- Clients call
DescribeSemaphore
withIncludeOwners=true
and optionally withWatchOwners=true
. In the result, theOwners
field'sData
will contain the endpoints of registered application instances. - When the list of endpoints changes,
OnChanged
is called. In this case, clients make a similarDescribeSemaphore
call and receive the updated list.