Finally, we make sure that the counter data is available on another member when a member goes down. To do this, have the IncOperation class implement the BackupAwareOperation interface contained in the SPI package. See the following code.

class IncOperation extends Operation
	implements PartitionAwareOperation, BackupAwareOperation {
   public int getAsyncBackupCount() {
      return 0;

   public int getSyncBackupCount() {
      return 1;

   public boolean shouldBackup() {
      return true;

   public Operation getBackupOperation() {
      return new IncBackupOperation(objectId, amount);

The methods getAsyncBackupCount and getSyncBackupCount specify the count for asynchronous and synchronous backups. Our sample has one synchronous backup and no asynchronous backups. In the above code, counts of the backups are hard-coded, but they can also be passed to IncOperation as parameters.

The method shouldBackup specifies whether our Operation needs a backup or not. For our sample, it returns true, meaning the Operation will always have a backup even if there are no changes. Of course, in real systems, we want to have backups if there is a change. For IncOperation for example, having a backup when amount is null would be a good practice.

The method getBackupOperation returns the operation (IncBackupOperation) that actually performs the backup creation; the backup itself is an operation and will run on the same infrastructure.

If a backup should be made and getSyncBackupCount returns 3, then three IncBackupOperation instances are created and sent to the three machines containing the backup partition. If fewer machines are available, then backups need to be created. Hazelcast will just send a smaller number of operations.

Performing the Backup with IncBackupOperation

Now, let's have a look at the IncBackupOperation. It implements BackupOperation, you can see the source code for BackupOperation here.

public class IncBackupOperation
	extends Operation implements BackupOperation {
   private String objectId;
   private int amount;

   public IncBackupOperation() {

   public IncBackupOperation(String objectId, int amount) {
      this.amount = amount;
      this.objectId = objectId;

   protected void writeInternal(ObjectDataOutput out) throws IOException {

   protected void readInternal(ObjectDataInput in) throws IOException {
      objectId = in.readUTF();
      amount = in.readInt();

   public void run() throws Exception {
      CounterService service = getService();
      System.out.println("Executing backup " + objectId + ".inc() on: " 
        + getNodeEngine().getThisAddress());
      Container c = service.containers[getPartitionId()];, amount);

image NOTE: Hazelcast will also make sure that a new IncOperation for that particular key will not be executed before the (synchronous) backup operation has completed.

Running the Sample Code

Let's see the backup functionality in action with the following code.

public class Member {
   public static void main(String[] args) throws Exception {
      HazelcastInstance[] instances = new HazelcastInstance[2];
      for (int k = 0; k < instances.length; k++) 
         instances[k] = Hazelcast.newHazelcastInstance();
      Counter counter = instances[0].getDistributedObject(CounterService.NAME, "counter");;

Once it is run, the following output will be seen.

Executing on: Address[]:5702
Executing backup on: Address[]:5701

As it can be seen, both IncOperation and IncBackupOperation are executed. Notice that these operations have been executed on different cluster members to guarantee high availability.