skip to content

As a C# lead, would you make ConcurrentDictionary or a lock around Dictionary<TKey,TValue> the team default for shared mutable maps, and why?

level: principalimportance: should knowfreq 30%

answer

  1. the shape of the operations
  2. lock-free reads, striped writes
  3. operations that take every lock
  4. invariants across entries

basics

~20 s

Default to ConcurrentDictionary for maps that many threads read and update one key at a time. Choose a lock around a plain Dictionary when an operation spans several entries, needs a consistent snapshot, or the map is small and rarely touched.

solid answer

~50 s

The choice turns on the shape of the operations. `ConcurrentDictionary<TKey,TValue>` wins when many threads read and update independent keys: `TryGetValue` takes no lock, a writer locks only the stripe holding its key (one lock per processor by default, growing up to 1,024), and `GetOrAdd`, `AddOrUpdate` and `TryUpdate` cover single-key atomic changes. Its costs are a node object per entry, factories that may run more than once, `Count`, `Keys`, `Values`, `ToArray` and `Clear` taking every lock, and enumeration that is not a snapshot. A lock around a plain `Dictionary` buys real multi-step atomicity and consistent views by serialising everything, which is fine for a small or cold map. A workable convention: `ConcurrentDictionary` for per-key caches and counters, a private lock wherever an invariant crosses entries, and an immutable or frozen snapshot for read-mostly data. Write the reasons down.

code

csharp · 17 lines
csharp
using System.Collections.Concurrent;

var limits = new ConcurrentDictionary<int, int>();
for (int i = 0; i < 1_000; i++) limits[i] = i;

var writer = Task.Run(() =>
{
    for (int i = 1_000; i < 200_000; i++) limits[i] = i;
});
int seen = 0;
foreach (var pair in limits) seen++;    // no exception while the writer runs
await writer;
Console.WriteLine($"enumerated {seen}, now {limits.Count}");

ICollection<int> keys = limits.Keys;    // a copy, taken under every lock
limits[-1] = -1;
Console.WriteLine($"keys copy {keys.Count}, dictionary {limits.Count}");

go deeper

for a junior

Know both options exist and are both correct: ConcurrentDictionary for shared per-key access, a lock around Dictionary when several steps must happen together.

for a middle

Explain the mechanics behind the choice: lock-free reads, striped writes, members that take every lock, and enumeration that is not a snapshot.

for a senior

Match each shared map to its access pattern, and catch the costly cases in review: Count on hot paths, multi-key updates spread over separate calls, mutable values.

for a principal

Own the convention and its exceptions: frozen snapshots for read-mostly data, ConcurrentDictionary for per-key state, locks for invariants, and measurement before anyone overrides it for speed.

