Skip to content

Latest commit

 

History

History
75 lines (50 loc) · 2.55 KB

README.md

File metadata and controls

75 lines (50 loc) · 2.55 KB

JitterMagic

A c# library to add jitter to cache durations and retry intervals to increase entropy in your system and prevent thundering herds

Build Status Version License

Why?

The library is inspired by YouTube's strategy to add entropy to large systems by adding randomness to cache expiry durations

Usage

Use it with either the default percentage of 25

int cacheDuration = Jitter.Apply(1000);

or by supplying a settings object with different percentage yourself

int cacheDuration = Jitter.Apply(1000, new JitterSettings(percentage: 50));

Also works for doubles

double cacheDuration = Jitter.Apply(1000D);

or by supplying different settings yourself

double cacheDuration = Jitter.Apply(1000D, new JitterSettings(percentage: 50));

Changing settings

The following default settings are used and can be changed by using the code below with different values

Jitter.UpdateSettings(new JitterSettings(percentage: 25));

Non-static usage

If you wish to be able to inject it - for example for having different percentages in different places - you can use the JitterInstance class:

IJitterInstance instance = new JitterInstance(new JitterSettings(percentage: 25));

This interface and class only has the Apply methods without settings, because you already provide those during construction.

So it provides the following function for integers

int cacheDuration = instance.Apply(1000);

And also works for doubles

double cacheDuration = instance.Apply(1000D);

Get it

First, install NuGet. Then, install JitterMagic from the package manager console:

PM> Install-Package JitterMagic

JitterMagic is Copyright © 2015 Jorrit Salverda and other contributors under the MIT license.