Soenneker.Sets.Concurrent.Bounded
4.0.15
Prefix Reserved
dotnet add package Soenneker.Sets.Concurrent.Bounded --version 4.0.15
NuGet\Install-Package Soenneker.Sets.Concurrent.Bounded -Version 4.0.15
<PackageReference Include="Soenneker.Sets.Concurrent.Bounded" Version="4.0.15" />
<PackageVersion Include="Soenneker.Sets.Concurrent.Bounded" Version="4.0.15" />
<PackageReference Include="Soenneker.Sets.Concurrent.Bounded" />
paket add Soenneker.Sets.Concurrent.Bounded --version 4.0.15
#r "nuget: Soenneker.Sets.Concurrent.Bounded, 4.0.15"
#:package Soenneker.Sets.Concurrent.Bounded@4.0.15
#addin nuget:?package=Soenneker.Sets.Concurrent.Bounded&version=4.0.15
#tool nuget:?package=Soenneker.Sets.Concurrent.Bounded&version=4.0.15
Soenneker.Sets.Concurrent.Bounded
A high-performance bounded concurrent set for .NET
Soenneker.Sets.Concurrent.Bounded provides a thread-safe concurrent set with an approximate maximum size.
It behaves similarly to a ConcurrentDictionary-backed set but automatically evicts older entries when the set grows beyond a configured limit.
Designed for high-throughput workloads where preventing unbounded memory growth is more important than strict eviction guarantees.
Typical uses include:
- deduplication caches
- request ID tracking
- preventing duplicate messages
- bounded concurrent caches
- rate limiting helpers
- abuse protection
- event stream deduplication
Installation
dotnet add package Soenneker.Sets.Concurrent.Bounded
Why this library exists
The .NET ecosystem lacks a simple bounded concurrent set.
Existing options have tradeoffs:
| Collection | Problem |
|---|---|
ConcurrentDictionary |
Unbounded growth |
HashSet |
Not thread-safe |
MemoryCache |
Heavy for simple dedupe use cases |
| LRU caches | Often require locks or background eviction |
BoundedConcurrentSet provides a lightweight alternative optimized for:
- high concurrency
- predictable memory usage
- low allocation
- minimal locking
Features
✔ High-throughput concurrent operations ✔ Approximate size bounding ✔ Opportunistic eviction of older entries ✔ Lock-minimized design ✔ Low allocation footprint ✔ Safe for heavy multi-threaded workloads
Example
using Soenneker.Sets.Concurrent.Bounded;
var set = new BoundedConcurrentSet<string>(maxSize: 1000);
set.TryAdd("alpha");
set.TryAdd("beta");
bool exists = set.Contains("alpha");
set.TryRemove("beta");
string[] snapshot = set.ToArray();
Configuration
var set = new BoundedConcurrentSet<string>(
maxSize: 1000,
capacityHint: 1000,
trimBatchSize: 64,
trimStartOveragePercent: 5
);
| Parameter | Description |
|---|---|
maxSize |
Target maximum number of elements |
capacityHint |
Initial capacity hint for the dictionary |
trimBatchSize |
Number of eviction attempts per trim cycle |
trimStartOveragePercent |
Allowed overage before trimming begins |
Performance Characteristics
| Operation | Complexity |
|---|---|
TryAdd |
O(1) average |
Contains |
O(1) |
TryRemove |
O(1) |
Eviction work is bounded per call, preventing long latency spikes.
Internally the structure uses:
- concurrent dictionary indexing
- opportunistic FIFO-like eviction
- generation tokens to avoid stale scans
- atomic counters for approximate size tracking
When to use this
Use this collection when you need:
- a bounded concurrent set
- deduplication tracking
- a lightweight concurrent cache
- to prevent unbounded memory growth
Good examples:
- deduplicating phone numbers
- tracking recent request IDs
- preventing duplicate events
- guarding against repeated API calls
Thread Safety
All operations are fully thread-safe.
The implementation is designed for high-concurrency environments and does not require external locking.
| 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
- Soenneker.Atomics.Longs (>= 4.0.13)
- Soenneker.Atomics.ValueInts (>= 4.0.16)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Soenneker.Sets.Concurrent.Bounded:
| Package | Downloads |
|---|---|
|
Soenneker.Deduplication.Bounded
A thread-safe high-performance bounded size deduplication utility for .NET. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.15 | 96 | 3/13/2026 |
| 4.0.14 | 33 | 3/13/2026 |
| 4.0.13 | 31 | 3/12/2026 |
| 4.0.12 | 34 | 3/12/2026 |
| 4.0.11 | 31 | 3/12/2026 |
| 4.0.10 | 262 | 3/11/2026 |
| 4.0.9 | 41 | 3/11/2026 |
| 4.0.8 | 38 | 3/10/2026 |
| 4.0.7 | 148 | 3/10/2026 |
| 4.0.6 | 71 | 3/10/2026 |
| 4.0.5 | 77 | 3/9/2026 |
| 4.0.4 | 78 | 3/9/2026 |
| 4.0.3 | 77 | 3/9/2026 |
| 4.0.2 | 273 | 3/5/2026 |
| 4.0.1 | 84 | 3/5/2026 |