Reading — step 1 of 5
Read
~1 min readProduction
Production MessagePack
Real-world usage:
Library options:
- Python:
msgpack-python,umsgpack. - Go:
vmihailenco/msgpack,tinylib/msgp. - JavaScript:
msgpack-lite,@msgpack/msgpack. - Rust:
rmp,rmp-serde. - Java:
msgpack-core,msgpack-jackson.
Common pitfalls:
- Strings vs bytes: MsgPack distinguishes; pre-2013 didn't. Compatibility issues with old data.
- Integer overflow: 64-bit ints don't fit in JSON's 53-bit float. MsgPack handles native; serialization to JSON loses precision.
- NaN/Inf in floats: MsgPack handles. JSON doesn't (some libs special-case).
- Map key types: Some libraries restrict. Stringify if portability matters.
- Large messages: 32-bit length limits. Plan for streaming if approaching.
Schema validation:
- MsgPack alone has no schema. Apps must validate.
- Solutions: combine with JSON Schema, or use Protobuf if strict schema needed.
Versioning:
- No native versioning.
- App can include version field in maps:
{"v": 2, "name": ...}. - Or branch on type detection.
Performance tips:
- Reuse encoder/decoder instances (avoid allocation).
- Use pre-sized buffers.
- For known types: handcoded faster than reflection.
- Streaming for large data; avoid full-buffer allocation.
Compression:
- Already binary; gzip helps for very repeating data.
- Snappy + msgpack common for cache wire formats.
- Trade CPU for bytes.
Use cases I've seen:
- Redis values (compact storage, faster than JSON).
- Cache layer for API responses.
- Job queue payloads.
- Cross-language RPC (Tarantool, Apache Hivemall).
- IoT device → cloud data.
When NOT to use:
- Public-facing APIs (browser doesn't speak it natively).
- Human inspection (binary, not readable).
- Strict schema enforcement (use Protobuf).
Modern alternatives gaining traction:
- CBOR (RFC 8949): nearly identical, IETF-blessed.
- Cap'n Proto: zero-copy alternative if perf-critical.
For most "JSON but smaller" needs: MsgPack still solid choice.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…