In .NET, can ConcurrentDictionary<TKey,TValue>.GetOrAdd run its value factory more than once for the same key, and how do you make it run once?
answer
- lookup first, insert second
- the delegate runs outside the lock
- the loser's value is thrown away
- store a Lazy<TValue> instead
basics
~20 sYes. GetOrAdd runs the factory outside the dictionary's lock, so threads that miss at the same moment can each run it; one result is stored and every caller receives that one. Store Lazy<TValue> values to make the expensive work run once.
solid answer
~50 s`GetOrAdd(TKey, Func<TKey,TValue>)` first does a lock-free lookup. On a miss it calls `valueFactory(key)` and only then locks the stripe that holds the key to insert, add-only. If another thread inserted first, that insert fails and `GetOrAdd` returns the stored value, discarding the one this thread just built. The dictionary stays consistent, and every caller gets the same instance, but the factory's side effects (an HTTP call, a connection, a log line) can happen once per racing thread, and a discarded `IDisposable` is never disposed. The .NET docs state it plainly: the delegate runs outside the internal lock so unknown code cannot block other threads. When the work must run once, use `ConcurrentDictionary<TKey, Lazy<TValue>>`: racing threads may build several cheap `Lazy` wrappers, but only the stored one ever has `.Value` read, and a `Lazy<T>` built with just a delegate runs it once.
code
csharp · 26 linesusing System.Collections.Concurrent;
var settings = new ConcurrentDictionary<string, TenantSettings>();
int factoryRuns = 0;
using var start = new Barrier(8);
var tasks = Enumerable.Range(0, 8).Select(_ => Task.Run(() =>
{
start.SignalAndWait();
return settings.GetOrAdd("tenant-42", id =>
{
Interlocked.Increment(ref factoryRuns);
Thread.Sleep(50); // stands in for a slow config fetch
return new TenantSettings(id);
});
})).ToArray();
TenantSettings[] results = await Task.WhenAll(tasks);
Console.WriteLine($"factory runs: {factoryRuns}");
Console.WriteLine($"distinct instances returned: {results.Distinct().Count()}");
Console.WriteLine($"entries: {settings.Count}");
record TenantSettings(string TenantId)
{
public Guid Instance { get; } = Guid.NewGuid();
}go deeper
Know that GetOrAdd either returns the existing value or adds a new one, and that its factory may run more than once when threads race.
Explain the three steps: lock-free lookup, factory with no lock, add-only insert that returns the winner. Show the Lazy<TValue> fix and why only the stored wrapper is evaluated.
Point at the production cost: a cold cache plus a traffic burst multiplies calls to a backing service, and discarded IDisposable values leak. Handle a faulted Lazy by removing that exact pair.
Turn it into a review rule: factories passed to GetOrAdd must be cheap and idempotent, or the value type must be Lazy<TValue>. Weigh the extra indirection against the dependency load it saves.
## What GetOrAdd does, step by step `ConcurrentDictionary<TKey,TValue>` lives in `System.Collections.Concurrent` and is the BCL's thread-safe hash map. Its `GetOrAdd(TKey key, Func<TKey,TValue> valueFactory)` reads like one atomic "get, or create and add" operation, but the reference source for .NET 10 shows three separate steps: 1. **Lock-free lookup.** It hashes the key and calls an internal `TryGetValue`, which takes no lock. If the key is there, it returns the stored value and the factory never runs. 2. **Run the factory, with no lock held.** On a miss it calls `valueFactory(key)` on the calling thread. 3. **Locked, add-only insert.** It then locks the one stripe (lock object) that guards the key's bucket and inserts with *add-only* semantics. If another thread inserted the key while step 2 was running, nothing is overwritten: the call returns the value already stored. So when eight threads miss the same key at the same instant, all eight can reach step 2, and the factory can run up to eight times. Only the first insert wins, and all eight callers walk away with *that* value. ## Why the delegate runs outside the lock The .NET documentation on adding and removing items from a `ConcurrentDictionary` says it directly: all members are thread-safe, but `GetOrAdd` and `AddOrUpdate` are not atomic, because the user delegate is invoked outside the internal lock "to prevent unknown code from blocking all threads". A factory that waits two seconds on a network call while holding a stripe lock would stall every other writer whose key hashes to that stripe, and could deadlock if the factory waited on another thread that needed the same lock. What that design means for you: - **The stored value is consistent.** Every caller receives the same instance, so the dictionary never holds two values for one key. - **The work is not deduplicated.** Side effects inside the factory (a remote call, a file open, a metric increment) can happen once per racing thread. - **Losers' objects are dropped, not disposed.** If the factory creates something `IDisposable`, the discarded instances are never handed to anyone, so nobody calls `Dispose` on them. - **The window is short but real.** The race needs callers to miss together, which is exactly what happens when a cold cache meets a burst of traffic. ## Making the work run once with Lazy<TValue> Store a `Lazy<TValue>` as the value and read `.Value` after `GetOrAdd` returns: ```csharp var cache = new ConcurrentDictionary<string, Lazy<TenantSettings>>(); TenantSettings Get(string tenantId) => cache.GetOrAdd(tenantId, id => new Lazy<TenantSettings>(() => LoadSettings(id))) .Value; ``` The `Lazy` factory passed to `GetOrAdd` can still race, but all it does is allocate a small wrapper. Only the wrapper that won the insert is returned to callers, so only its `.Value` is ever read, and a `Lazy<T>` created with just a delegate defaults to a thread-safe mode that runs the delegate once and lets every other reader wait for the result. The other wrappers become garbage without ever running `LoadSettings`. How `Lazy<T>`'s thread-safety modes and a throwing delegate behave on their own is covered with locks and lazy initialisation; here, the point is only which wrapper gets evaluated. ## The overloads and what each one costs | Overload | When the value is produced | Typical trap | |---|---|---| | `GetOrAdd(TKey, TValue)` | by the caller, before every call | `GetOrAdd(id, new Settings(id))` builds an object even on a hit | | `GetOrAdd(TKey, Func<TKey,TValue>)` | only on a miss, possibly by several threads | a lambda that captures locals allocates a closure each call | | `GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg)` | only on a miss, possibly by several threads | none extra: pass state as `factoryArgument` and mark the lambda `static` | All three share the same race: none holds a lock while the value is being produced or chosen. ## In the API gateway A gateway caches per-tenant HTTP client settings that every request thread reads. On a cold start, a burst of requests for `tenant-42` all miss together. With `ConcurrentDictionary<string, TenantSettings>` each of them may fetch the tenant's configuration from the config service, which multiplies load on that service at the worst moment; with `ConcurrentDictionary<string, Lazy<TenantSettings>>` the fetch happens once and the other requests wait for it. If the fetch can fail, decide what a failure should leave behind: remove the faulted entry with `TryRemove(KeyValuePair<TKey,TValue>)`, passing the exact `Lazy` you read, so a retry can try again without deleting a newer entry another thread already stored.
- Does storing Lazy<TValue> stop several Lazy objects from being created?No. Racing threads can each run the `GetOrAdd` factory and allocate a `Lazy` wrapper. The dictionary stores one of them, every caller receives that stored one, and only its `.Value` is read, so the expensive delegate runs once. The losing wrappers are never evaluated and simply become garbage.
- What does settings.GetOrAdd(id, new TenantSettings(id)) cost when the key is already present?It still builds a `TenantSettings` on every call. That is the `GetOrAdd(TKey, TValue)` overload: C# evaluates the argument before the method runs, so the object is allocated and then ignored on a hit. Use the factory overload, or `GetOrAdd<TArg>` with a `static` lambda, so the value is built only on a miss.
- What should happen when the Lazy's delegate throws?By default the `Lazy` caches the exception and rethrows it on every `.Value`, so the dictionary would hold a poisoned entry. Catch the failure and remove that entry with `TryRemove(new KeyValuePair<string, Lazy<TenantSettings>>(id, lazy))`, which removes it only if the stored value is still that wrapper, so the next request can retry.
Two cooks see no plate for table 7 on the pass and both start cooking it. The pass accepts the first plate that arrives and the second goes in the bin, so the waiter always serves the plate on the pass. Pinning an order ticket to the pass first, and cooking only the pinned ticket, is the Lazy<TValue> fix.
- GetOrAdd holds the key's lock while the factory runs, so it runs once.
- When the factory races, two values end up stored for one key.
- Each racing caller gets back the value its own factory built.
- With Lazy<TValue> values only one Lazy object is ever created.
- GetOrAdd(key, new Expensive()) builds the object only when the key is missing.