Skip to main content

Command Palette

Search for a command to run...

Serialization and Deserialization: From First Principles

Published
4 min read
Serialization and Deserialization: From First Principles
S

Software developer with a strong foundation in React, Node.js, PostgreSQL, and AI-driven applications. Experienced in remote sensing, satellite image analysis, and vector databases. Passionate about defense tech, space applications, and problem-solving. Currently building AI-powered solutions and preparing for a future in special forces.

Why Data Needs to Be Translated Before It Can Travel

Modern backend systems are fundamentally about communication.

A frontend written in JavaScript talks to a backend written in Python, Java, or Go.
These systems may run on different machines, different operating systems, and different architectures.

Yet, somehow, they understand each other.

This blog answers a deceptively simple question:

How does data move from one program to another across a network, and still make sense on the other side?

The answer lies in serialization and deserialization.

1. The Core Problem: Programs Can’t Send Objects

Let’s start from first principles.

A program works with in-memory objects:

  • JavaScript objects

  • Python dictionaries

  • Java classes

These objects:

  • Live in RAM

  • Have language-specific structure

  • Depend on compiler/runtime internals

Important Insight
Memory structures are not portable. You cannot take a Python object in memory and “send” it directly to another machine.

Networks only transmit bytes.

So the fundamental problem is:

How do we convert rich in-memory data structures into a sequence of bytes — and back again?

2. Client–Server Communication Basics

In a typical backend system:

  • A client (browser, mobile app) sends a request

  • A server processes it and sends a response

  • They communicate using protocols like:

    • HTTP / REST

    • WebSockets

    • gRPC

Let’s focus on HTTP for simplicity.

For example:

GET /users
POST /users { data }

The client and server must agree on how data is represented.

Agreement is impossible without standardization.

3. Why Raw Memory Cannot Cross the Network

Imagine this JavaScript object on the frontend:

{
  name: "Shiv",
  age: 22
}

Internally:

  • JavaScript represents this in a JS engine–specific memory layout

  • Python represents dictionaries completely differently

Critical Constraint
Different languages, runtimes, and CPUs interpret memory differently.

So instead of sending memory, we send data representations.

4. What Is Serialization?

Serialization is the process of converting an in-memory data structure into a standardized format that can be transmitted or stored.

Formally:

Object → Byte Stream

Intuition
Serialization is like converting spoken language into written text so it can be mailed.

5. What Is Deserialization?

Deserialization is the reverse process of converting received bytes back into an in-memory data structure.

Formally:

Byte Stream → Object

Intuition
Deserialization is reading the letter and reconstructing the meaning in your head.

6. Where Serialization Fits in the Network Stack

Your notes correctly hint at the OSI model.

Conceptually:

  • Application Layer → structured data (objects)

  • Transport & Network Layers → raw bytes

  • Physical Layer → electrical signals

Serialization happens at the application boundary.

7. Text vs Binary Serialization Formats

Not all serialization formats are the same.

Text-Based Formats

Examples:

  • JSON

  • XML

  • YAML

Characteristics

  • Human-readable

  • Easy to debug

  • Language-agnostic

  • Larger payload size

  • Slower parsing

Binary Formats

Examples:

  • Protocol Buffers (Protobuf) used in gRPC

  • Avro

  • MessagePack

Characteristics

  • Compact

  • Faster to parse

  • Strict schemas

  • Not human-readable

Rule of Thumb

  • Public APIs → JSON

  • Internal microservices → Protobuf

8. JSON as a Concrete Example

Example

{
  "name": "Shiv",
  "age": 22
}

On the Frontend (JavaScript)

const data = {
  name: "Shiv",
  age: 22
};

const serialized = JSON.stringify(data);

On the Backend (Python)

import json

data = json.loads(serialized)

What happened?

  • JavaScript object → JSON string → bytes over network

  • Bytes → JSON string → Python dictionary

That conversion process is serialization + deserialization.

9. Under the Hood: What Actually Happens

Step-by-step:

  1. Client creates an object

  2. Object is serialized into JSON text

  3. JSON is encoded into bytes (UTF-8)

  4. Bytes travel over TCP/IP

  5. Server receives bytes

  6. Bytes decoded into text

  7. Text parsed into native object

Important Insight
Serialization is not the same as transport.
Transport moves bytes. Serialization gives bytes meaning.

10. Trade-offs, Edge Cases, and Common Mistakes

Common Mistakes

  • Assuming order in JSON objects

  • Sending language-specific types (Date, BigInt)

  • Forgetting versioning

  • Breaking backward compatibility

Edge Cases

  • Floating point precision

  • Character encoding issues

  • Optional vs required fields

  • Schema evolution

11. Real Backend Use Cases

Serialization appears everywhere:

  • REST APIs (JSON)

  • gRPC microservices (Protobuf)

  • Message queues (Kafka, RabbitMQ)

  • Database storage

  • Caching (Redis)

Backend Reality
Most backend performance issues at scale involve serialization overhead, not business logic.

12. Summary

Let’s compress everything into first principles:

  • Programs cannot send memory

  • Networks only move bytes

  • Serialization converts objects → bytes

  • Deserialization converts bytes → objects

  • Standards (JSON, Protobuf) make communication possible

  • Every backend system depends on this abstraction

If backend systems are cities, serialization is the language everyone agrees to speak.

3 views

Building Backend Systems from Zero

Part 2 of 3

A backend engineering series exploring systems from first principles starting with why problems exist before how they’re solved. As I learn, I document my journey through APIs, databases, auth, scalability, and distributed systems.

Up next

Understanding HTTP from First Principles

Why the Web Works, Why HTTP Is Stateless, and Why That Was a Brilliant Decision