Data Serialization Formats

Basics

Visual Representation

Rendering diagram...

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:

  • JSON = standard moving boxes (everyone uses them, easy to read labels, but bulky)
  • Protocol Buffers (Protobuf) = vacuum-sealed bags (compact, efficient, but need a packing guide to open)
  • XML = those giant oversized boxes with excessive labeling (old school, verbose, nobody likes moving with these)
  • πŸ’‘ 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:

  • Server has data in memory β€” A User object: {name: "Prakshay", followers: 500, posts: 42}
  • Serialization (packing) β€” Server converts this to JSON text: {"name":"Prakshay","followers":500,"posts":42}
  • Sent over network β€” These bytes travel through the internet to your phone.
  • Deserialization (unpacking) β€” Your phone's app reads the JSON text and recreates a User object it can display on screen.
  • With Protobuf (binary):

  • Same data β€” But packed into compact binary: just 26 bytes vs JSON's 50 bytes.
  • Needs a schema β€” Both sides must have the .proto file defining the structure (like a packing diagram).
  • Can't read raw β€” If you intercept the bytes, you see gibberish (not human-readable). But it's 2x smaller and 10x faster to parse.
  • ❓ 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)

  • 🎯 "How do your services communicate?" β€” data format choice matters for performance
  • Understanding JSON vs Protobuf helps in microservices design discussions
  • Schema evolution (adding fields without breaking old code) is a common interview topic
  • Shows you think about efficiency at scale (small savings Γ— billions of requests = huge impact)
  • Key Things to Remember

  • JSON β€” Text-based, human-readable, no schema needed (flexible), universal support. But: verbose (field names repeated in every message), slow to parse (textβ†’data conversion), larger size.
  • Protocol Buffers β€” Binary, compact, requires .proto schema, strongly typed. 3-10x smaller than JSON, 20-100x faster to parse. Used by gRPC.
  • MessagePack β€” "JSON in binary." Compatible with JSON structure but binary-encoded. Smaller than JSON, no schema needed. A good middle ground.
  • Avro β€” Popular in Apache Kafka/data pipelines. Schema included WITH the data. Good for evolving schemas over time.
  • XML β€” Old, verbose, mostly legacy. HTML is a form of XML. Still used in SOAP web services (enterprise/banking).
  • Schema evolution β€” Adding new fields to Protobuf/Avro won't break old consumers (they ignore unknown fields). This is crucial for microservices where not all services upgrade simultaneously.
  • Schema registry β€” Central place to store and version schemas. Kafka + Avro often uses Confluent Schema Registry. Ensures producers and consumers agree on data format.
  • Compression β€” Even after serialization, you can compress (gzip, zstd). A 100KB JSON response becomes 10KB after gzip. Most HTTP traffic is gzipped.
  • Speed comparison β€” JSON parse (JavaScript): ~100MB/s. Protobuf parse: ~1-2 GB/s. At millions of requests/sec, this difference is SIGNIFICANT.
  • When to use what β€” Public API: JSON. Internal microservices: Protobuf/gRPC. Data pipelines/Kafka: Avro. Config files: YAML/JSON. Legacy enterprise: XML.
  • 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/5

    Uber's microservices exchange millions of messages per second. Why would they choose Protobuf over JSON?