Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ConcurrentDictionary.GetOrAdd() guaranteed to invoke valueFactoryMethod only once per key?

Problem: I need to implement object cache. The cache need to be thread-safe and need to populate values on demand(lazy loading). The values are retrieved via web service by Key(slow operation). So I've decided to use ConcurrentDictionary and its GetOrAdd() method that has a value factory method supposing that the operation is atomic and synchronized. Unfortunately I found the following statement in the MSDN article: How to: Add and Remove Items from a ConcurrentDictionary:

Also, although all methods of ConcurrentDictionary are thread-safe, not all methods are atomic, specifically GetOrAdd and AddOrUpdate. The user delegate that is passed to these methods is invoked outside of the dictionary's internal lock.

Well that's unfortunate but still doesn't answer my answer completely.

Question: Is value factory invoked only once per key? In my specific case: Is it possible that multiple threads that are looking for the same key spawning multiple request to the web service for the same value?

like image 397
f0rt Avatar asked Jul 26 '15 13:07

f0rt


People also ask

Is ConcurrentDictionary GetOrAdd thread safe?

The GetOrAdd function The vast majority of methods it exposes are thread safe, with the notable exception of one of the GetOrAdd overloads: TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory); This overload takes a key value, and checks whether the key already exists in the database.

Is ConcurrentDictionary indexer thread safe?

It is thread-safe in the sense that the internal state of the ConcurrentDictionary will not be corrupted. This is the main guarantee offered by this class.


3 Answers

As others have already pointed out, valueFactory may be invoked more than once. There is a common solution that mitigates this issue - have your valueFactory return a Lazy<T> instance. Although it's possible that multiple lazy instances will be created, the actual T value will only be created when you access Lazy<T>.Value property.

Specifically:

// Lazy instance may be created multiple times, but only one will actually be used.
// GetObjectFromRemoteServer will not be called here.
var lazyObject = dict.GetOrAdd("key", key => new Lazy<MyObject>(() => GetObjectFromRemoteServer()));

// Only here GetObjectFromRemoteServer() will be called.
// The next calls will not go to the server
var myObject = lazyObject.Value;

This method is further explained in Reed Copsey's blog post

like image 108
Adi Lester Avatar answered Sep 21 '22 16:09

Adi Lester


Is value factory invoked only once per key?

No, it isn't. The docs say:

If you call GetOrAdd simultaneously on different threads, valueFactory may be invoked multiple times, but its key/value pair might not be added to the dictionary for every call.

like image 32
Yuval Itzchakov Avatar answered Sep 19 '22 16:09

Yuval Itzchakov


Let's take a look at the source code of GetOrAdd:

public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
{
    if (key == null) throw new ArgumentNullException("key");
    if (valueFactory == null) throw new ArgumentNullException("valueFactory");

    TValue resultingValue;
    if (TryGetValue(key, out resultingValue))
    {
        return resultingValue;
    }
    TryAddInternal(key, valueFactory(key), false, true, out resultingValue);
    return resultingValue;
}

Unfortunately, in this case, it's clear nothing guarantees that valueFactory won't be called more than once, if two GetOrAdd invocations happen to run in parallel.

like image 37
Lucas Trzesniewski Avatar answered Sep 20 '22 16:09

Lucas Trzesniewski