Understanding Hazelcast PN Counter: A Comprehensive Guide

Overview

Hazelcast work with distributed cluster with nodes. If you need to implement counter which need to shared accross the distributed nodes for the testing and fundamental. Hazelcast gives a few incredible assets to deal with these difficulties. Among them is the Hazelcast PN Counter, a profoundly helpful design for overseeing dispersed information in a ultimately steady way.

Also know about:

What is a Hazelcast PN Counter?

A Hazelcast PN Counter, or Positive-Negative counter, is a distributed counter that can be incremented and decremented by multiple nodes in a Hazelcast cluster. It is based on the Conflict-free Replicated Data Types (CRDTs) concept, which allows for eventually consistent distributed systems where operations can be performed without requiring synchronization among all nodes at all times. Hazelcast’s PN Counter guarantees that the counter values will converge correctly even if there are network partitions, hence providing a scalable and fault-tolerant solution for distributed counting problems.

The counter ensures eventual consistency by replicating the updates across the cluster asynchronously. Each node that interacts with the PN Counter maintains a local replica of the counter and propagates updates to other nodes in the background.

Understanding Hazelcast PN Counter: A Comprehensive Guide

Key Features of Hazelcast PN Counter

  1. Distributed Nature: The PN Counter works seamlessly in a distributed environment where multiple nodes in the cluster can independently increment or decrement the counter.
  2. Fault-Tolerant: Thanks to the CRDT-based approach, even if some nodes go offline due to network failures, the counter value will still converge once the nodes are back online.
  3. Eventually Consistent: While the updates to the counter may not be immediate across the cluster, the system ensures eventual consistency without conflicts.
  4. Asynchronous Replication: The changes made to the PN Counter are replicated across the cluster asynchronously, providing high availability and reduced latency.

How the Hazelcast PN Counter Works

To fully understand how a Hazelcast PN Counter works, it’s crucial to look at how CRDTs function. Conflict-free Replicated Data Types (CRDTs) are data structures designed for distributed systems where multiple replicas are independently updated. CRDTs allow concurrent operations without locks or coordination between replicas, which is essential for high availability and partition tolerance in distributed systems.

PN Counter’s Operation

The PN Counter is built upon the principle of maintaining two separate counters: Positive (P) and Negative (N). These counters track the increments and decrements, respectively. Each replica in the cluster maintains both a P and an N counter.

  • Increment Operations: When a node increments the counter, the positive (P) part of the PN counter increases.
  • Decrement Operations: When a node decrements the counter, the negative (N) part of the PN counter decreases.

Each node holds its own replica of the P and N counters and communicates its updates asynchronously with other replicas. Over time, the system merges these updates to maintain the correct overall count.

Convergence of Counter Values

In a typical scenario where network partitions occur, nodes might update the counter independently, leading to temporarily diverging values. However, thanks to the CRDT properties, when communication is restored between the nodes, the system automatically merges the values by summing the increments and decrements across all replicas.

The counter’s final value is calculated as:

Final Value = Sum of all P counters – Sum of all N counters across the cluster.

This mechanism ensures that no data is lost during network partitions, and all nodes converge on the correct counter value eventually.

Use Cases of Hazelcast PN Counter

Distributed Rate Limiting

One common use case of PN Counters is distributed rate limiting. In scenarios where multiple nodes need to keep track of a limited resource, such as API calls or requests per second, the PN Counter can act as a globally distributed counter, tracking the current usage across multiple nodes.

By allowing asynchronous updates across nodes and eventual consistency, Hazelcast PN Counter ensures that even if a node is temporarily unavailable, the rate-limiting data is not lost, and the system continues to function as expected.

Distributed Metrics Collection

Another critical use case is in distributed metrics collection, where various metrics (such as hits on a webpage or transactions processed) need to be counted across different nodes in a cluster. Using a PN Counter allows for accurate and scalable collection of metrics in real time, even under heavy load and with potential network failures.

Global Counters in Microservices

In microservice architectures, having a global counter for tracking the number of instances of certain events (e.g., order completions, user registrations) across different services is crucial. The Hazelcast PN Counter allows microservices to increment or decrement the count without worrying about locking or synchronization issues, providing a robust solution for global counting across services.

Advantages of Using Hazelcast PN Counter

High Availability

Hazelcast PN Counter is designed to be highly available, even in cases of network partitions or node failures. Since each node maintains its own replica of the counter, operations can continue without interruption, and the final count will converge once communication is restored between nodes.

Scalability

Because the PN Counter operates in a distributed manner, it can easily scale as more nodes are added to the Hazelcast cluster. The asynchronous replication mechanism ensures that performance is not bottlenecked by synchronization overheads.

Consistency Guarantees

Although Hazelcast PN Counter is eventually consistent, it guarantees that all updates to the counter will be merged correctly, with no loss of data. This is crucial in distributed environments where immediate consistency might not be feasible.

How to Implement Hazelcast PN Counter

Implementing a PN Counter in a Hazelcast cluster is straightforward. Below is a basic example of how to initialize and use the PN Counter in Java:

package com.javatecharc.demo.pncounter;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.crdt.pncounter.PNCounter;

public class PNCounterExample {
    public static void main(String[] args) {
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
        PNCounter counter = hazelcastInstance.getPNCounter("javatecharc-pn-counter");

        // Increment the counter
        counter.incrementAndGet();
        System.out.println("Increment Current counter value: " + counter.get());
        counter.incrementAndGet();
        System.out.println("Increment Current counter value: " + counter.get());
        counter.incrementAndGet();
        System.out.println("Increment Current counter value: " + counter.get());

        // Decrement the counter
        counter.decrementAndGet();
        System.out.println("Decrement Current counter value: " + counter.get());

        // Decrement the counter
        counter.decrementAndGet();
        System.out.println("Decrement Current counter value: " + counter.get());

        // Decrement the counter
        counter.decrementAndGet();
        System.out.println("Decrement Current counter value: " + counter.get());

        // Get the current value
        long value = counter.get();
        System.out.println("Current counter value: " + value);

        hazelcastInstance.shutdown();
    }
}

Output:

Increment Current counter value: 1
Increment Current counter value: 2
Increment Current counter value: 3
Decrement Current counter value: 2
Decrement Current counter value: 1
Decrement Current counter value: 0

This simple example demonstrates the creation of a PN Counter in a Hazelcast cluster, incrementing, decrementing, and retrieving the current counter value.

Conclusion

The Hazelcast PN Counter is a powerful and flexible solution for managing distributed counters in a fault-tolerant, scalable manner. Its use of CRDTs ensures that counter values converge correctly, even in the face of network partitions or node failures. Whether you’re tracking metrics, implementing rate limiting, or managing global counters in a microservices architecture, the PN Counter offers a reliable and efficient way to handle distributed counting operations.

The sample code available over the github.

Leave a Comment

Your email address will not be published. Required fields are marked *

Index