MessagePack

Also known as: msgpack

On this page 7

MessagePack, often shortened to msgpack, is a binary serialization format that encodes the same kinds of values as JSON, such as maps, arrays, strings, numbers, booleans and null, in a compact, self-describing byte stream. Among data formats, it occupies the space between human-readable text formats and schema-driven binary formats: it needs no schema definition, yet produces smaller messages and faster parsing than JSON.

The format was created by Sadayuki Furuhashi around 2008 and is defined by a short, public specification with implementations in more than fifty programming languages. It is common in remote procedure calls, caching, message queues and log shipping, where many small messages make every saved byte and microsecond count.

How MessagePack encodes values

MessagePack encodes each value as a type byte followed by optional length and payload bytes. The first byte tells the decoder both the type and, for small values, the value itself or its length. This design lets small integers, short strings and small collections fit in very few bytes, because their type and size share a single byte.

For example, the integer 7 is stored as one byte, 0x07, because values from 0 to 127 use the positive fixint range. A string of up to 31 bytes uses a fixstr header, so the word “hello” takes six bytes in total. Larger values switch to explicit length fields of 8, 16 or 32 bits, and all multi-byte numbers use big-endian byte order.

The table below lists the main type families and their header bytes.

Type familyHeader byte rangeEncoded content
Positive fixint0x00 to 0x7fIntegers 0 to 127 in one byte
Fixmap and fixarray0x80 to 0x9fMaps and arrays with up to 15 elements
Fixstr0xa0 to 0xbfUTF-8 strings up to 31 bytes
Nil and booleans0xc0, 0xc2, 0xc3Null, false and true
Bin and ext0xc4 to 0xc9, 0xd4 to 0xd8Raw bytes and application-defined types
Float and sized integers0xca to 0xd332-bit and 64-bit floats, 8 to 64-bit integers
Negative fixint0xe0 to 0xffIntegers from -32 to -1 in one byte
Main MessagePack type families

The MessagePack type system and extensions

MessagePack defines nine core types: nil, boolean, integer, float, string, binary, array, map and extension. Two differences from JSON stand out. First, MessagePack has a dedicated binary type for raw bytes, so images or encrypted payloads do not need Base64 encoding, which inflates data by about one third. Second, integers and floats are distinct types, so a 64-bit integer survives a round trip without the precision loss that affects JSON numbers in JavaScript.

The extension type pairs a signed 8-bit type code with a byte payload. Codes from 0 to 127 are available to applications, while negative codes are reserved by the specification. The only reserved type defined so far is timestamp, with code -1, which stores seconds and nanoseconds in 4, 8 or 12 bytes.

  • Map keys may be any type, not only strings, although most implementations default to string keys.
  • Strings must be valid UTF-8, while binary values carry arbitrary bytes.
  • Maps and arrays declare their element count up front, which enables streaming decoders to allocate memory efficiently.

MessagePack compared with other data formats

MessagePack is best understood as binary JSON without a schema. It competes with JSON on simplicity and with schema-based formats such as Protocol Buffers and Apache Avro on efficiency, but it makes different trade-offs from both. Columnar formats such as Parquet and ORC serve a different purpose entirely: analytical storage of large tables rather than message exchange.

FormatEncodingSchema requiredTypical strength
JSONTextNoHuman readability and universal support
MessagePackBinaryNoCompact messages without schema management
CBORBinaryNoIETF standard, used in IoT and security protocols
Protocol BuffersBinaryYesSmallest messages and strong typing via code generation
Apache AvroBinaryYesSchema evolution in streaming and data pipelines
ParquetBinary, columnarYesCompressed analytical storage
Comparison with neighboring serialization formats

Because MessagePack stores field names inside every map, records with many repeated keys remain larger than Protocol Buffers messages, which replace names with numeric field tags. The size advantage over JSON is therefore largest for numeric and binary data and smallest for text-heavy records, where savings often fall to 10 to 30 percent before compression.

Where MessagePack is used

MessagePack fits systems that exchange many small, loosely structured messages between services written in different languages. It is the wire format of the Neovim remote plugin API, an optional protocol in ASP.NET Core SignalR, the forward protocol of the Fluentd log collector and a supported format in Redis Lua scripting.

SituationRecommended choiceReason
Public web API for third partiesJSONReadability and tooling matter more than size
Internal RPC between polyglot servicesMessagePackCompact, fast and no schema coordination
Cache values in Redis or MemcachedMessagePackSmaller memory footprint than JSON strings
Strict contracts across many teamsProtocol BuffersSchemas enforce compatibility
Large analytical datasetsParquetColumnar compression and predicate pushdown
Choosing MessagePack by situation

Limitations and common mistakes

The absence of a schema is both the main benefit and the main risk. Without a schema, nothing enforces field names or types, so producers and consumers can drift apart silently. Teams that rely on MessagePack for long-lived data usually add validation at the application layer or a versioning convention.

Other pitfalls are practical. Binary payloads are not human-readable, so debugging requires a decoder. Older libraries predating the 2013 specification revision did not distinguish strings from binary data, causing compatibility problems between old and new implementations. Decoders exposed to untrusted input must limit declared lengths and nesting depth, because a forged header can request gigabytes of memory.

A plain-language summary

MessagePack is a way for programs to send each other structured information in a compact form. JSON writes data as readable text, like a letter spelled out in full. MessagePack writes the same content in a shorthand that computers read very quickly.

The content stays the same: lists, labeled values, numbers and text. Only the packaging changes. The result is smaller messages and faster processing, at the cost of data that people cannot read without a tool.

An example from practice

An online gaming company runs a backend in which a Go matchmaking service and a Python analytics service exchange player state through a message queue. Each message holds a player identifier, position coordinates, a timestamp and a small binary blob with inventory data. At peak load the queue carries about 200,000 messages per second.

With JSON, the inventory blob had to be Base64-encoded and every coordinate was written as decimal text, giving an average message of about 410 bytes. After switching to MessagePack, coordinates became 32-bit floats, the blob traveled as native binary and the average message fell to about 240 bytes.

Network traffic on the queue dropped by roughly 40 percent, and decoding time in the Python service fell noticeably. Because MessagePack required no schema, the team migrated each service independently within two weeks, adding a version field to every map to keep producers and consumers compatible.