Skip to content

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1,430 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

NCode Group

NCode.Coordination

Coordination for the nodes of a cluster: sessions, leases, records, counters and notifications over interchangeable providers with declared guarantees.

NuGet version NuGet downloads CI Target framework License OpenSSF Scorecard

Coordination for the nodes of a cluster, for .NET: who is alive, who owns what, what happened once, what changed, and how much was used. One programming model over interchangeable providers, each speaking a standard protocol, each declaring the guarantees it keeps and proving them against one conformance kit.

NCode.Coordination is not a database, a cache or a message broker. It holds the small, often short-lived state that the nodes of one deployment must agree on, and leaves durable data to a database and durable messaging to a broker.

Primitive Answers Familiar from
Sessions & membership Which nodes are alive, where they are, and what dies with a node ZooKeeper sessions, Consul catalog, Orleans
Partition ownership Which node owns each partition of a workload Akka cluster sharding, rendezvous hashing
Leases & semaphores Who owns a resource now, proven by a fencing token Chubby locks, etcd leases
Elections & claims Which node leads a role, or runs this interval's pass Akka cluster singleton, ZooKeeper recipes
Records A small versioned value, changed by compare-and-swap etcd revisions, Consul modify index
Transactions Several records changed together, on conditions etcd transactions, ZooKeeper multi
Sequences Unique, increasing numbers across the cluster ZooKeeper sequential nodes
Occurrences Whether something already happened, at most once Replay caches
Sets Which members belong to a key, each with its own expiry Secondary indexes
Counters How much was used in a window, checked against limits Rate limiters
Notifications What changed, with an explicit signal when a node missed it etcd watches, ZooKeeper watches

The design is in docs/designs/coordination.md. NCode.Coordination is an original work, derived from no other project; ORIGINALITY.md says how it stays one.

Issues and discussion are welcome, and code is by invitation while the design settles, as CONTRIBUTING.md says.

Installation

Add the package to a project:

dotnet add package NCode.Coordination

A cluster's nodes share a server through a provider: add NCode.Coordination.Resp for Valkey or Garnet, or NCode.Coordination.PostgreSql for PostgreSQL. Add NCode.Coordination.Hosting for a node that holds a session and joins its cluster's membership for as long as it runs.

dotnet add package NCode.Coordination.Resp
dotnet add package NCode.Coordination.Hosting

Quick start

Compose coordination on a host with the in-process provider, which keeps every primitive in the process's memory, for one node or a test, and take a lease on a resource:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NCode.Coordination;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddCoordination(coordination => coordination
    .AddInProcessProvider()
    .Bind(new ProviderBinding(InProcessCoordinationBuilderExtensions.ProviderName)));

using var host = builder.Build();
await host.StartAsync();

var leases = host.Services.GetRequiredService<ILeaseService>();
var resource = new Keyspace("orders").CreateKey("invoice-run");
var terms = LeaseTerms.ForTimeToLive(new TimeToLive(TimeSpan.FromSeconds(30)));

var result = await leases.TryAcquireAsync(resource, terms, null, CancellationToken.None);
if (result.IsAcquired)
{
    // Send result.Grant.FencingToken with each write the work makes, so the store refuses a holder whose lease ended.
    await leases.ReleaseAsync(result.Grant, null, CancellationToken.None);
}

await host.StopAsync();

The host's start checks every binding and requirement against what each provider declares, and fails with every problem listed before any hosted service starts.

Usage

Bind a provider

A host binds each provider to every primitive it supports, to some of them, or to a keyspace and the keyspaces under it, and each key goes to the most specific binding:

builder.Services.AddCoordination(coordination => coordination
    .AddRespProvider(resp => resp.Configuration = "valkey.internal:6379,ssl=true,password=...")
    .AddPostgreSqlProvider(postgresql => postgresql.ConnectionString =
        "Host=db.internal;Database=app;Username=coordination;Password=...;SSL Mode=VerifyFull")
    .Bind(new ProviderBinding(RespCoordinationBuilderExtensions.ProviderName))
    .Bind(new ProviderBinding(PostgreSqlCoordinationBuilderExtensions.ProviderName)
    {
        Keyspace = new Keyspace("billing"),
    }));

Each provider's readme says what it keeps and what its server needs: NCode.Coordination.Resp and NCode.Coordination.PostgreSql.

Require what a feature relies on

A recipe names the primitives and guarantees it relies on. Require it, and the host's start fails when the binding can't keep them:

builder.Services.AddCoordination(coordination => coordination
    .AddRespProvider(resp => resp.Configuration = "valkey.internal:6379,ssl=true")
    .Bind(new ProviderBinding(RespCoordinationBuilderExtensions.ProviderName))
    .RequireRecipe(CoordinationRecipes.Lock, "invoicing"));
var locks = host.Services.GetRequiredService<ILockService>();
var grant = await locks.AcquireAsync(resource, terms, null, cancellationToken);

