Unlocking Hessian: How Java’s Binary Serialization Boosts Performance

This article explains the core principles of Hessian, a Java‑optimized binary serialization protocol, covering object graph traversal, encoding formats, data‑block tags, repeated‑object reuse, small‑integer inlining, and integer compression, and shows why it offers higher efficiency and compression than text‑based alternatives.

DeWu Technology
DeWu Technology
DeWu Technology
Unlocking Hessian: How Java’s Binary Serialization Boosts Performance

Overview

In high‑level programming languages developers constantly deal with object/struct abstractions, but in a distributed architecture every service process must exchange data across process boundaries. In Java, RPC inputs and outputs are objects that must be transmitted as a flattened byte[] stream.

Basic Encoding Principles

The encoding process consists of two core parts:

Object‑graph traversal : walk the object graph (using reflection or meta‑programming) to visit each node.

Encoding format : flatten the high‑level structure into a byte[] according to a protocol.

Object‑graph traversal determines the lower bound of what can be encoded (whether the structure can be correctly handled), while the encoding format determines the upper bound (efficiency, compatibility, etc.).

Hessian Encoding Format

Hessian is a mature Java object codec that offers two major advantages over language‑neutral protocols such as JSON or Protobuf:

Deep Java ecosystem adaptation : native support for Java‑specific features like generics and polymorphism.

Efficient binary protocol : a carefully designed binary encoding yields higher encode/decode speed and better data compression than textual formats.

For Java RPC, Hessian is a robust, widely‑tested choice.

Object‑Graph Traversal

Traversal is performed depth‑first, navigating object properties. Each node in the graph becomes a data block in the byte stream.

Encoding Format

The format adds a prefix byte (tag) to each data block to convey its type and structure. For example, an int32 is encoded with a specific tag followed by the 4‑byte value, but Hessian also provides compact variants for small integers.

POJO Encoding

A POJO is treated as a special data block. Its class name and field list are written once, and subsequent field values are encoded according to the field order. This enables structure reuse and avoids repeated encoding of identical field metadata.

Repeated Object Reuse

When the same object appears multiple times (including cyclic references), Hessian assigns it a sequential reference number and later writes only that number, preventing duplicate encoding and stack overflow in cyclic graphs.

Small‑Integer Inline (direct)

Values in the range –16 to 47 are encoded in a single byte by adding 0x90 to the value, eliminating the need for a full 4‑byte integer.

Integer Compression

Hessian chooses the smallest possible representation based on the integer’s magnitude:

1‑byte encoding for –16 to 47.

2‑byte encoding for –2048 to 2047 (tag 0xC8 + high bits, followed by low byte).

3‑byte encoding for –262144 to 262143 (tag 0xD4 + high bits, followed by two bytes).

5‑byte encoding for all other 32‑bit integers (tag ‘I’ followed by 4 bytes).

String Encoding

Strings are split into chunks up to 32 KB. Full‑size chunks use a fixed tag ‘R’. The final chunk uses a length‑dependent tag: 0‑31 bytes use a single‑byte tag that inlines the length; 32‑1023 bytes use tag 0 followed by a 1‑byte length; ≥1024 bytes use tag ‘S’ followed by a 2‑byte length. This reduces overhead for short strings.

Summary

Hessian is a Java‑centric binary protocol that improves serialization efficiency through object‑graph traversal, data‑block tagging, structure reuse, and variable‑length integer and string encodings. Its design reduces both bandwidth and CPU consumption compared with text‑based formats, and the concepts apply to other serialization frameworks.

Reference links are provided for further reading.

JavaserializationHessianbinary protocolobject encoding
DeWu Technology
Written by

DeWu Technology

A platform for sharing and discussing tech knowledge, guiding you toward the cloud of technology.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.