## The decision is about operations, not "thread-safe or not" Both options are correct when used as designed. A `Dictionary<TKey,TValue>` with every access inside `lock (_gate)` is thread-safe; so is a `ConcurrentDictionary<TKey,TValue>`. They differ in **which operations are atomic** and **what concurrency costs**. | Question | `lock` + `Dictionary` | `ConcurrentDictionary` | |---|---|---| | Reads | serialised with every writer | lock-free | | Writes to different keys | serialised | parallel unless they share a stripe | | Multi-key or multi-collection change | atomic inside one lock | not atomic: needs your own lock | | `Count`, `Keys`, snapshot | consistent, under your lock | `Count`, `Keys`, `Values`, `ToArray` take every stripe lock | | `foreach` while others write | impossible without holding the lock | allowed; not a snapshot | | Memory per entry | a struct in one array | a heap `Node` object per entry | | Value factories | run once, under your lock | may run several times, outside the lock | ## Where ConcurrentDictionary wins - **Hot, read-heavy, per-key access.** A cache of per-tenant settings read on every request: lookups never block, and the .NET guidance says that with many reads and many updates `ConcurrentDictionary` "generally is significantly faster". - **Independent single-key updates.** Counters with `AddOrUpdate`, registrations with `TryAdd`, conditional changes with `TryUpdate`, removals with `TryRemove`. - **Code you do not want deadlocking.** User delegates run outside its locks, so a callback cannot hold up other writers. ## What ConcurrentDictionary costs 1. **Global operations take every lock.** In the .NET 10 source, `Count`, `Keys`, `Values`, `ToArray`, `Clear` and `CopyTo` all call `AcquireAllLocks`. A health endpoint that reads `Count` on every call stalls every writer while it runs. `IsEmpty` checks the buckets without locks first and takes them all only when it finds nothing. 2. **Enumeration is not a snapshot.** The source remarks say the enumerator "does not represent a moment-in-time snapshot": a report built with `foreach` may mix states from before and after concurrent writes. 3. **Allocation per entry.** Each entry is a `Node` object, where `Dictionary` packs entries into one struct array: more objects for the garbage collector on large maps. 4. **Factories race.** `GetOrAdd` and `AddOrUpdate` may run their delegates more than once. 5. **It protects the map, not the values.** A mutable object stored as a value is as unsafe as ever. The constructor's `concurrencyLevel` sets the number of stripes, `Environment.ProcessorCount` by default. More stripes reduce write contention and make every global operation more expensive, and the docs advise avoiding global operations if you raise it. ## Where a lock around Dictionary wins - **Invariants across entries or collections:** a quota transfer between two tenants, a dictionary kept in step with a queue, an index that must match a list. - **Consistent reads of several entries,** or a snapshot for a report. - **Small or cold maps,** where an uncontended lock costs little, and a single `Dictionary` is simpler to reason about and lighter in memory. - **Compound logic** that would otherwise be several `ConcurrentDictionary` calls plus your own lock anyway. How `lock` behaves, and C# 13's `System.Threading.Lock`, belong with locks and `Interlocked`. ## A convention a team can enforce 1. **Read-mostly data that changes rarely:** build a new `Dictionary` or a `FrozenDictionary` (.NET 8) and publish it by swapping one reference; readers take no lock at all. The details sit with immutable and frozen collections. 2. **Per-key caches and counters touched by many threads:** `ConcurrentDictionary`, with `Lazy<TValue>` values when a factory is expensive or has side effects, and pure `AddOrUpdate` delegates. 3. **Anything with a cross-entry invariant:** a private lock object around a plain `Dictionary`, with all access through one small class. 4. **Review flags:** `Count`, `Keys` or `ToArray` on hot paths; read-then-write pairs on a concurrent collection; mutable values stored in a `ConcurrentDictionary`. Measure before overriding the default on performance grounds, since the .NET guidance itself says results vary with workload and core count. ## The gateway as a worked case The gateway's per-tenant HTTP client settings are a per-key cache read on every request: `ConcurrentDictionary<string, Lazy<TenantSettings>>`. Its rate-limit counters are independent per tenant: `ConcurrentDictionary<string, int>` with `AddOrUpdate`. A feature that lets an admin move request quota between two tenants crosses entries: put quotas behind a lock. The admin page that lists every tenant's usage should call `ToArray()` once per page view, not `Count` per request.

  • When would neither option be the right default?
    When data is read constantly and replaced rarely, such as configuration or feature rules. Build a new `Dictionary` or a `FrozenDictionary` (.NET 8) off to the side and publish it by swapping one reference. Readers then take no lock at all and always see a complete version; the cost is rebuilding on every change.
  • Why is reading ConcurrentDictionary.Count on every request a problem?
    `Count` acquires every stripe lock to add up the per-stripe counts, so each read briefly blocks all writers and serialises with other global operations, and the number is stale as soon as it returns. Use `IsEmpty`, which checks without locks first, or keep a separate counter updated with `Interlocked` if you need a running total.
  • What does the concurrencyLevel constructor argument change?
    It sets how many stripe locks guard the buckets, `Environment.ProcessorCount` by default. More stripes mean fewer writers colliding on the same lock, but `Count`, `Keys`, `ToArray` and `Clear` must take every one of them, so global operations get slower. The .NET docs advise measuring and avoiding global operations if you raise it.

  • ConcurrentDictionary is always faster than Dictionary, so use it everywhere.
  • A lock around Dictionary never scales and should be banned.
  • ConcurrentDictionary.Count is a cheap field read.
  • Enumerating a ConcurrentDictionary gives a consistent point-in-time snapshot.
  • Switching to ConcurrentDictionary makes multi-key updates atomic.