AcquireAsync waits until the lock is free, and returns null once the session its terms bind it to has ended. CoordinationRecipes lists every recipe: locks, read-write locks, barriers, singletons, partition ownership, single-flight, fixed-window, sliding-window and token-bucket rate limiters, and cache invalidation.

Run a node

NCode.Coordination.Hosting holds a session for the node's lifetime and joins its cluster's membership with it:

builder.Services.AddCoordinationHosting(builder.Configuration.GetSection(CoordinationHostOptions.SectionName));
builder.Services.AddCoordination(coordination => coordination
    .AddRespProvider(resp => resp.Configuration = "valkey.internal:6379,ssl=true")
    .Bind(new ProviderBinding(RespCoordinationBuilderExtensions.ProviderName))
    .RequireHostSession());
var sessions = host.Services.GetRequiredService<IHostSessionService>();
var session = await sessions.WaitForSessionAsync(cancellationToken);
var terms = LeaseTerms.ForSession(session.Id);

The Coordination:Host section names the node's cluster and node id, and the host refuses to start with options that aren't valid. A lease bound to the session ends with it, when the node stops or loses its session.

Check a provider with the conformance kit

NCode.Coordination.Testing holds a contract suite for each primitive. A provider's xUnit tests derive a sealed class from each suite and give it the provider, and, where they can, a way to advance its clock and to drop its connections:

public sealed class ValkeyLeaseContractTests : LeaseContractTests
{
    protected override ValueTask<ConformanceTarget> CreateTargetAsync(CancellationToken cancellationToken) =>
        ValueTask.FromResult(new ConformanceTarget(CreateProvider()));
}

Development

Run from the repository root in PowerShell 7:

Task Command
Build dotnet build
Test dotnet test

AI-Assisted Workflow

This repository is set up for plan-then-execute coding: Claude Opus plans each change into PLAN.md, a cheaper model executes it one verified step at a time, and any step that needs a decision goes back to Opus. AGENTS.md is the index of the instructions every agent follows.

Work in Claude Code or GitHub Copilot, and switch whenever you like: both have the same commands, and PLAN.md is the only state they share, so one can plan and the other execute. Each bills its own subscription.

Tool Plan Execute Re-plan one step Review Land, then finish
Claude Code /plan-feature <what to build> (or /plan-bugfix, /plan-refactor, /plan-tests, …), or /plan-issue #42 for an issue, then review PLAN.md /orchestrate-plan /replan-step N /review-plan-diff /ship-plan, then /finish-plan
Copilot Chat in VS Code The Copilot Planner agent on Opus, or /plan-feature or /plan-issue #42 Run all steps with subagents, or Execute next step Re-plan with the planner Review the work /ship-plan, then /finish-plan
Copilot CLI copilot --model claude-opus-5.5, then /plan-feature or /plan-issue #42 /orchestrate-plan /replan-step N /review-plan-diff /ship-plan, then /finish-plan

In Claude Code, plan in Accept edits mode and execute in Auto.

Each plan gets its own git worktree under .claude/worktrees/, in every tool, so several sessions can work on different plans at once.

The workflow files come from a Copier template, which owns them: never edit .template/, .template-version, .githooks/pre-push, .github/instructions/project-template.instructions.md, .copier-answers.yml, CLAUDE.md or the commands, agents and skills the template generates. To change how agents work here, edit AGENTS.md or a file it lists. To pull in template improvements, run .\.template\Update-Project.ps1: it updates itself, replaces the template's files, merges template changes into the shared settings files, and never touches AGENTS.md, and in this README renders again only the header between <!-- template:readme-header --> and <!-- /template:readme-header -->, from the answers: change it with .\.template\Update-Project.ps1 -Data, never by editing inside the markers. To plan the update first, with a trial run, the project files that need changing and the checks, run /plan-template-update. The weekly template-update workflow opens an issue, labeled template-update, when a newer template version is out, and closes it once the project has updated; /plan-issue plans it.

Project

  • Issues: report a bug or ask for a feature.
  • CONTRIBUTING.md: how to set up, build, test and propose a change.
  • SECURITY.md: how to report a vulnerability privately.
  • CODE_OF_CONDUCT.md, GOVERNANCE.md and ROADMAP.md: how we work together, who decides, and where the project is going.
  • docs/architecture.md and docs/assurance-case.md: how the project is built, and why it meets its security requirements.
  • docs/decisions/: the decisions behind the design, the project's and the template's.
  • docs/replies.md: the replies the maintainers post to people outside the project.
  • CHANGELOG.md: in the Keep a Changelog format, what the product does until its first stable release, while AGENTS.md's Landing section says pre-release: untracked, and what changed in each release from then on. The first plan creates it.
  • LICENSE.txt: the license.

License

NCode.Coordination is licensed under the Apache License 2.0.

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages