Open Source Projects:
Pricing
Chat
Contact
Back to top

The simplest way to integrate Kryo with Hazelcast

September 13, 2016

Hazelcast offers a bunch of strategies to serialize your domain objects. You can start with simple Java Serialization and change the the strategy when it turns out to be performance bottleneck. It’s also simple to plug-in your own serialization.

Kryo is a popular serialization library. It’s super-fast yet easy-to-use. It does not pollute your domain model and it can even serialize classes which are not marked as Serializable. Hazelcast has no out-of-the-box support for Kryo. It’s rather easy to integrate it, however it means everyone has to write the same code and face the same issues. This is where the project SubZero comes to the rescue: It aims to make Kryo – Hazelcast serialization dead-easy.

Talk is cheap. Show me the code

Use SubZero for all classes

In this mode SubZero will completely replace Java serialization. Hazelcast internal serializers will still take precedence.

Declarative Configuration:

Insert this snippet into your Hazelcast configuration XML:

<serialization>
    <serializers>
        <global-serializer override-java-serialization="true">
            info.jerrinot.subzero.Serializer
        </global-serializer> 
   </serializers>
</serialization>

Programmatic Configuration:

Config config = new Config();
    SubZero.useAsGlobalSerializer(config);
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);

Use SubZero for selected classes only

In this mode Hazelcast will use SubZero for selected classes only.

Declarative Configuration:

<serialization>
    <serializers>
        <serializer type-class="some.package.Foo"
            class-name="info.jerrinot.subzero.Serializer"/> 
        <serializer type-class="some.package.Bar"
            class-name="info.jerrinot.subzero.Serializer"/>
   </serializers>
</serialization>

Programmatic Configuration:

Config config = new Config();
    SubZero.useForClasses(config, Foo.class, Bar.class);
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);

All cluster members have to use SubZero for the same types and the types have to be declared in the same order. As of Hazelcast 3.7 programmatic configuration will result in somewhat higher performance – this is given by a limitation of Hazelcast declarative configuration API. It is going to be fixed in Hazelcast 3.8

Maven Coordinates

SubZero is available in Maven Central. Just insert this snippet into pom.xml and you are ready to roll!

<dependency>
        <groupId>info.jerrinot</groupId>
        <artifactId>subzero-all</artifactId>
        <version>0.6</version>
    </dependency>

This version has all dependencies packaged inside. You can also use a version with regular dependencies:

<dependency>
       <groupId>info.jerrinot</groupId>
       <artifactId>subzero-core</artifactId>
       <version>0.6</version>
   </dependency>

Extensions

SubZero aims to provide the simplest possible way to hook Kryo serialization into Hazelcast.

Default SubZero serializer implementation uses auto-generated class type IDs and relies on a serializer registration order. This means all your cluster members have to use the same order in Hazelcast serializer configuration. This can be somewhat fragile. You can make it more robust by subclassing Serializer and returning a fixed class ID:

public class HashMapSerializerExample extends Serializer<HashMap> {
    
        public HashMapSerializerExample() {
            super(HashMap.class);
        }
    
        /**
         * TypeId has to be a unique for each registered serializer.
         *
         * @return TypeId of the class serialized by this serializer
         */
        @Override
        public int getTypeId() {
            return 10000;
        }
    }

Disclaimer

This is a community project not affiliated with the Hazelcast project.

About the Author

About the Author

Jaromir Hamala

Jaromir Hamala

Director of Engineering

Jaromir wrote his first line of code when he was seven years old. Since then, he has been interested in understanding how systems work ‘under the hood,’ and enjoys blogging about it. Jaromir’s areas of specialty include: performance and stability tuning of JVM, garbage collectors, data grids, application servers and architecture and design of applications. He enjoys pushing systems towards their limits, sharpening his skills by exploring HotSpot source code, contributing to open source projects and arguing about development over a pint of beer.

Follow me on

Latest Blogs

LockSupport.parkNanos() Under the Hood and the Curious Case of Parking, Part II: Windows

LockSupport.parkNanos() Under the Hood and the Curious Case of Parking

Hazelcast Jet Tutorial: Building Custom JDBC Sinks

View all blogs by the author

Free Hazelcast Online Training Center

Whether you're interested in learning the basics of in-memory systems, or you're looking for advanced, real-world production examples and best practices, we've got you covered.