This chapter revolves around two core questions:
- How data is encoded
- How data flows through systems
These questions may seem basic, but they directly determine a system's evolvability, compatibility, and long-term maintenance costs. This post attempts a systematic review of both from an engineering-practice perspective.
1. How Is Data Encoded?
In distributed systems, data is almost always being converted back and forth between in-memory structures and byte sequences. The choice of encoding determines whether data can be used safely, efficiently, and over the long term.
1. Language-Specific Encoding
Many programming languages provide built-in object serialization, for example:
- Java:
java.io.Serializable - Python:
pickle - Ruby:
Marshal
Pros
- Convenient to use
- Requires almost no extra modeling
- Lets you save and restore in-memory objects quickly
Cons
- Tightly coupled to the language
Data can barely be parsed safely by other languages, which severely limits room for system evolution. - Security risk
Deserializing an arbitrary byte stream can lead to remote code execution. - Poor evolvability
Support for forward/backward compatibility is insufficient. - Performance and size issues
Java's native serialization is a typical example: large payloads and poor performance.
👉 Conclusion: Only suitable for temporary use, not for long-term data storage or cross-service communication.
2. Text Encoding Formats (JSON / XML / CSV)
Common text formats include:
- JSON
- XML
- CSV
Pros
- Human-readable
- Broad cross-language support
- Well suited to data exchange between systems
Main issues
- Unclear numeric semantics
JSON cannot distinguish integers from floating-point numbers, and large integers have precision issues in JavaScript. - No support for binary data
Binary data can only be encoded indirectly via Base64, which inflates size by about 33%. - Schemas are complex or missing
- XML / JSON Schema are powerful but complex
- CSV has no schema at all; the meaning of rows and columns must be maintained manually
- Encoding/decoding logic tends to get scattered throughout the codebase
3. Binary Encoding Formats
To improve efficiency and reduce storage size, many binary encoding schemes have emerged, mainly including:
- MessagePack
- Protocol Buffers
- Avro
MessagePack
- Essentially a binary form of JSON
- Keeps field names
- Limited size savings, but faster parsing
Protocol Buffers
- Based on an explicit schema
- Uses field numbers (tags) instead of field names
- Naturally supports forward/backward compatibility
- Well suited to RPC use cases (such as gRPC)
Avro
- Does not store field identifiers in the data
- Decodes using the writer's schema (Writer Schema) and the reader's schema (Reader Schema)
- Very friendly to dynamically generated schemas
- Widely used in data pipelines and big data systems
👉 Overall conclusion:
Schema-based binary encoding is key infrastructure for building systems that can evolve over the long term.
2. How Does Data Flow?
Data encoding does not exist in isolation; it is always accompanied by how data flows.
1. Flowing Through Databases
A database can be thought of as sending a message to your future self.
Key questions
- Data is written in different formats at different times
- Data lives longer than code
- New code must be able to read old data (backward compatibility)
- Multiple versions of an application access the database at the same time
- During rolling upgrades, old and new code coexist
- The data format must support forward compatibility
- Archival and analytical storage
- Data is usually written once and then becomes immutable
- Formats that are friendly to analytics, such as Avro and Parquet, are a better fit
2. Flowing Through Services: REST and RPC
Web / REST Services
- Based on HTTP
- Usually uses JSON
- APIs are defined via OpenAPI / Swagger
- Adding fields is usually compatible
RPC
- Tries to make remote calls “look like local functions”
- Common implementations:
- gRPC (Protocol Buffers)
- Avro RPC
Real-world problems with RPC
- Unreliable networks
- Timeouts and retries
- Idempotency issues
- Unpredictable latency
👉 Conclusion:
RPC is not a “remote function call”; it is a distributed interaction that can fail.
3. Load Balancing and Service Discovery
To handle dynamically changing service instances, systems usually introduce:
- Hardware/software load balancers
- DNS
- Service discovery systems (such as Consul)
- Service meshes (Istio / Linkerd)
Through the sidecar pattern, service meshes separate the following from business code:
- Load balancing
- Encryption
- Observability
4. Durable Execution and Workflows
In complex business flows, a single request often involves calls to multiple services.
The problem: how do you guarantee “execute only once”?
This is precisely the core value of durable execution frameworks:
- Workflows are scheduled by a workflow engine
- All RPCs and state changes are persisted
- Execution can be replayed after a failure
- Externally, this appears as exactly-once semantics
A typical example: Temporal
Challenges
- Requires a very high degree of code determinism
- Changing execution order can break replay
- Usually requires versioned, isolated execution
5. Event-Driven Architecture
Another important way data flows is through asynchronous messages:
- Message queues / event brokers
- Publish-subscribe model
- Senders and receivers are decoupled
Advantages
- Improves system resilience
- Naturally supports smoothing out peak traffic
- Well suited to building loosely coupled systems
Points to watch
- Messages are still byte sequences at heart
- Schema evolution still has to be handled
- You need to prevent unknown fields from being lost
Summary
Data encoding and data flow are not low-level details; they are core pillars of system evolvability.
- The encoding determines whether data can be used safely over the long term
- The way data flows determines whether systems can evolve independently
- Forward/backward compatibility is a basic survival skill for distributed systems
Data outlives code.
When we design systems, we are essentially paving the way for future versions of the system.