← Back to Backend, page 1

backend-0007 · Medium mid

How do you evolve a protobuf schema safely: which changes are compatible, and why must field numbers never be reused?

Protobuf data is identified by field numbers, so you can add new fields (old readers skip them, new readers see defaults for old data), rename fields (names are not on the wire), and stop using a field. You must never reuse a number for a different meaning, change a field's type to an incompatible wire type, or renumber. Mark removed numbers and names as reserved so nobody reuses them by mistake.

Full answer

Compatible changes

  • Add a field with a new number. Old readers ignore it (unknown field), new readers get the default value when decoding old data. Proto3 defaults are 0, empty string, false, empty list.
  • Rename a field. Names are only for code generation. JSON mapping would change, though.
  • Remove a field, and add reserved 2; and reserved "email"; so the number and name stay unused.

Breaking changes

  • Reusing a number for another meaning: old data is read as the new field, silently. This is the worst case because nothing fails.
  • Changing a type to a different wire type (string to int32). Decoding fails or returns garbage. Some changes are compatible (int32 and int64 and bool share the varint wire type, with value limits).
  • Renumbering fields, changing repeated to singular in surprising ways, or changing a field's meaning.

Practices

  • Treat the .proto file as an API contract in version control. Run a breaking-change check in CI (buf breaking).
  • Do not rely on required fields (proto2 required was removed from proto3 for this reason). Validate in code.
  • Use enums with an UNSPECIFIED = 0 value, and handle unknown enum values.
  • Use optional when you need to tell "not set" from the default value.
javascript
const protobuf = require('protobufjs');
const type = (proto) => protobuf.parse(`syntax = "proto3"; ${proto}`).root.lookupType('Student');
const show = (t, bytes) => JSON.stringify(t.toObject(t.decode(bytes), { defaults: true }));

const V1 = type('message Student { int32 id = 1; string name = 2; }');
const V2 = type('message Student { int32 id = 1; string name = 2; string email = 3; }');       // added a field
const Renamed = type('message Student { int32 student_id = 1; string full_name = 2; }');        // renamed, same numbers
const Reused = type('message Student { int32 id = 1; int32 age = 2; }');                       // number 2 changed type

const oldBytes = V1.encode({ id: 7, name: 'Asha' }).finish();
const newBytes = V2.encode({ id: 7, name: 'Asha', email: 'asha@example.com' }).finish();

console.log('new data, old reader  :', show(V1, newBytes), '(email is skipped)');
console.log('old data, new reader  :', show(V2, oldBytes), '(email gets the default)');
console.log('renamed fields        :', show(Renamed, oldBytes), '(same bytes, new names)');
try { console.log('number 2 as int32     :', show(Reused, oldBytes)); }
catch (err) { console.log('number 2 as int32     : decoding failed with', err.name); }

// Reusing a number with the SAME wire type does not fail. It silently changes the meaning.
const Before = protobuf.parse('syntax = "proto3"; message Item { int32 quantity = 1; }').root.lookupType('Item');
const After = protobuf.parse('syntax = "proto3"; message Item { int32 price_in_cents = 1; }').root.lookupType('Item');
const stored = Before.encode({ quantity: 3 }).finish();
console.log('reused number, same type:', show(After, stored), '(3 items is now read as 3 cents, with no error)');

Several schemas are parsed in one script and used to decode the same bytes. The failing case is described by the error name only, because the exact message depends on the library. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): new data, old reader : {"id":7,"name":"Asha"} (email is skipped) old data, new reader : {"id":7,"name":"Asha","email":""} (email gets the default) renamed fields : {"studentId":7,"fullName":"Asha"} (same bytes, new names) number 2 as int32 : decoding failed with RangeError reused number, same type: {"priceInCents":3} (3 items is now read as 3 cents, with no error)

Key points

  • New fields with new numbers are safe in both directions.
  • Names are not on the wire, so renames are safe.
  • Never reuse or change a field number's meaning.
  • Use reserved for removed numbers and names.
  • Automate breaking change checks.

Common mistakes

  • Reusing a deleted field's number for a new field.
  • Changing int32 to string and expecting old data to decode.
  • Using required fields.
  • Not reserving removed names and numbers.
  • Skipping breaking checks in CI.

Follow-ups

  • What is the unknown field behaviour of proto3 in different languages?
  • How do you add a field that has no sensible default?
  • How do you migrate stored data when a field changes meaning?

Tips

  • Say 'wire is numbers, names are code'.
  • Call out the silent reuse bug.
  • Mention reserved.