Data Serialization Formats
Basics
Visual Representation
What is it?
π¦ Think of it like packing for a move:
When you move houses, you can't just teleport your furniture. You need to PACK it into boxes, label them, transport them, and UNPACK at the new house.
Serialization is the same thing for data. When your app wants to send data (like a user profile) across the internet, it can't send a live JavaScript object through a wire. It must PACK the data into a format (like JSON or Protobuf), send those bytes, and the receiving end UNPACKS it back into usable data.
Different formats are like different box sizes:
π‘ Simple Summary: Serialization = converting data into a format that can be stored or sent over a network. JSON (human-readable, flexible) vs Protobuf (binary, fast, compact).
How it works β Like you're watching it happen
When Instagram app fetches your profile:
{"name":"Prakshay","followers":500,"posts":42}With Protobuf (binary):
β But wait β if Protobuf is faster and smaller, why doesn't everyone use it?
JSON is UNIVERSALLY readable. Open a JSON file β you can read it instantly. Open a Protobuf file β binary gibberish. For public APIs (consumed by millions of developers), JSON wins because anyone can use it without installing special tools. For INTERNAL service-to-service communication (where speed matters and both sides control the code), Protobuf wins because performance > readability.
Why should you care? (Interview perspective)
Key Things to Remember
Real Examples You Use Daily
π Every website/API β When your browser calls an API, the response is almost always JSON: {"posts": [{"id": 1, "text": "hello"}, ...]}. Human-readable, easy to debug in browser DevTools.
π Uber internal β Between their 2000 microservices, Uber uses Protobuf/gRPC. Rider Service β Pricing Service β Matching Service all communicate in compact binary. At their scale, JSON would waste terabytes of bandwidth daily.
π Kafka data pipelines β When companies stream millions of events through Kafka (user clicks, purchases, logs), they often use Avro format. Schema registry ensures everyone producing/consuming data agrees on the format, even as it evolves.
π± Mobile apps β Apps like Instagram send/receive JSON from APIs. But internally, Instagram stores some data in compact binary formats in the database for efficiency. The translation (serialization/deserialization) happens at the API layer.
Common Mistakes in Interviews
β Always defaulting to JSON β For a public API? Yes, JSON. But for internal service communication at high scale? Protobuf can be 5-10x more efficient. Show you know alternatives.
β Ignoring schema evolution β "We'll use Protobuf." How do you add a new field without breaking old services? Discuss backward/forward compatibility (optional fields, field numbers).
β Forgetting compression β Even with JSON, gzip compression reduces size by 80-90%. Always mention compression for HTTP responses.
β Not considering parse time β At high QPS (100K+ requests/sec), JSON parsing becomes a significant CPU cost. Protobuf parsing is 10-100x faster. This matters for hot paths.
β Ignoring the debugging trade-off β Protobuf is faster but you can't read a Protobuf message in logs without the schema. JSON lets you debug by just reading the log. This is a real operational trade-off.
π― Interview One-Liner
"I'd use JSON for public-facing APIs prioritizing developer experience, and Protocol Buffers for internal service-to-service communication where the 3-10x size reduction and 20-100x faster parsing provide meaningful performance gains at scale β with Avro for event streaming where schema evolution is critical."
Interview Q&A
Q: JSON vs Protobuf β when would you pick each?
JSON when: public API (developer friendly), low-traffic internal tools, configuration files, anything where humans need to read the data. Protobuf when: high-throughput internal services (gRPC), mobile apps with bandwidth constraints, any hot path where parsing speed matters. The break-even point is roughly: if you're making >10K requests/sec between services, Protobuf's efficiency gains are worth the complexity.
Q: What is schema evolution and why does it matter?
Schema evolution is changing data structure over time without breaking existing services. Example: Adding a "phone_number" field to a User message. With Protobuf, you add it as a new optional field with a new number β old services that don't know about it simply ignore it. Without schema evolution, adding a field means updating ALL services simultaneously (deployment nightmare in microservices).
Q: How does Protobuf achieve smaller size than JSON?
JSON includes field NAMES in every message: {"name":"Prakshay","age":25} β "name" and "age" are text consuming bytes every time. Protobuf uses field NUMBERS (defined in schema): field 1 = name, field 2 = age. The message only contains number tags + values in binary. No field names, no quotes, no braces. Also, integers are varint-encoded (small numbers use fewer bytes).
Q: What's Avro and why is it used with Kafka?
Avro includes the schema WITH the data (or references a schema registry). When you write millions of events to Kafka, consumers joining later need to know the format. Avro's schema registry ensures: (1) All producers use a valid schema, (2) Schema changes are backward-compatible, (3) Consumers can read old AND new format messages. This is perfect for long-lived event streams where data format evolves over months.
Q: How do you handle versioning in serialization?
For Protobuf: never reuse field numbers, always add new fields as optional with new numbers, never remove required fields. For JSON APIs: use API versioning (/v1/users, /v2/users). For Avro: schema registry checks compatibility before allowing schema updates. General rule: be additive (add new fields) not destructive (remove/rename fields). Old consumers ignore new fields; new consumers handle missing old fields with defaults.
Quick Quiz
1/5Uber's microservices exchange millions of messages per second. Why would they choose Protobuf over JSON?