Quick revision
The short answer
I would standardise on a schema-based format with generated code on every side, Protocol Buffers by default, FlatBuffers where zero-copy reads matter and CBOR for the smallest devices, so nobody hand-writes offsets again. Where a raw layout must stay, it gets one written specification with byte order, sizes, padding and a version field, generated or shared parsers, and test vectors that the Java and C++ suites both run. Every message carries a magic number and version so readers reject what they do not understand.
What to remember
In this answer, we saw that hand-written layouts break because the format lives only in code and cannot evolve. The standard I would set is a schema-based format with generated code, Protocol Buffers by default, a single written specification and shared parsers where raw layouts must stay, a magic number and version on every message, and shared test vectors run by both the Java and C++ suites. On the JVM, MemoryLayout describes native structs with explicit order and padding. The rollout is owned, checked in CI and measured by incidents.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would standardise on a schema-based format with generated code on every side, Protocol Buffers by default, FlatBuffers where zero-copy reads matter and CBOR for the smallest devices, so nobody hand-writes offsets again. Where a raw layout must stay, it gets one written specification with byte order, sizes, padding and a version field, generated or shared parsers, and test vectors that the Java and C++ suites both run. Every message carries a magic number and version so readers reject what they do not understand.
Why this question is asked
Hand-written binary layouts fail in a predictable way: one team adds a field or widens a type, the other team learns about it from a production incident, and nobody can say which side was right because the format exists only in two codebases. When it happens with every change, the problem is no longer a bug; it is the absence of a contract. Interviewers ask this design question to see whether we can turn repeated incidents into a standard that teams with different languages and constraints will actually adopt. In this answer, we will look at why hand-written layouts keep breaking, compare the schema formats, design the rules for layouts that must stay raw, add versioning and shared test vectors, show how the Java side can describe native structs safely, and plan the rollout.
1Why Hand-Written Layouts Keep Breaking
In a hand-written layout, the format is implied by code: a C++ struct on one side and a sequence of ByteBuffer reads on the other. Every fact that matters, byte order, field sizes, padding, signedness, the meaning of each field, must be kept in sync by people reading each other's code. Nothing fails at compile time when they drift apart; the reader just decodes garbage.
There is also no way to evolve. If version 2 adds a field, an old reader cannot tell a version 2 message from a corrupt version 1 message. So every change needs a coordinated release of all producers and consumers, which with IoT devices in the field is impossible. Those two facts explain why every change causes an incident, and they tell us what the standard must provide: one source of truth for the format, and safe evolution.
2Choosing a Schema Format
A schema format describes messages in a small language and generates the encoding and decoding code for each programming language, so the layout cannot drift. The main candidates have different strengths.
| Format | Strengths | Watch out for |
|---|---|---|
| Protocol Buffers | compact, mature, great Java/C++ support, field-number evolution | must parse before reading fields |
FlatBuffers | read fields in place without parsing; good for large or hot messages | more awkward API, larger messages |
| Avro | schema evolution with a registry; strong in Kafka pipelines | less common on microcontrollers |
| CBOR | self-describing binary JSON; tiny encoders for constrained devices | no generated types unless paired with a schema (CDDL) |
| Documented raw layout | zero overhead; fits fixed hardware frames | needs spec, versioning and test vectors |
For most service-to-service and device-to-cloud messages, I would pick Protocol Buffers as the default: it is small on the wire, has mature generators for Java and C++, and implementations such as nanopb run on microcontrollers with a few kilobytes of RAM. Its evolution rules are simple and enforceable: fields are identified by numbers, new fields are optional, and a removed field's number is reserved and never reused. FlatBuffers is the exception for large messages read on hot paths, where avoiding a parse step matters. CBOR suits devices too small even for generated Protocol Buffers code.
3When a Raw Layout Must Stay
Some layouts cannot change: a sensor's firmware that cannot be updated, a hardware frame, a legacy file format. For those, the standard is a single specification document per layout, kept in version control, that lists every field with offset, size, signedness, byte order and unit, including explicit padding fields. From that specification we generate, or at least centrally write, one parser per language, published as a shared library, so services stop writing their own.
Every message, raw or schema-based, starts with a small header: a magic number that identifies the format, and a version number. A reader that sees an unknown magic or a version it does not support rejects the message loudly and counts it in a metric, instead of decoding garbage and passing it on.
4Shared Test Vectors and Describing Native Structs in Java
Test vectors are the cheapest, most effective part of the standard. For each message type and version, we keep a set of sample byte arrays, written as hex files, together with the values they must decode to. Both the C++ and the Java test suites load the same files from a shared repository. When someone changes a field, either the vectors change in the same pull request, visible to every team, or a test fails before release.
On the JVM, the Foreign Function and Memory API, final since Java 22, lets us describe a native struct with MemoryLayout instead of hand-typed offsets. Padding becomes an explicit element and each field gets its order, so the layout reads like the specification.
The unaligned variants are used because a segment over a Java byte array only guarantees 1-byte alignment; the trailing 0L is the base offset that layout VarHandles take since Java 22. Check your version if you are on an older JDK, where the API was in preview with slightly different signatures.
5Rolling the Standard Out
A standard that is only a document will be ignored. I would give it an owner, put the schemas and specifications in one repository with review from both the Java and C++ sides, and add a CI check that rejects incompatible schema changes, such as reusing a field number, using tools like buf for Protocol Buffers. Migration goes by risk: the message types behind the most incidents move first, and during the transition a new version field lets old and new formats coexist on the same channel. We measure success by the number of format-related incidents per quarter and by how many releases needed lock-step coordination.
- Inventory every binary message and its incident history
- Pick the format per messageProtocol Buffers default; raw spec only when forced
- Add magic and version to every messagereaders reject unknown versions
- Publish schemas, specs and test vectors in one repositoryboth test suites use them
- Migrate the noisiest message types firstold and new coexist by version
- Track format incidents per quarterthe measure of success
6 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why not just use JSON everywhere?
JSON is fine between services where size and CPU do not matter, but it is large and slow to parse on constrained devices, has no integer size guarantees, and without a schema it drifts just like a hand-written layout. A binary schema format gives both compactness and a contract.
How do you evolve a Protocol Buffers message safely?
Add new fields with new numbers, never change a field's number or type, and reserve the numbers and names of removed fields. Old readers ignore unknown fields and new readers see default values for missing ones, so producers and consumers can upgrade at different times.
Who owns the specifications?
A named platform or integration team owns the repository and its review rules, but changes come from the teams that need them, reviewed by both language sides. Ownership means the process and the tooling, not writing every schema.
7Conclusion
In this answer, we saw that hand-written layouts break because the format lives only in code and cannot evolve. The standard I would set is a schema-based format with generated code, Protocol Buffers by default, a single written specification and shared parsers where raw layouts must stay, a magic number and version on every message, and shared test vectors run by both the Java and C++ suites. On the JVM, MemoryLayout describes native structs with explicit order and padding. The rollout is owned, checked in CI and measured by incidents.
Open with
The root cause is that each format exists only as code on two sides, so nothing catches drift and nothing supports versioning. I would standardise in layers. First, for new and changeable messages, a schema-based format with generated code on both sides: Protocol Buffers as the default because it is compact, mature for Java and C++, and has nanopb for microcontrollers; FlatBuffers where we need to read large messages without parsing; CBOR for the tiniest devices.
Then, if they want more
Its evolution rules, new field numbers and never reusing old ones, let devices and services upgrade at different times. Second, for layouts that cannot change, like old firmware, one written specification per layout with offsets, sizes, byte order, padding and units, and one shared parser library instead of one per team.
Third, every message gets a magic number and version so readers reject what they do not understand. Fourth, shared test vectors, hex samples with expected values, that both the Java and C++ test suites run, so a change cannot ship without both sides seeing it.
On the Java side I would describe native structs with the FFM API's MemoryLayout, which makes order and padding explicit. Then I would roll it out by risk, starting with the message types behind the most incidents, with CI checks on schema changes, and measure format incidents per quarter.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 22
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Several Java services exchange binary data with C++ systems and IoT devices using...": https://scrivio-interview-library.netlify.app/java/io-and-nio/f49137fe20/