Affinity Colocation

Colocate compute with data or data with data.

Given that the most common ways to cache data is in PARTITIONED caches, colocating compute with data can significantly improve performance and scalability of your application.

Colocating Compute with Data

It is possible to route computations to the nodes where the data is cached. This concept is known as Colocation Of Computations And Data. It allows to route whole units of work to a certain node.

To colocate compute with data you should use ICompute.AffinityRun(...) and ICompute.AffinityCall(...) methods.

Here is how you can colocate your computation with the same cluster node on which person with specified id is cached:

void AffinityRun()
{
    using (var ignite = Ignition.Start())
    {
        int key = 5;

        ignite.GetCompute().AffinityRun("persons", key, new AffinityAction {Key = key});
    }
}

class AffinityAction : IComputeAction
{
    [InstanceResource] private readonly IIgnite _ignite;

    public int Key { get; set; }

    public void Invoke()
    {
        // When used in AffinityRun, this cache access is local
        var person = _ignite.GetCache<int, IPerson>("persons").Get(Key);

        Console.WriteLine(person.Name);
    }
}

ICompute vs ICacheEntryProcessor

Both ICompute.AffinityRun(...) and ICache.Invoke(...) methods offer ability to colocate compute and data. The main difference is that Invoke(...) methods are atomic and execute while holding a lock on a key. You should not access other keys from within the ICacheEntryProcessor logic as it may cause a deadlock.

AffinityRun(...) and AffinityCall(...), on the other hand, do not hold any locks. For example, it is absolutely legal to start multiple transactions or execute cache queries from these methods without worrying about deadlocks. In this case Ignite will automatically detect that the processing is colocated and will employ a light-weight 1-Phase-Commit optimization for transactions (instead of 2-Phase-Commit).