Why ditch JSON? 4 binary formats that can make your API up to 5× faster
The article explains why JSON can become a performance bottleneck in high‑throughput APIs and compares four binary serialization formats—Protocol Buffers, FlatBuffers, MessagePack, and CBOR—showing benchmark results, trade‑offs, and a practical migration checklist.
Why JSON struggles in high‑performance scenarios
JSON is popular for its readability, but as a text format it requires CPU‑intensive parsing and creates large payloads, increasing network latency and memory pressure, especially on mobile or embedded devices.
Four high‑performance data formats and their use cases
1. Protocol Buffers: strong‑typed, compact, efficient
Protobuf, developed by Google, uses a .proto schema to describe data structures.
Typical scenarios: strong‑typed contracts, service‑to‑service RPC, micro‑services with stable schemas.
syntax = "proto3";
message User {
int64 id = 1;
string name = 2;
string email = 3;
string region = 4;
repeated string roles = 5;
}Python example:
# Define Protobuf structure
user = User(id=42, name='Ada', email='[email protected]', region='AP', roles=['dev'])
# Serialize
data = user.SerializeToString()
# Deserialize
user2 = User()
user2.ParseFromString(data)Performance: p50 latency drops from 120 ms to 20 ms (≈6× faster); payload size shrinks 4–6×.
Pros: compact binary, cross‑platform, forward/backward compatible.
Cons: requires schema management and code generation.
2. FlatBuffers: zero‑copy reads for hot data paths
FlatBuffers, also from Google, allows direct access to serialized data without parsing.
Typical scenarios: frequently read hot paths, game servers, real‑time streams, mobile UI pipelines.
Direct access: no parsing step, data can be read in‑place.
Memory efficient: only the buffer is needed, minimal allocations.
Near‑native speed: low latency, only slight overhead for schema evolution.
Performance: p50 latency drops from 120 ms to 17.1 ms (≈7× faster); memory allocation drops dramatically, stabilising p99 latency.
Unique value: binary buffer can be accessed directly while still supporting schema evolution.
3. MessagePack: binary alternative that retains JSON flexibility
MessagePack is a compact binary format described as “like JSON but fast and small.”
Typical scenarios: systems needing JSON‑like flexibility with better performance, quick migrations without strict schema enforcement.
Python example:
import msgpack
# Original data
data = {'name': 'Alice', 'age': 30, 'is_student': False}
# Serialize
packed_data = msgpack.packb(data)
print("Serialized:", packed_data)
# Deserialize
unpacked_data = msgpack.unpackb(packed_data)
print("Deserialized:", unpacked_data)Binary format: smaller and faster than text.
Cross‑language support: JavaScript, Python, Java, etc.
Easy to use: API similar to JSON, low entry barrier.
Performance: p50 latency drops from 120 ms to 34.3 ms (≈3.5× faster); payload size reduces 2–4×.
4. CBOR: ideal for constrained devices
CBOR (Concise Binary Object Representation) is an IETF standard designed for low‑memory, low‑bandwidth environments, inspired by MessagePack.
Typical scenarios: IoT devices, low‑end mobile clients, battery‑sensitive apps, bandwidth‑limited networks.
Python example:
import cbor2
obj = {'id': 42, 'temp': 23.5}
data = cbor2.dumps(obj)
obj2 = cbor2.loads(data)Compact binary encoding: built for low memory and stream processing.
Standardized protocol: IETF‑backed, long‑term support.
JSON compatibility: any valid JSON is also valid CBOR.
Performance: p50 latency drops from 120 ms to 34.3 ms (≈3.5× faster); in federated learning use‑cases, message size is 75 % smaller than JSON alternatives.
How to choose the right format
Need strong‑typed contracts and backward compatibility: choose Protocol Buffers.
Need extreme read performance and zero‑copy: choose FlatBuffers.
Want JSON flexibility with better performance: choose MessagePack.
Target constrained devices or low‑bandwidth environments: choose CBOR.
A practical deployment strategy is to keep JSON on public APIs while using binary formats for internal services or mobile RPC.
Practical deployment checklist
Measure first: capture real‑world JSON p50/p99 latency before optimizing.
Select a single endpoint: pick a frequently called, latency‑sensitive API.
Prototype in a test environment: replace JSON only for that endpoint, keep a feature flag.
End‑to‑end measurement: verify client p50/p99 latency, payload size, and CPU usage.
Add compatibility support: support both JSON and binary during migration, negotiate content type.
Document schemas: for Protobuf or FlatBuffers, maintain version rules and changelogs.
Roll out gradually: watch for decoding errors and outdated SDKs.
Final thoughts
Binary formats are tools, not dogma. Use them when latency, bandwidth, or CPU are critical, but always keep compatibility and observability in mind. Replacing a single high‑traffic internal RPC from JSON to a binary format can provide the most compelling performance evidence.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Data STUDIO
Click to receive the "Python Study Handbook"; reply "benefit" in the chat to get it. Data STUDIO focuses on original data science articles, centered on Python, covering machine learning, data analysis, visualization, MySQL and other practical knowledge and project case studies.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
