Skip to content

Reduce subscriber contention with bounded spinning - #18

Open
prezaei wants to merge 6 commits into
mainfrom
v2
Open

Reduce subscriber contention with bounded spinning#18
prezaei wants to merge 6 commits into
mainfrom
v2

Conversation

@prezaei

@prezaei prezaei commented Nov 28, 2022

Copy link
Copy Markdown
Member

When subscribers contend for the read lock, blocking Dequeue currently yields and progresses to semaphore waits of up to 10 ms. Use bounded SpinWait retries before a 5 ms semaphore wait, resetting the spin budget after waiting. TryDequeue still makes one read-lock attempt, and the existing publisher/subscriber crash recovery remains intact. Also observe cancellation while waiting for a publisher to finish writing a reserved message.

Add eight regression cases covering contended reads, expired read locks, cancellation, and exactly-once delivery with concurrent publishers and subscribers. Add a reproducible concurrent-subscriber benchmark, support benchmark selection from the command line, and update existing benchmark jobs to .NET 10. This branch includes current main and the queue lifetime fix.

Validation:

BenchmarkDotNet comparison against main, with one publisher sending batches of 65,536 eight-byte messages. Values are mean time per message; lower is better.

Environment Subscribers main This PR
Linux arm64 container, .NET 10 1 134.6 ns 135.8 ns
Linux arm64 container, .NET 10 4 546.5 ns 199.7 ns
macOS M5 Max, .NET 10 1 253.3 ns 241.0 ns
macOS M5 Max, .NET 10 4 1,505.4 ns 342.0 ns

Linux used three warmup and eight measured iterations; macOS used three of each. These are local measurements, not general latency guarantees; the macOS contended baseline was noisy. Linux shows about 2.7x throughput with four subscribers and essentially unchanged single-subscriber throughput.

Reproduce from the repository root:

dotnet run --project src/Interprocess.Benchmark -c Release -- --filter '*SubscriberBenchmark*' --iterationCount 8

@prezaei prezaei changed the title Faster Susbscriber Reduce subscriber contention with bounded spinning Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant