In recent months, I’ve had to dive deep into serialization and its impact on system performance for professional reasons. While it may seem like a minor technical detail, choosing the right method for data exchange can have a significant effect on application efficiency, especially in distributed systems.
JSON is a standard for sending and receiving data over the network. Sending data over the network requires sending every character of JSON over the network and although it is very readable. At least, more than XML or any other format, it is not the one that sends the fewest bytes over the network.
In this article, I’ll compare three common approaches: JSON, Protocol Buffers (Protobuf), and Object Serialization (when both the client and server use the same language, such as Kotlin or Dart). I’ll also share a practical table summarizing estimated results based on size and speed.
JSON
JSON (JavaScript Object Notation) is a human-readable text-based format for structuring data. It is widely used due to its simplicity and compatibility with almost any programming language.
Example in JSON:
Advantages:
- Readable by humans, making debugging easier.
- Extremely compatible across different programming languages.
- Ideal for heterogeneous systems.
Disadvantages:
- Size: Includes redundant text labels, making it heavier.
- Speed: Parsing text is slower compared to binary formats.
- Computational Cost: Requires conversion at both the client and server.
Protocol Buffers (Protobuf)
Protobuf, developed by Google, is a binary serialization format that encodes data in a compact and efficient structure. It requires a predefined schema (.proto) for data modeling.
Example in Protobuf:
Advantages:
- Compact Size: Binary messages are much smaller than JSON.
- High Speed: Serialization and deserialization are optimized for performance.
- Efficiency: Ideal for systems exchanging large amounts of data.
Disadvantages:
- Requires a predefined schema, which adds maintenance overhead.
- Not human-readable, making debugging harder.
- Both client and server must support Protobuf.
Object Serialization
When the client and server share the same language, Object Serialization allows direct conversion of objects into a binary format using language-specific libraries.
Example in Kotlin for KMP:
Example in Dart (Serverpod):
Advantages:
- Efficiency: Comparable to Protobuf in size and speed.
- Ease of Use: No separate schema is needed; the code serves as the model.
- Speed: Avoids intermediate parsing steps like JSON.
Disadvantages:
- Limited Compatibility: Only works when the client and server share the same language.
- Tight Coupling: May depend on language or framework versions.
- If we want this binary serialization efficiency, we must check and ensure that the technology we use does not create a JSON-based encoding and decoding wrapper.
Comparative Table with Estimated Results
Feature | JSON | Protobuf | Object Serialization |
---|---|---|---|
Payload Size | ~120 bytes | ~40 bytes | ~50 bytes |
Serialization Speed | ~10 ms / 10,000 ops | ~3 ms / 10,000 ops | ~4 ms / 10,000 ops |
Deserialization Speed | ~12 ms / 10,000 ops | ~3.5 ms / 10,000 ops | ~4.5 ms / 10,000 ops |
Human Readability | High (Text) | Low (Binary) | Low (Binary) |
Interoperability | High | Medium (requires schema) | Low (same language) |
Comments
Post a Comment