An open API service indexing awesome lists of open source software.

https://github.com/gregyjames/leaderelection

An easy-to-use C# implementation of the distributed leader election pattern, leveraging common infrastructure tools like Azure Blob Storage, Redis, and others.
https://github.com/gregyjames/leaderelection

blob-storage coordination coordinator-pattern csharp distributed distributed-computing distributed-systems dotnet dotnet-core instances kubernetes kubernetes-cluster leader-election leadership microservice microservices redis s3

Last synced: 6 days ago
JSON representation

An easy-to-use C# implementation of the distributed leader election pattern, leveraging common infrastructure tools like Azure Blob Storage, Redis, and others.

Awesome Lists containing this project

README

          

[![.NET](https://github.com/gregyjames/LeaderElection/actions/workflows/dotnet.yml/badge.svg)](https://github.com/gregyjames/LeaderElection/actions/workflows/dotnet.yml)
[![codecov](https://codecov.io/gh/gregyjames/LeaderElection/graph/badge.svg?token=3Fmj814UkK)](https://codecov.io/gh/gregyjames/LeaderElection)
![NuGet Version](https://img.shields.io/nuget/v/LeaderElection?style=flat&label=LeaderElection&link=https%3A%2F%2Fwww.nuget.org%2Fpackages%2FLeaderElection)
![NuGet Version](https://img.shields.io/nuget/v/LeaderElection.BlobStorage?style=flat&label=LeaderElection.BlobStorage&link=https%3A%2F%2Fwww.nuget.org%2Fpackages%2FLeaderElection.BlobStorage)
![NuGet Version](https://img.shields.io/nuget/v/LeaderElection.Redis?style=flat&label=LeaderElection.Redis&link=https%3A%2F%2Fwww.nuget.org%2Fpackages%2FLeaderElection.Redis)
![NuGet Version](https://img.shields.io/nuget/v/LeaderElection.DistributedCache?style=flat&label=LeaderElection.DistributedCache&link=https%3A%2F%2Fwww.nuget.org%2Fpackages%2FLeaderElection.DistributedCache)
![NuGet Version](https://img.shields.io/nuget/v/LeaderElection.FusionCache?style=flat&label=LeaderElection.FusionCache&link=https%3A%2F%2Fwww.nuget.org%2Fpackages%2FLeaderElection.FusionCache)
![NuGet Version](https://img.shields.io/nuget/v/LeaderElection.S3?style=flat&label=LeaderElection.S3&link=https%3A%2F%2Fwww.nuget.org%2Fpackages%2FLeaderElection.S3)
![NuGet Version](https://img.shields.io/nuget/v/LeaderElection.Postgres?style=flat&label=LeaderElection.Postgres&link=https%3A%2F%2Fwww.nuget.org%2Fpackages%2FLeaderElection.Postgres)

# Leader Election

An easy-to-use C# implementation of the distributed leader election pattern, leveraging common
infrastructure tools like Azure Blob Storage, Redis, and others.

## Features

- **Async/Await Support**: Full async/await pattern support for better performance and scalability
- **Comprehensive Error Handling**: Robust error handling with retry logic and exponential backoff
- **Structured Logging**: Integration with Microsoft.Extensions.Logging for observability
- **Configuration Options**: Flexible configuration through options pattern
- **Event-Driven**: Leadership change and error events for reactive programming
- **Graceful Shutdown**: Proper cleanup and resource disposal
- **Thread Safety**: Thread-safe operations with proper synchronization

## Currently Supported Stores

- Redis → [LeaderElection.Redis](https://www.nuget.org/packages/LeaderElection.Redis)
- FusionCache →
[LeaderElection.FusionCache](https://www.nuget.org/packages/LeaderElection.FusionCache)
- DistributedCache →
[LeaderElection.DistributedCache](https://www.nuget.org/packages/LeaderElection.DistributedCache)
- Azure Blob Storage (blob leases) →
[LeaderElection.BlobStorage](https://www.nuget.org/packages/LeaderElection.BlobStorage)
- S3 (minio) → [LeaderElection.S3](https://www.nuget.org/packages/LeaderElection.S3)
- Postgres → [LeaderElection.Postgres](https://www.nuget.org/packages/LeaderElection.Postgres)

## Quick Start

### 1. Install the Package

```bash
dotnet add package LeaderElection
dotnet add package LeaderElection.Redis
```

### 2. Configure Services

Look in LeaderElectionTester for a full example.

```csharp
using LeaderElection.Redis;

var builder = WebApplication.CreateBuilder(args);

// Add Redis connection
builder.Services.AddSingleton(sp =>
{
return ConnectionMultiplexer.Connect("localhost:6379");
});

// Add leader election with configuration
builder.Services.AddRedisLeaderElection(settings =>
{
settings.LockKey = "leader_election_tester";
settings.LockExpiry = TimeSpan.FromSeconds(30);
settings.RenewInterval = TimeSpan.FromSeconds(10);
settings.RetryInterval = TimeSpan.FromSeconds(5);
settings.EnableGracefulShutdown = true;
});
```

### 3. Use in Your Service

If you execute this program below, you will notice that leadership is automatically transfered
across instances once the current leader is killed!

```csharp
public class MyService : BackgroundService
{
private readonly ILeaderElection _leaderElection;
private readonly ILogger _logger;

public MyService(ILeaderElection leaderElection, ILogger logger)
{
_leaderElection = leaderElection;
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Subscribe to events
_leaderElection.LeadershipChanged += OnLeadershipChanged;
_leaderElection.ErrorOccurred += OnErrorOccurred;

try
{
// Start leader election
await _leaderElection.StartAsync(stoppingToken);

// Run leader tasks
while (!stoppingToken.IsCancellationRequested)
{
await _leaderElection.RunTaskIfLeaderAsync(async ct =>
{
_logger.LogInformation("Executing leader task");
await DoLeaderWorkAsync(ct);
}, stoppingToken);

_logger.LogInformation("Executing normal task");

await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
}
}
finally
{
// Cleanup
_leaderElection.LeadershipChanged -= OnLeadershipChanged;
_leaderElection.ErrorOccurred -= OnErrorOccurred;
await _leaderElection.StopAsync(stoppingToken);
}
}

private void OnLeadershipChanged(object? sender, bool isLeader)
{
_logger.LogInformation("Leadership changed: {IsLeader}", isLeader);
}

private void OnErrorOccurred(object? sender, Exception exception)
{
_logger.LogError(exception, "Leader election error");
}
}
```

## API Reference

### Methods

- `StartAsync(CancellationToken)` - Start the leader election process
- `StopAsync(CancellationToken)` - Stop the leader election process
- `TryAcquireLeadershipAsync(CancellationToken)` - Manually attempt to acquire leadership
- `RunTaskIfLeaderAsync(Func, CancellationToken)` - Execute a task only if
this instance is the leader
- `RunTaskIfLeaderAsync(Action, CancellationToken)` - Execute a synchronous task
only if this instance is the leader

### Properties

- `IsLeader` - Returns true if this instance is currently the leader
- `LastLeadershipRenewal` - Date/Time of the last accquisition of leadership

### Events

- `LeadershipChanged` - Fired when leadership status changes
- `ErrorOccurred` - Fired when an error occurs during leader election

## Contributing

1. Fork the repository
2. Run `./build.ps1 bootstrap`\* to set up the development environment.
3. Create a feature branch
4. Make your changes
5. Add tests
6. Review test coverage using `./build.ps1 coverage`.
7. Submit a pull request

See `./build.ps1 list` for other common tasks.

_\* Requires [PowerShell 7.4](https://aka.ms/powershell) or later._

## License

MIT License

Copyright (c) 2025 Greg James

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## Icon

["Business icon pack leader icon"](https://cdn0.iconfinder.com/data/icons/business-1797/32/19-512.png)
by [mr icons](https://www.iconfinder.com/mr-icons-1) is licensed under
[CC BY 4.0](http://creativecommons.org/licenses/by/4.0)