Avro and Protobuf Doubles Are the Same Eight Bytes (and the Silent Break Is in the Integers)

I wrote the number 1.5 into an Avro record and into a protobuf message and compared the eight bytes each one put on disk, because the float was supposed to be the obvious place the two formats would disagree. They do not. Both of them wrote 000000000000f83f, the least significant byte first, the way an x86 machine stores a double in memory. I checked it three ways, in the JavaScript Avro library, in the JavaScript protobuf library, and against the real protoc C++ compiler, and all three wrote the same eight bytes, and reading them back in either direction hands you exactly 1.5, no drift, no exception. The place where the two formats look most different on paper is the one place where they agree on the wire, because both of them just hand the IEEE 754 bit pattern to the CPU and let it fall out in native byte order. So if you are trying to read an Avro file with protobuf eyes, the float will not be what trips you up.

The integers are what trip you up, and they do it quietly. Every integer in Avro uses a zigzag varint, the same folding that protobuf reserves for its sint32 and sint64, so a minus one is the single byte 01 and a 42 is the single byte 54. Protobuf's ordinary int32 and int64 do not zigzag, they sign into a two's complement varint, so the same minus one is ten bytes long, 08ffffffffffffffffff01. There are no tags and no field numbers anywhere in an Avro stream, the fields just sit next to each other in the order the schema declares them, so nothing in the bytes announces which encoding is in play. Feed an Avro minus one, the byte 01, to a protobuf int64 reader and it hands you plus one, no error. Feed the Avro 42, the byte 0x54, to that reader and it hands you 84. Feed the protobuf 42, 0x2a, back to an Avro long reader and it hands you 21. The only time it gets loud is when the protobuf ten-byte minus one reaches an Avro long reader, and then it throws. Every one of those comebacks is a plausible number, and none of them is the one you wrote.

The 64-bit boundary is where the two JavaScript libraries split into opposite failure modes, and it is the cleanest signal you will get about which format you are actually holding. Protobuf in the browser silently rounds: a 13-digit id over 2 to the 53 quietly becomes a different id on the encode path, no warning, the long shim just swallows the overflow. Avro does the opposite, it throws. The safe boundary is 9007199254740990, which is 2 to the 53 minus 2, and the moment you write 9007199254740991, one more than the library will trust, the encode raises invalid long and the decode side throws potential precision loss on the same value. I checked the boundary from both sides and it lands exactly on the safe-integer line, not a round number. So a converter that accepts arbitrary 64-bit ids has to wire up the custom long support each library exposes, and until it does, the Avro side fails loud while the protobuf side fails quiet, which is the better kind of bug even though it feels worse.

The on-disk file is worth knowing before you open one in a hex editor, because the first thing you see is the schema in plain text. The file opens with the four bytes Obj plus a null, then a metadata map that carries the full schema as a JSON string and the codec name, then sixteen bytes of sync marker. I expected those sixteen to be a fingerprint of the schema, which is what some older spec text implies, and they are not: I wrote two files with the identical schema and the markers came out different, the encoder generates them randomly and reuses them between blocks so a reader can resynchronize after a torn write. After the header comes the first data block, a record count, a size, the records, and the sync marker again. I want to be precise here because a lot of write-ups get this wrong: in the default null codec and in the required deflate codec there is no per-block checksum, the count, the size, and the bytes are the whole block. The optional snappy codec is the one that appends a four-byte CRC32, and the spec is explicit that deflate carries no checksum. So a corrupted block is caught because the sync marker falls out of line at the next boundary, not because a hash disagrees, unless you picked the codec that carries one.

The size is the same shape as the protobuf comparison, measured on the same record, a name, an email, an age, a balance, and a list of eight ids. Avro came in at 47 bytes, the JSON equivalent at 99 characters, a little under two to one, exactly the same ratio as protobuf, and for the same reason, a string is a string in either format and only the length prefix differs, and the savings concentrate in the numbers and the list. Where Avro genuinely goes further than protobuf is schema evolution, and the behavior is stricter than people expect. I ran the reader over writer data in both directions: a reader that adds a field with a default fills it, a reader that drops a field skips it cleanly, reordering fields in the reader still matches by name because the resolver works on field names and not on position, but a reader that wants a field the writer never had and has no default for throws no matching field, and a field whose type changed from string to int throws even though the reader declared a default, because the default only covers absence and not a mismatch. The widening moves are allowed, an int reads fine as a long and a float reads fine as a double, and those are the only two cross-type moves the resolver lets through without a default.

One last place I assumed they matched and they do not. I expected Avro's JSON to mirror protobuf's, with decimal strings for 64-bit values, and it does not. The Avro spec's JSON encoding does not distinguish int from long at all, so a 64-bit id writes out as a plain number, 9007199254740990 with no quotes, while protobuf turns the same 64-bit integer into a decimal string because of the 2 to the 53 rule. The layer where you would expect the most overlap is where the two formats make a small, quiet, different choice. The endianness, the missing tags, the 64-bit handling, and the file layout are the facts that bite when you try to read one family with the other, and the tool on the protobuf side of this, at proto.datajutsu.com, is where I measure every byte before I write it down, and the size and speed numbers for that format are in the protobuf versus JSON post.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe