Serialization and Deserialization for backend engineers
This video explains the fundamental concepts of serialization and deserialization in the context of web-based client-server communication. It highlights how different systems, often written in different programming languages, can exchange data over the network by agreeing on a common data format, focusing primarily on JSON as the most prevalent serialization standard used in RESTful HTTP APIs.
1. Client-Server Communication Basics
The video begins by setting the stage for serialization/deserialization by explaining a typical client-server setup:
- The client is usually a browser-based JavaScript application (React, Angular, Vue, etc.).
- The server may run on any backend technology and language (e.g., Rust).
- Communication happens over a network through various protocols such as HTTP (REST APIs), gRPC, or WebSocket.
- The client sends a request to the server, usually via HTTP request, which includes:
- A URL path
- Request headers
- Request body containing data payload
- The server processes this request and sends a response back to the client.
Key challenge: How does the server understand the client's data and vice versa, especially when they use different programming languages and data types?
2. The Need for a Common Data Format
Because JavaScript is dynamically typed and Rust is statically typed with different type systems, the data cannot be sent directly in native language formats. The solution is:
- Define a common serialization format (a standardized way of representing data).
- The client serializes its internal data into this common format before sending.
- The server deserializes the incoming data into its native format, processes it, and then serializes the response to send back.
- The client then deserializes the response to continue its processing/display.
This language-agnostic data exchange ensures interoperability between different systems.
3. Role of OSI Model and Network Layers (High-Level Overview)
A brief mention is made of the OSI model, which defines layered networking protocols:
- The Application Layer (top layer) is where serialization standards like JSON operate.
- Data passes through lower layers (Transport, Network, Data Link, Physical) where it is converted into packets, frames, bits, and physical signals (voltage, optical pulses).
- Backend engineers mainly deal with the application layer; lower-layer details are abstracted away.
Mental model: Application data (e.g., JSON) is converted down to bits for transmission and reassembled at the server, where it returns to the application-level format.
4. Selecting Technologies for Serialization and Communication
Given the broad spectrum of backend technologies, the video adopts a practical focus on:
- HTTP REST APIs as the primary communication protocol (due to their widespread use).
- JSON (JavaScript Object Notation) as the serialization standard for data exchange (used 80%+ of the time).
- For databases, they select PostgreSQL as a popular relational database example.
- Other serialization formats exist, including:
- Text-based: YAML, XML, JSON
- Binary-based: Protocol Buffers (Protobuf), Avro, etc.
But the focus remains on JSON for simplicity and relevance.
5. Details about JSON as a Serialization Standard
JSON is described as:
- A human-readable, text-based serialization format.
- Originally based on JavaScript object syntax but language independent.
- Widely used not only for client-server transmission but also in configuration files and logs.
- The structure includes:
- Data enclosed in curly braces
{ } - Keys as strings inside double quotes
"key" - Values can be strings, numbers, booleans, arrays, or nested objects.
- Data enclosed in curly braces
JSON Format Rules Summary
JSON’s simplicity and readability make it a preferred choice for API data transmission.
6. Practical Example of JSON Serialization/Deserialization
The video revisits a previously shown API example to highlight serialization in action:
- A client sends a POST request with a JSON body containing book details (
id,title,author).
Example JSON request:
{
"id": 1,
"title": "Book Title",
"author": "Author Name"
}
- The server receives this JSON, parses it into its own data structure (e.g., a Rust struct), and processes it.
- The server responds with a JSON payload containing an array of book objects.
- The client receives this JSON and renders the data (e.g., displaying a list).
This cycle demonstrates full round-trip serialization and deserialization using JSON.
7. Summary and Key Insights
- Serialization and deserialization are processes of converting data to and from a common standard format to enable language- and platform-agnostic communication.
- This standardization is crucial for interoperability between diverse systems (JavaScript frontend and Rust backend, for example).
- The application layer handles serialized formats like JSON, while the underlying network stack manages data transmission transparently.
- JSON is the most prevalent serialization format in web APIs due to its simplicity, readability, and wide support.
- Being familiar with the HTTP protocol and JSON serialization is fundamental for backend developers and helps build a strong foundation to learn other protocols and formats later.
Glossary of Relevant Terms
Conclusion
The video delivers a clear and practical understanding of how data interoperability between client and server works through serialization and deserialization, anchored on JSON and HTTP as industry-standard technologies. By mastering these concepts, backend engineers can effectively design, build, and troubleshoot communication across different systems and environments.