RedisBalancer 1.0.0
dotnet add package RedisBalancer --version 1.0.0
NuGet\Install-Package RedisBalancer -Version 1.0.0
<PackageReference Include="RedisBalancer" Version="1.0.0" />
<PackageVersion Include="RedisBalancer" Version="1.0.0" />
<PackageReference Include="RedisBalancer" />
paket add RedisBalancer --version 1.0.0
#r "nuget: RedisBalancer, 1.0.0"
#:package RedisBalancer@1.0.0
#addin nuget:?package=RedisBalancer&version=1.0.0
#tool nuget:?package=RedisBalancer&version=1.0.0
RedisBalancer
RedisBalancer is a library that utilizes redis in order to distribute items amongst multiple instances equally.
Description
Types
This library has one basic type Instance<TItem>. Instance being the current instance that is running the library and TItem being any class that implements IEquitable<TItem>. The idea is that every Instance holds some Items and each time that the distribution job runs, it can correctly find and remove or add - distribute - the Items across the Instances.
Jobs
The library implements the basic c# PeriodicTimer and has 3 jobs.
- Ping: Informs that this Instance is active
- RemoveInactiveInstances: Checks for any Instance that has outdated Ping time and removes it.
- DistributeItems: Gets the items to be distributed via an ItemsProvider service that should be provided to the library and distributes them amongst the active Instances
Exposed Services
This library also expose a IGetInstances<TItem> service that can be used to get all Instances or get an Instance by name.
Options
When a project wants to use the library, it will require some options (RedisBalancerOptions). The properties are:
- RedisKey [required]: A string key that is used as a RedisKey. The RedisValue is an array of Instances. So all the Instances that use the same RedisKey will be used in order to distribute the Items.
- InstanceName [required]: A string which is used for the name of the Instance.
- RedisConnectionString [required]: A string which is used for the connection string of the redis to be connected to.
- PingInterval [required]: A TimeSpan which is used for the Ping job interval.
- RemoveInactiveInstancesInterval: A TimeSpan which is used for the RemoveInactiveInstances job interval. If not set then RemoveInactiveInstancesInterval = PingInterval * 2.
- DistributeItemsInterval: A TimeSpan which is used for the DistributeItems job interval. If not set then DistributeItemsInterval = PingInterval * 2.
- InstanceInactivityPeriod: A TimeSpan used by the RemoveInactiveInstances job, in order to determine the outdated Instances. If not set then InstanceInactivityPeriod = PingInterval * 3.
- RedisKeyExpiration: A TimeSpan that is used for the RedisKey expiration. If not set, then RedisKey will have no expiration and should be removed manually if needed.
Logic
There is a Main class that starts when the application starts as a Hosted Service, that is responsible for the jobs. Each time it runs (using the PeriodicTimer), it gets a redlock based on the RedisKey provided (in order to avoid multiple Instances to run the jobs simultaneously) and runs the jobs.
Usage
Using net core DI.
var options = new RedisBalancerOptions(
redisKey: "ItemsBalanceKey",
instanceName: "Instance_1",
redisConnectionString: "127.0.0.1:6379",
pingInterval: TimeSpan.FromSeconds(5),
redisKeyExpiration: TimeSpan.FromSeconds(50));
services.AddRedisBalancer(
options,
serviceProvider => serviceProvider.GetRequiredService<IITemsProvider<Item>>(),
serviceProvider =>
{
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
return loggerFactory.CreateLogger("Instance_1-RedisBalancer");
}
Included in the solution is an example project that uses the library.
Installation
Use the Manage Nuget Packages from Visual Studio or install via cli
dotnet add package RedisBalancer
License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 7.0.13)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- RedLock.net (>= 2.3.2)
- StackExchange.Redis (>= 2.7.17)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 158 | 3/30/2024 |