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
AcquireSemaphorewithCount=1, specifying their endpoint in theDatafield. - Since the semaphore limit is very high, all
AcquireSemaphorecalls 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
DescribeSemaphorewithIncludeOwners=trueand optionally withWatchOwners=true. In the result, theOwnersfield'sDatawill contain the endpoints of registered application instances. - When the list of endpoints changes,
OnChangedis called. In this case, clients make a similarDescribeSemaphorecall and receive the updated list.
Was the article helpful?
Previous