With a .NET ConcurrentDictionary<string, int>, why does `counters[t] = counters.GetValueOrDefault(t) + 1` lose counts, and when is a lock still needed?
answer
- two calls, not one
- read, compute, compare, retry
- TryUpdate compares before it swaps
- invariants across several entries
basics
~20 sThe read and the write are separate thread-safe calls, so two threads can both read 7 and both write 8. AddOrUpdate retries its delegate until a compare-and-swap succeeds; a lock is still needed when one rule spans several keys or collections.
solid answer
~50 sEach `ConcurrentDictionary` member is thread-safe, but the dictionary cannot know that two calls belong together: between your read and your indexer write another thread may write, and your write overwrites it. `AddOrUpdate(key, 1, (_, n) => n + 1)` loops instead: it reads the current value, runs the update delegate with no lock held, then swaps the result in only if the stored value still equals the one it read (the same check `TryUpdate(key, newValue, comparisonValue)` makes); otherwise it reads and calls the delegate again. In one run, 800,000 increments made about 1.4 million delegate calls, so the delegate must be cheap and free of side effects. You still need a `lock`, usually around a plain `Dictionary`, when an invariant spans more than one entry or collection, such as moving quota between two tenants, or when threads mutate a shared value object stored in the dictionary.
code
csharp · 26 linesusing System.Collections.Concurrent;
var naive = new ConcurrentDictionary<string, int>();
var atomic = new ConcurrentDictionary<string, int>();
int updateCalls = 0;
Task[] requests = Enumerable.Range(0, 8).Select(_ => Task.Run(() =>
{
for (int i = 0; i < 100_000; i++)
{
// Read, add one, write back: three separate thread-safe calls.
naive["tenant-42"] = naive.GetValueOrDefault("tenant-42") + 1;
// One call: the update delegate is retried until TryUpdate wins.
atomic.AddOrUpdate("tenant-42", 1, (_, current) =>
{
Interlocked.Increment(ref updateCalls);
return current + 1;
});
}
})).ToArray();
await Task.WhenAll(requests);
Console.WriteLine($"naive: {naive["tenant-42"]}");
Console.WriteLine($"atomic: {atomic["tenant-42"]}");
Console.WriteLine($"update delegate calls: {updateCalls} (successful updates: 799999)");go deeper
Remember that a thread-safe collection makes each call safe, not a read followed by a write; use AddOrUpdate for counters.
Walk through AddOrUpdate's loop: lock-free read, delegate outside the lock, compare-and-swap, retry. Name TryUpdate as the public compare-and-swap.
Spot the cases the dictionary cannot cover: multi-key transfers, two collections kept in step, mutable value objects, consistent multi-key reads, and move them under one lock.
Set the review rule: no read-then-write pairs on concurrent collections, pure update delegates, and an explicit lock wherever an invariant spans more than one entry.
## Thread-safe calls, non-atomic sequences Every public member of `ConcurrentDictionary<TKey,TValue>` is safe to call from many threads at once. That is a promise about **each call**, not about code that strings several calls together. The line ```csharp counters[t] = counters.GetValueOrDefault(t) + 1; ``` is two calls: a read (`GetValueOrDefault`, an extension method over `IReadOnlyDictionary<TKey,TValue>`) and a write (the indexer setter). Two request threads can interleave like this: 1. Thread A reads 7. 2. Thread B reads 7. 3. Thread A writes 8. 4. Thread B writes 8, and A's increment is gone. Nothing is corrupted, unlike a plain `Dictionary` under concurrent writes; the dictionary did exactly what each call asked. The lost update is in your code. `counters[t]++` and `counters[t] += 1` compile to the same get-then-set pair and lose counts the same way. ## How AddOrUpdate makes it one operation `AddOrUpdate(TKey key, TValue addValue, Func<TKey,TValue,TValue> updateValueFactory)` is a retry loop in the reference source: 1. Look the key up without a lock. 2. If it is missing, try an add-only insert of `addValue`; if another thread added first, go round again. 3. If it is present, call `updateValueFactory(key, oldValue)` **with no lock held**. 4. Take the key's stripe lock and replace the value only if it still equals `oldValue`, compared with `EqualityComparer<TValue>.Default`. If it changed, release and go back to step 1. Step 4 is a **compare-and-swap**, and it is public as `TryUpdate(TKey key, TValue newValue, TValue comparisonValue)`, which returns `false` instead of retrying, so you can write your own loop when you need control over the retry. | Single call | Atomic guarantee | |---|---| | `TryAdd(key, value)` | adds only if the key is absent | | `TryUpdate(key, newValue, comparisonValue)` | replaces only if the current value equals `comparisonValue` | | `TryRemove(key, out value)` | removes and hands back the removed value | | `TryRemove(KeyValuePair<TKey,TValue>)` | removes only if the key still maps to that value | | `GetOrAdd(key, factory)` | one value stored, but the factory may run several times | | `AddOrUpdate(key, add, update)` | one update lands per call, but the delegate may run several times | ## The update delegate's contract Because the delegate runs outside the lock and is retried on contention: - **It may run many times per successful update.** In the example below it ran about 1.4 million times for 799,999 successful updates. - **It must be pure:** compute a new value from the key and the old value. A log line, a metric or an HTTP call inside fires per attempt, not per update. - **It must not mutate `oldValue`** when that is a reference type: build a new object, otherwise a failed attempt has already changed the shared instance. - **It must be fast:** a slow delegate widens the window for other writers to win, which causes more retries. ## When a lock is still needed The dictionary protects its own structure and single-key transitions. It cannot help when your rule covers more: - **Several keys at once.** Moving 10 units of quota from tenant A to tenant B with two `TryUpdate` calls leaves a moment where the units exist in neither or both. - **Several collections at once.** Keeping a `ConcurrentDictionary` and a `ConcurrentQueue` in step needs both changes under one lock. - **Mutable values.** Storing a `List<string>` as the value makes the dictionary safe, not the list: two threads calling `Add` on it race exactly like any shared `List<T>`. - **A consistent view of several entries,** such as reading two tenants' counters as of one instant. In those cases a private lock object around a plain `Dictionary<TKey,TValue>` is usually clearer than a `ConcurrentDictionary` plus a lock, because the lock already serialises everything. How `lock` itself works belongs with locks and `Interlocked`. ## In the API gateway For per-tenant request counters, `counters.AddOrUpdate(tenant, 1, (_, n) => n + 1)` is correct and simple. An alternative that avoids retries entirely stores a small mutable holder per tenant, created once with `GetOrAdd`, and increments a field in it with `Interlocked.Increment`. Either way, the code that resets every tenant's window at the top of the minute touches all keys, so it needs its own design: a new dictionary swapped in, or a lock.
- Why must AddOrUpdate's update delegate have no side effects?It runs with no lock held and is called again whenever another thread changes the value first. A log line, a counter or a remote call inside it therefore fires once per attempt, not once per successful update, and under contention attempts can far outnumber updates. Keep it a pure function of the key and the old value.
- How does TryUpdate decide that the value has not changed?It compares the stored value with `comparisonValue` using `EqualityComparer<TValue>.Default`. For an `int` or a class that does not override `Equals`, that is value or reference identity. For a record or any type with value equality, a different instance with equal contents counts as unchanged, so the swap succeeds against a replacement that merely looks the same.
- Is if (!d.ContainsKey(k)) d.TryAdd(k, v) a race on a ConcurrentDictionary?The data stays correct, because `TryAdd` alone adds only when the key is absent and returns `false` otherwise; the `ContainsKey` is a wasted lookup. The same shape with the indexer, `if (!d.ContainsKey(k)) d[k] = v`, is a real race: another thread can add between the check and the write, and the indexer overwrites its value.
- A ConcurrentDictionary makes every piece of code that uses it thread-safe.
- AddOrUpdate runs its update delegate exactly once, inside the lock.
- counters[t]++ on a ConcurrentDictionary is an atomic increment.
- Storing a List<T> as the value makes adding to that list thread-safe.
- Two TryUpdate calls on different keys behave as one atomic step.