Atomic Types

Atomics in Ignite can be read and updated from any node in the cluster.

Ignite supports distributed atomic long, atomic reference and atomic sequence, providing an API similar System.Threading.Interlocked class.

Atomics in Ignite are distributed across the cluster, essentially enabling performing atomic operations (such as Increment or CompareExchange) with the same globally-visible value. For example, you could update the value of an atomic long on one node and read it from another node.

Features

  • Retrieve current value.
  • Atomically modify current value.
  • Atomically increment or decrement current value.
  • Atomically CompareExchange the current value to new value.

Distributed atomic long can be obtained via IAtomicLong interface, as shown below:

IIgnite ignite = Ignition.Start();
 
IAtomicLong atomicLong = ignite.GetAtomicLong(
    "atomicName", // Atomic long name.
    0,        		// Initial value.
    false     		// Create if it does not exist.
)

Below is a usage example of IAtomicLong:

IIgnite ignite = Ignition.Start();

// Initialize atomic long.
IAtomicLong atomicLong = ignite.GetAtomicLong("atomicName", 0, true);

// Increment atomic long on local node.
Console.WriteLine("Incremented value: " + atomicLong.Increment());

All atomic operations provided by IAtomicLong are synchronous. The time an atomic operation will take depends on the number of nodes performing concurrent operations with the same instance of atomic long, the intensity of these operations, and network latency.

📘

ICache interface has PutIfAbsent() and Replace() methods, which provide the same CompareExchange functionality as atomic types.

Atomic Configuration

Atomics in Ignite can be configured via AtomicConfiguration property of IgniteConfiguration. The following configuration parameters can be used :

PropertyDescriptionDefault
BackupsSet number of backups.0
CacheModeSet cache mode for all atomic types.Partitioned
AtomicSequenceReserveSizeSets the number of sequence values reserved for IgniteAtomicSequence instances.1000

Example

var cfg = new IgniteConfiguration
{
    AtomicConfiguration = new AtomicConfiguration
    {
        Backups = 1,
        CacheMode = CacheMode.Partitioned,
        AtomicSequenceReserveSize = 5000
    }
};
<igniteConfiguration>                            
    <atomicConfiguration backups='1' cacheMode='Partitioned' atomicSequenceReserveSize='5000' />
</igniteConfiguration>
<bean class="org.apache.ignite.configuration.IgniteConfiguration">
    ...
    <property name="atomicConfiguration">
        <bean class="org.apache.ignite.configuration.AtomicConfiguration">
            <!-- Set number of backups. -->
            <property name="backups" value="1"/>
          	
          	<!-- Set cache mode. -->
          	<property name="cacheMode" value="PARTITIONED"/>
        </bean>
    </property>
</bean>