Tooark.Caching.Distributed
5.0.0
dotnet add package Tooark.Caching.Distributed --version 5.0.0
NuGet\Install-Package Tooark.Caching.Distributed -Version 5.0.0
<PackageReference Include="Tooark.Caching.Distributed" Version="5.0.0" />
<PackageVersion Include="Tooark.Caching.Distributed" Version="5.0.0" />
<PackageReference Include="Tooark.Caching.Distributed" />
paket add Tooark.Caching.Distributed --version 5.0.0
#r "nuget: Tooark.Caching.Distributed, 5.0.0"
#:package Tooark.Caching.Distributed@5.0.0
#addin nuget:?package=Tooark.Caching.Distributed&version=5.0.0
#tool nuget:?package=Tooark.Caching.Distributed&version=5.0.0
Tooark.Caching.Distributed
Library that adds the distributed level to Tooark.Caching over one connection string to a RESP server: shared cache, backplane between instances and distributed stampede lock.
With it, the instances of the application share a server β Redis, Valkey, Garnet, Amazon ElastiCache, Azure Cache for
Redis, Google Memorystore β besides the memory of each one: a value loaded by one instance serves the others, a removal
reaches every instance, and only one instance at a time runs the factory of a key. The application code does not
change: it keeps using the ICacheService of
Tooark.Caching.
π Docs: Tooark.Caching.Distributed on the site Β· All packages Β· API reference
π Languages: πΊπΈ English (this file) Β· π§π· PortuguΓͺs
π Contents
- Overview
- Installation
- Configuration
- Servers
- How It Works
- Server Down
- Semantics
- Usage Examples
- Integration Tests
- Dependencies
- Error Codes and Solutions
- Contributing
- Help & Security
- Support
- License
π Overview
| Piece | What it does |
|---|---|
| Distributed level | The entries go to the server too: a value loaded by one instance serves the others |
| Backplane | A write or a removal in one instance drops the memory copy of the others, through pub/sub |
| Distributed lock | Only one instance at a time runs the factory of a key without a value |
AddTooarkCacheDistributed |
Registers the three over one connection, with the options of the Cache:Distributed section |
One connection serves the three. The values go to the server serialized with System.Text.Json. Each instance keeps
the memory level of Tooark.Caching in front of the server, so a hot key does not reach the network at every read.
The package is not part of the Tooark aggregator: it brings the Redis client and a third-party cache engine,
which an application without a distributed cache does not need. Install it on its own.
π§ Installation
dotnet add package Tooark.Caching.Distributed
βοΈ Configuration
appsettings.json
ConnectionString and InstanceName are required. The connection string carries the password: keep it in a secret
store or an environment variable (Cache__Distributed__ConnectionString), never in a versioned appsettings.json.
{
"Cache": {
"DefaultDurationSeconds": 300,
"Distributed": {
"ConnectionString": "set it in a secret store or in Cache__Distributed__ConnectionString",
"InstanceName": "orders-prod",
"Backplane": true,
"DistributedLock": true,
"TimeoutMilliseconds": 1000,
"LockTimeoutMilliseconds": 5000,
"CircuitBreakerSeconds": 5
}
}
}
Program.cs
using Tooark.Caching.Distributed.Injections;
using Tooark.Caching.Injections;
builder.Services.AddTooarkCache(builder.Configuration);
builder.Services.AddTooarkCacheDistributed(builder.Configuration);
Call it after AddTooarkCache: without the cache, it fails at startup with Cache.NotRegistered. The options are
read with the strict configuration and validated at registration: an
unknown key, a value out of range or an unreadable connection string fails at startup. The connection is not opened at
registration but when the cache first uses it, and the application starts with the server down. Only the first
registration counts.
| Registration | What it registers |
|---|---|
AddTooarkCacheDistributed(configuration, configure?) |
The options of Cache:Distributed, the connection, the distributed level, the backplane and the lock |
AddTooarkCacheDistributed(configure) |
The same, without reading the configuration; configure sets ConnectionString and InstanceName |
Options β Cache:Distributed
| Key | Default | Description |
|---|---|---|
ConnectionString |
(none) | Connection string of the server, in the StackExchange.Redis format: host:6379,password=...,ssl=true. A secret. Without abortConnect in it, Tooark uses abortConnect=false |
InstanceName |
(none) | Name of the application in the server: it prefixes every key and names the backplane channel. Up to 64 letters, digits, ., -, _ or :, starting and ending with a letter or a digit |
Backplane |
true |
Drops the memory copy of the other instances on each write or removal. Off, a copy keeps being served until it expires, even after being removed in another instance, and a removal by tag reaches the other instances within 5 seconds |
DistributedLock |
true |
Only one instance at a time runs the factory of a key. Off, the stampede protection holds only inside each instance. Needs Lua scripts in the server |
TimeoutMilliseconds |
1000 |
Time limit of each operation in the server, 100 to 60000. A read past it counts as a server failure: the instance goes on with its memory and, without a value, runs the factory |
LockTimeoutMilliseconds |
5000 |
How long an instance waits for the factory of another before running its own, without the lock, 100 to 60000 |
CircuitBreakerSeconds |
5 |
How long the instance stops using the server after a failure, going on with its memory only, 0 to 3600. 0 turns the circuit off: every call tries the server |
InstanceName. Instances with the same name share the entries and the backplane; different names in the same
server do not see each other. Use the same name in every instance of the application β the API and the worker that read
the same data β and one name per application and per environment (orders-prod, orders-staging). It has no default
because a server shared by two applications without the name would mix their keys. Changing it leaves the cache empty,
as a new server.
π Servers
Any server that speaks RESP and supports the commands of each feature. Memcached is out: it has no pub/sub.
| Feature | Needs in the server | Turn off with |
|---|---|---|
| Distributed level | Hashes (HSET, HGET, HMGET) and EXPIRE |
(required) |
| Backplane | Pub/sub (PUBLISH, SUBSCRIBE) |
Backplane: false |
| Distributed lock | Lua scripts (EVAL, EVALSHA) |
DistributedLock: false |
| Server | Notes |
|---|---|
| Redis, Valkey | Every feature |
| Garnet | The lock needs the server started with Lua (--lua); otherwise, set DistributedLock: false |
| Amazon ElastiCache (Redis OSS, Valkey) | ssl=true with encryption in transit; the cluster mode works with the cluster configuration endpoint |
| Azure Cache for Redis / Azure Managed Redis | ssl=true, port 6380 (Azure Cache) or 10000 (Managed Redis), with the access key as password |
| Google Memorystore (Redis, Valkey) | ssl=true with in-transit encryption, and the password of the AUTH string |
Each operation touches a single key, so the cluster mode of the managed services works. Authentication by password or access key goes in the connection string; token authentication (Microsoft Entra ID, AWS IAM) is not covered.
redis-1:6379,password=secret,ssl=true # Redis / Valkey with TLS
my-cache.redis.cache.windows.net:6380,password=access-key,ssl=true # Azure Cache for Redis
clustercfg.my-cache.abc123.use1.cache.amazonaws.com:6379,ssl=true,password=token # ElastiCache, cluster mode
The package is tested without a server, through its wiring, and against a real server only in the opt-in integration tests. Validate the managed service you use with them.
π How It Works
- Reading: the memory of the instance first; without the value there, the server; without it in the server, the factory, with the stampede protection inside the instance and, with the lock, between instances.
- Writing: the memory and the server, before the call returns, and a message on the backplane channel, which makes the other instances drop their memory copy and read the server.
- Removing: the memory, the server and the backplane. With the backplane, removing by tag is valid at once in every instance; without it, within 5 seconds.
- Keys: every key in the server has
{InstanceName}:in front of the scope ofTooark.Caching(g:shared,t:{tenant}:of a tenant) and of the key of the application, after a marker of the engine's own format. The keys of the tags and of the lock follow the same prefix. - Values: serialized with
System.Text.Json. An entry that no longer deserializes β its type changed between two deploys β is read as missing and written again, instead of failing the call.
π Server Down
The cache does not take the application down with it:
| What fails | What happens |
|---|---|
| The server at startup | The application starts; the connection is retried in the background (abortConnect=false) |
| The connection, during operation | The commands fail at once, with no waiting queue; the instance goes on with its memory and, without a value, the factory |
| An operation past the time limit | Counts as a failure of the server, with the same effect |
| After a failure | The circuit opens for CircuitBreakerSeconds: the instance does not try the server at each call |
| The lock | The instance runs the factory without it |
| The backplane | The instances keep their memory copies until they expire |
While the server is down, each instance serves its own memory, and the instances may diverge until it comes back and
the entries expire. The fail-safe of Tooark.Caching keeps
working on the memory level.
π§ Semantics
The semantics of Tooark.Caching hold, plus:
- The values make a round trip through JSON. A value read from the server is a new object, built by
System.Text.Json: public properties with a setter or a constructor with the same names (records), or[JsonInclude]. A property with a private setter comes back with its default value. - The memory copy lives until it expires or the backplane drops it. Without the backplane, an instance keeps serving its copy after another instance removed the key, until the duration of the entry ends. A removal by tag reaches it within 5 seconds, when it reads the mark of the tag from the server again.
- The server keeps the entries for the fail-safe too. With the fail-safe of
Tooark.Caching, each entry stays in the server forFailSafe:MaxStaleSecondsbeyond its duration. Size the memory of the server with that time. - The Bloom filter stays per instance. The distributed level does not share it: see the Bloom filter.
π Usage Examples
API with Valkey or Redis
using Tooark.Caching.Distributed.Injections;
using Tooark.Caching.Injections;
// "Cache": { "DefaultDurationSeconds": 300, "Distributed": { "InstanceName": "orders-prod" } }
// Cache__Distributed__ConnectionString=valkey:6379,password=...,ssl=true (environment variable or secret store)
builder.Services.AddTooarkCache(builder.Configuration);
builder.Services.AddTooarkCacheDistributed(builder.Configuration);
The services and handlers do not change: they use ICacheService as in the
examples of Tooark.Caching.
Local development with Docker
docker run -d --name valkey -p 6379:6379 valkey/valkey:8
{
"Cache": {
"DefaultDurationSeconds": 60,
"Distributed": {
"ConnectionString": "localhost:6379",
"InstanceName": "orders-dev"
}
}
}
Connection string from a secret store
using Tooark.Secrets.Aws.Injections;
// "Secrets": { "SecretsPrefix": "orders/prod" }: the secret orders/prod/Cache/Distributed/ConnectionString becomes the
// key Cache:Distributed:ConnectionString of the configuration
builder.Configuration.AddTooarkSecretsAws();
builder.Services.AddTooarkCache(builder.Configuration);
builder.Services.AddTooarkCacheDistributed(builder.Configuration);
Garnet without Lua
builder.Services.AddTooarkCacheDistributed(builder.Configuration, options => options.DistributedLock = false);
π§ͺ Integration Tests
The test project has an Integration category that runs against a real server only when the TOOARK_TEST_REDIS
environment variable has its connection string; without it, the tests show up as skipped.
docker run -d --name valkey -p 6379:6379 valkey/valkey:8
TOOARK_TEST_REDIS=localhost:6379 dotnet test --project Tooark.Tests/Tooark.Tests.csproj -- --filter-trait "Category=Integration"
In GitHub Actions, a job can start Valkey as a service container:
integration-cache:
runs-on: ubuntu-latest
services:
valkey:
image: valkey/valkey:8
ports: ["6379:6379"]
options: >-
--health-cmd "valkey-cli ping" --health-interval 2s --health-timeout 2s --health-retries 15
env:
TOOARK_TEST_REDIS: localhost:6379
steps:
- uses: actions/checkout@v7
- uses: actions/setup-dotnet@v6
with:
dotnet-version: 10.0.x
- run: dotnet test --project Tooark.Tests/Tooark.Tests.csproj --configuration Release -- --filter-trait "Category=Integration"
They cover the value shared through the server, the removal and the removal by tag that reach the other instance through the backplane, the lock that runs the factory once across two instances, the cached "not found" and the separation by instance name.
π Dependencies
| Package | Version | Description |
|---|---|---|
Tooark.Caching |
5.x | The cache and its engine |
Tooark.Exceptions |
5.x | Configuration errors |
Microsoft.Extensions.Caching.StackExchangeRedis |
10.x | The distributed level over the server |
StackExchange.Redis |
3.4.x | The connection with the server |
ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis |
2.10.x | The backplane over pub/sub |
ZiggyCreatures.FusionCache.Locking.Distributed.Redis |
2.10.x | The distributed lock (with DistributedLock.Redis) |
ZiggyCreatures.FusionCache.Serialization.SystemTextJson |
2.10.x | The serialization of the values |
β οΈ Error Codes and Solutions
The errors are InternalServerErrorException thrown at registration (AddTooarkCacheDistributed):
| Message | Description | Solution |
|---|---|---|
Cache.NotRegistered;AddTooarkCacheDistributed |
AddTooarkCache was not called before |
Call AddTooarkCache first |
Options.Cache.Distributed.ConnectionStringRequired |
Cache:Distributed:ConnectionString is not configured |
Set the connection string, from a secret store or environment variable |
Options.Cache.Distributed.ConnectionStringInvalid |
The connection string cannot be read, or has no host. The value is not shown | Use the StackExchange.Redis format: host:6379,password=...,ssl=true |
Options.Cache.Distributed.InstanceNameRequired |
Cache:Distributed:InstanceName is not configured |
Set the name of the application, such as orders-prod |
Options.Cache.Distributed.InstanceNameInvalid;{value} |
The name has another character, more than 64, or starts or ends with a symbol | Use letters, digits, ., -, _ and : |
Options.Cache.Distributed.TimeoutInvalid;{min};{max} |
TimeoutMilliseconds outside 100 to 60000 |
Use a value in the range |
Options.Cache.Distributed.LockTimeoutInvalid;{min};{max} |
LockTimeoutMilliseconds outside 100 to 60000 |
Use a value in the range |
Options.Cache.Distributed.CircuitBreakerInvalid;{min};{max} |
CircuitBreakerSeconds outside 0 to 3600 |
Use a value in the range; 0 turns the circuit off |
Options.Configuration.UnknownKey;Cache:Distributed;{key} |
Unknown key in Cache:Distributed, with strict configuration on |
Fix the key name, or remove it from the section |
π€ Contributing
Contributions are welcome! Start with CONTRIBUTING.md β it covers the development workflow, the coding and commit conventions and the pull request checklist. Bugs and feature requests go through the issue templates of the Tooark repository.
By participating you agree to the Code of Conduct.
π Help & Security
- β Questions, bugs, feature ideas β see SUPPORT.md for the right channel
- π Security vulnerabilities β do not open a public issue; follow SECURITY.md
π Support
If Tooark helps your projects, consider supporting its development:
- π GitHub Sponsors
- β Ko-fi
Every contribution helps keep the project maintained and improving. Thank you! π
π License
This project is licensed under the BSD 3-Clause License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.Caching.Memory (>= 10.0.15)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 10.0.15)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.15)
- Microsoft.Extensions.Localization (>= 10.0.15)
- Microsoft.Extensions.Options (>= 10.0.15)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.15)
- StackExchange.Redis (>= 3.4.35)
- Tooark.Caching (>= 5.0.0)
- Tooark.Exceptions (>= 5.0.0)
- ZiggyCreatures.FusionCache (>= 2.10.0)
- ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis (>= 2.10.0)
- ZiggyCreatures.FusionCache.Locking.Distributed.Redis (>= 2.10.0)
- ZiggyCreatures.FusionCache.Serialization.SystemTextJson (>= 2.10.0)
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 |
|---|---|---|
| 5.0.0 | 0 | 12/1/2026 |