RedisBalancer 1.0.0

dotnet add package RedisBalancer --version 1.0.0
                    
NuGet\Install-Package RedisBalancer -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RedisBalancer" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RedisBalancer" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RedisBalancer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RedisBalancer --version 1.0.0
                    
#r "nuget: RedisBalancer, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RedisBalancer@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RedisBalancer&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RedisBalancer&version=1.0.0
                    
Install as a Cake Tool

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.

  1. Ping: Informs that this Instance is active
  2. RemoveInactiveInstances: Checks for any Instance that has outdated Ping time and removes it.
  3. 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:

  1. 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.
  2. InstanceName [required]: A string which is used for the name of the Instance.
  3. RedisConnectionString [required]: A string which is used for the connection string of the redis to be connected to.
  4. PingInterval [required]: A TimeSpan which is used for the Ping job interval.
  5. RemoveInactiveInstancesInterval: A TimeSpan which is used for the RemoveInactiveInstances job interval. If not set then RemoveInactiveInstancesInterval = PingInterval * 2.
  6. DistributeItemsInterval: A TimeSpan which is used for the DistributeItems job interval. If not set then DistributeItemsInterval = PingInterval * 2.
  7. InstanceInactivityPeriod: A TimeSpan used by the RemoveInactiveInstances job, in order to determine the outdated Instances. If not set then InstanceInactivityPeriod = PingInterval * 3.
  8. 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

MIT

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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