JustinDB is a highly distributed system which forms cluster of nodes that communicate each other via message passing. All of these messages (think about internal communication of nodes or typical data replication) are send over wire (network). In order to being able do that we have to serialize and deserialize passing state to single bytes back and forth. That can cost. Sometimes a lot. We tend to think that network is always the slowest part of distributed systems communication. Numerous tests however show that using not an efficient serializer can lead to getting times that are on par with a network hop. Thats crazy!
JustinDB is built on top of Akka Cluster toolkit. Akka has a built-in Extension for serialization which allows us to use the built-in serializers or even write our own. The mechanism is both used by Akka internally to serialize messages and ad-hoc serialization of whatever message you might need it for.
Quick notes to follow:
- Current remoting module of Akka uses standard Java Serializer mechanism.
- Since the 2.4.11 release of Akka it is possible to entirely disable the default Java Serialization mechanism.
- New remoting implementation (codename Artery, still experimental though) does not use Java serialization for internal messages by default.
- All messages that are sent in the same local Actor System don’t need to be serialized as in-memory message passing is done via references.
Unfortunately Java Serializer is one of the exemplary mechanism known to be slow - it never was designed for high throughput messaging though. At the same time it’s very convenient to use - when prototyping your project you can focus on logic implementation and leave this part for later time.
When talking about serialization mechanism in terms of JustinDB I’ve recently decided to move on and switch to using Akka-Kryo extension which provides custom Kryo-based serializers for Scala and Akka.
JustinDB and Kryo got married 👰
Kryo is known to be a very fast and efficient serialization. I’ve decided to use Romix’s kryo-based serialization library which is a well maintained project (exemplary implementation of own Akka Serializer).
SerializerInit is an entrypoint for Akka-Kryo extension to know what payload/classes/messages we want to register into it. As you can see every registration line consists of msg class definition, its dedicated serializer function and ID. This class is instantiated right after new process of JustinDB is rolling out.
Its worth to take a look at into how I managed to serialize ADT (Abstract Data Type) which extends common interface (e.g.
StorageNodeConflictedWrite - they share the same serializer).
I’ve also declared in the Akka
serialization-bindings section which classes should use kryo serialization. You can find it under
Solution is simply - I use specific discriminator numbers that are saved next to specific serialized payload (e.g.
StorageNodeSuccessfulWrite payload). Pattern matching for the win! 👊
You can find more details about Akka-Kryo in its official documentation.
Currently I’m doing some heavy test loading against cluster of JustinDB that runs on 5 JVMs (5 node cluster) with enabled replication factor set to 3. All tests speak to databse via http protocol. You can find them here. Repository is almost empty but I’ve finally got a time to do some database stabilization and benchmarking around. As soon as I will get any meaningful result I will update this post (or write dedicated one).