Leader election

In this instance, we are using the WhenStartedAsLeader method from a wonderful third-party extension entitled Topshelf.Leader, which we installed via our NuGet package mechanism, the same as we have done before. This provides us with something known in computer science as leader election. Leader election is the process by which a single process can be designated as the leader among several distributed processes. In the beginning, no process knows who the leader is. They must agree on which one will assume this role. That is exactly the functionality the Topshelf.Leader framework provides.

We now use the WhenStartedAsLeader() extension method, which contains its own version of the WhenStarted() method, one with cancellation-token support. We want one Bitcoin microservice controlling many if needed, and we want them to handle leadership among themselves (who will take the next message, and so on).

The WhenStarted() method will be executed when the service discovers that it is the current leader. If that situation changes, the cancellation token will be set to cancelled. We will leave it up to the aspiring developer to enhance this method to do whatever you think is appropriate for your scenario.

We should mention that the Bitcoin microservice implements the ILeaseManager interface, which means that the responsibility for deciding on the leader falls to the microservice. Our microservice will call the AcquireLease() function until it gets a lease, meaning we are the leader. Once that happens, we run the WhenStarted delegate as instructed.

While all this is happening, we are still renewing our lease every few seconds. When we stop the service, we release the lease back to the next potential leader.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset