The New CP Subsystem Map

Hazelcast Platform 5.4 introduces a map data structure to the CP Subsystem – the CP Map.

In this blog, we’ll show how CP Map* differs from Hazelcast Platform’s existing maps, and how to work with it from code and command line.

Background

For those unfamiliar, let’s briefly review CP Subsystem and maps.

What are AP and CP?

Production-grade Hazelcast Platform clusters run across a number of machines for scalability, resilience, and so on. As a result, consideration must be given to behavior during a network fault that partitions the cluster into two. Changes to a data record cannot be reflected in a backup copy if there is no networking.

The choice is simple: do or don’t allow changes that cause copies to deviate. The former has the data available for use, and the latter keeps it consistent.

AP = Availability

AP selects availability when a network fault has partitioned the cluster. Data can be used, but since the data record and its backup can’t communicate, they diverge if changed. For example, you might be selling seats on an airplane. The important thing is to sell the seats. If some sales are a result of updating one copy of the data and other sales are a result of updating the other copy, it’s possible the flight has been oversold. Certainly, this is not ideal, but it can be sorted out before the flight departs.

CP = Consistency

CP is selecting consistency when a network fault has partitioned the cluster; data copies are not allowed to diverge. Here, you might be executing orders on the stock market, and if these are very large, then correctness becomes much more important. If there is an order to buy 10,000,000 units of stock and, due to a fault, 20,000,000 are bought, that’s a massive problem that could bankrupt a company.

Consistency is the priority here. Data can’t be allowed to diverge.

CP or AP

So that’s it. In Hazelcast Platform, some features are AP, and some are CP, and users can pick the one that suits the use case.

You can read more on CP here.

What is a Map?

A map is a data store for large volumes of individual records, much like a database table. Each map entry has a key that uniquely identifies it, and other fields. In Hazelcast Platform, the original implementation is the IMap. If the Hazelcast cluster has 5 server processes, each process is responsible for 1/5th of the storage. Meaning, a process will hold 1/5th of the map entries and 1/5th of the backup copies of map entries. Until now, all maps have had AP properties.

The CP Map

Starting with Hazelcast Platform 5.4, there is the first implementation of CPMap, the CP version of a map. Now, if you have business-critical data for which map storage is appropriate, you can benefit from the strong data consistency guarantees and persistence options in Hazelcast Platform’s CP Subsystem.

Hazelcast Platform’s CPMap includes basic map functions, “put(K,V)“, “get(K)” and others. Subsequent Hazelcast Platform releases will add more.

Working with the CP Map

Depending on your objective, you can work with the CP Map from code, the command-line client, or in Management Center.

From code

Working from code is, of course, simple. You might do something like this:
CPMap cpMap = hazelcastInstance.getCPSubsystem().getMap("m1");
cpMap.set("hello", "world");

Apart from obtaining the reference to the CP Map, it’s an easy change from working with an IMap, from a developer’s perspective.
IMap iMap = hazelcastInstance.getMap("m2");
iMap.set("hello", "world");

Or, if you like, using a compareAndSet operation, multiple clients can attempt to mutate data, but only one will succeed, and that write will be safe across network splits:

boolean ok = cpMap.compareAndSet("hello", "world", "hallo");

From the command-line client (CLC)

Although production data is typically updated by code, there may occasionally be a need for ad-hoc data changes. For this, use the Hazelcast Command-Line Client.

This allows you to query the CP Map “m1” created in the code sample above.

nstevenson SQL> \cpmap -n=m1 get hello
this
hallo

OK Returned 1 row(s).

Or, if you prefer, you can delete or mutate the entry:

nstevenson SQL> \cpmap -n=m1 delete hello
OK Deleted the key from the CPMap 'm1'.

Management Center

Finally, the Management Center allows you to inspect the CP system as a whole. For individual data record access, use the CLC as above.

CP Map in Management Center

Coming Soon in the CP Map

Hazelcast Platform 5.4 sees the first release of CP Map. Later releases will add more functionality, such as bulk write operations.

Comparison to IMap

As mentioned above, Hazelcast Platform 5.4 is the first release of the CP Map, yet IMap currently has more features. Additionally, CP Map is constrained in size; all members in the CP group that hosts the map have a complete copy.

Comparison to IAtomicReference

It is possible to approximate similar functionality with IAtomicReference, but this does not perform as well.

With CP Map, you can do:

CPMap cpMap = hazelcastInstance.getCPSubsystem().getMap("m1");
String v = cpMap.get("hello");

With IAtomicReference it would be:

IAtomicReference<Map> iAtomicReference = hazelcastInstance.getCPSubsystem().getAtomicReference("ar1");
Map m = iAtomicReference.get();
String v = m.get("hello");

Using IAtomicReference, the entire map moves across the wire. This approach is – at least – less efficient and becomes a point of contention when two threads need to update different values in the map stored as a value in the IAtomicReference.

Summary

From a developer’s perspective, the CPMap can replace the IMap in due course; it’s a map with the same concepts. For an architect, this is a powerful new data structure for the storage of map data, but with the consistency guarantees needed for business-critical operations.

To learn more about the CP Subsystem in Hazelcast Platform, we offer a training course on strong data consistency with the CP Subsystem.

*CP Map is only available in Hazelcast Platform Enterprise Edition.

Keep Reading