liveBook · Manning

Serialization

As explained in Akka in Action, Second Edition

Marshalling is the conversion of in-memory data or objects into a representation suitable for transmission in a communication protocol.

Serialization converts in-memory objects into bytes for transport or storage, then deserialization reconstructs objects. It becomes necessary when Akka actors communicate across JVMs, whether on one host or different machines; persistent events likewise must be serialized before they reach a journal. section 8.5.1 section 9.3.5

For clustered actor protocols, bind a marker trait to a serializer and mix that trait into every message that crosses node boundaries. Jackson support offers JSON and CBOR choices: CBOR is binary, more compact, and described as having slightly better throughput, while JSON is text-based. section 8.4.3 section 8.5.1

serialization-bindings {
      "example.countwords.CborSerializable" = jackson-cbor     #1
}

A serialization binding maps the CborSerializable marker trait to the Jackson CBOR serializer, so marked protocol messages use that format. section 8.5.1

Choose serialization with the boundary and lifetime of data in mind:

  • Use Jackson serialization for a straightforward Akka setup; Protocol Buffers offers better throughput and flexible schema development but takes more setup and maintenance. section 8.5.1
  • Plan for schema evolution when persisting events. Older journal entries must still deserialize after fields are added, removed, split, or merged; preserve immutable historical facts rather than rewriting the past. section 9.3.5
  • Use marshalling at an HTTP or RPC boundary. In Akka HTTP, a Marshaller[A,B] found through implicit resolution can convert values to JSON, while formatters describe custom case classes. section 11.1.5

Cargo HTTP endpoint

A cargo service accepts entityId, kind, and size request parameters, creates a Cargo value, and sends it to a sharded container. For reads, JSON formatters for Cargo and Cargos let an asynchronous actor response complete as a JSON payload. The same formatters can support request unmarshalling when the request entity contains the needed fields. section 11.1.6 section 11.1.5

How Akka in Action, Second Edition covers it

How deep it goes
Explains serializer binding, marker traits, JSON formatters, and schema-evolution concerns without implementing a custom serializer.
Examples
Uses distributed word-count messages, evolving AddCargo events, and a sharded cargo HTTP endpoint returning JSON.
What it assumes
Assumes Scala and Akka actor usage while introducing cross-JVM conversion and implicit marshaller lookup.
What you can do after
Apply it via Akka Jackson and Spray JSON by binding marker traits and providing implicit formatters.
How it connects
Connects serialization with Akka actors, clustering, persistence, schema evolution, HTTP, JSON, and Protocol Buffers.

Also explained in

Compare how 24 books explain serialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.