Why Mir Uses Protocol Buffers
Protocol Buffers (protobuf) is at the heart of Mir's communication architecture. This choice was made after careful consideration of the unique requirements of IoT systems and the need for a robust, efficient, and scalable communication protocol.
1. Compact Binary Format
Protocol Buffers use a variable length binary encoding that dramatically reduces message size compared to JSON or XML:
JSON Example (78 bytes):
{
"deviceId": "weather-001",
"timestamp": 1640995200000,
"temperature": 23.5,
"humidity": 65.2
}
Protobuf Equivalent (~30 bytes):
[Binary representation - 60% smaller]
Impact: Smaller messages mean reduced bandwidth usage, lower data costs, and faster transmission over cellular networks.
2. High Performance
Protocol Buffers are optimized for speed with minimal CPU overhead:
- Serialization: 3-10x faster than JSON
- Deserialization: 2-5x faster than JSON
- Memory Usage: 2-3x less memory than JSON parsing
Impact: Extends battery life and enables real-time processing on resource-constrained devices or software.
3. Strong Type Safety
Protocol Buffers enforce strict typing at compile-time, preventing runtime errors that could crash devices:
message Telemetry {
google.protobuf.Timestamp timestamp = 1;
float temperature = 2; // Enforced as float, not string
float humidity = 3;
}
Impact: Reduces debugging time and prevents field failures due to type mismatches.
4. Multi-Language Support
Protocol Buffers generate idiomatic code for all programming languages.
Impact: Enables device development in C/C++, server development in Go, and client applications in Python/JavaScript.
Mir's Protobuf Architecture
Schema-First Design
Mir enforces a schema-first approach where device capabilities are defined upfront:
// Device schema defines the contract
message WeatherTelemetry {
option (mir.device.v1.message_type) = MESSAGE_TYPE_TELEMETRY;
google.protobuf.Timestamp timestamp = 1;
float temperature = 2;
float humidity = 3;
float pressure = 4;
}
message PowerCommand {
option (mir.device.v1.message_type) = MESSAGE_TYPE_TELECOMMAND;
bool enable = 1;
}
message DataRateProp {
option (mir.device.v1.message_type) = MESSAGE_TYPE_TELECONFIG;
int32 data_rate = 1;
}
Enable discovery of telemetry, commands and configuration by the system to provide a unified management platform.
Allows Mir to generate:
- database query to process and ingest data
- dashboards to visualize telemetry
- commands and configuration discovery
- commands and configuration templates
- and more...
The Protobuf schema approach also allows developers the flexibility of defining the schema they need rather then fix templates as propose by other solution.
The code generation also offer fast development time and offers strong type satefy.
Protobuf Files Management
Mir offers two approaches for managing Protocol Buffer files: the traditional protoc compiler and the modern buf tool. While both work seamlessly with Mir, buf is strongly recommended for new projects due to its superior developer experience and modern workflow.
buf advantages:
- Faster compilation with intelligent caching and parallel processing
- Built-in linting catches common protobuf issues before they become problems
- Dependency management handles external proto dependencies automatically
- Breaking change detection prevents accidental API changes
- Better error messages with clear guidance on how to fix issues
- Simplified configuration with declarative YAML files instead of complex command-line flags
protoc advantages:
- Wider ecosystem support with broader tooling compatibility
- Lower learning curve for teams already familiar with protoc workflows
- Direct control over compilation flags and plugin options
Conclusion
Protocol Buffers provide the perfect balance of performance, safety, and flexibility that IoT systems require. By choosing protobuf, Mir ensures:
- Devices can communicate efficiently with minimal resource usage
- Developers have type safety and excellent tooling support
- Operators can deploy and scale systems with confidence in a uniform platform
- Systems can evolve gracefully as requirements change
The investment in protobuf schema design pays dividends in reduced bugs, improved performance, and simplified operations across the entire IoT ecosystem.
Mir