We are going to design a strategy for migrating Protobuf schemas while preserving backwards compatibility.
The key principles for backwards compatibility in Protobuf are:
Do not change the field numbers of existing fields.
Do not change the data type of an existing field in a way that would break parsing (e.g., changing from int32 to string would break).
New fields can be added with new field numbers, and old code will ignore them.
Fields can be removed, but the field number should not be reused (to avoid old data with that number being misinterpreted by new code).
If a field is of a message type, you can add new fields to that message as long as you follow the same rules.
Steps for a migration strategy:
Versioning the Schema:
We can use a version number in the package name or a separate field to indicate the version of the schema.
Alternatively, we can rely on the fact that Protobuf is designed for compatibility and we don't need to change the package name if we follow the rules.
Adding New Fields:
Assign a new, unique field number that hasn't been used in that message.
The new field should be optional or repeated (since in proto3, all fields are optional by default, but we can also use the 'optional' keyword in proto3 to preserve presence).
Old code will ignore the new field, and new code can read old data because the new field will be set to its default value.
Removing Fields:
We can remove a field, but we must not reuse its field number. We can either reserve the field number or just avoid using it again.
In Protobuf, we can use the 'reserved' keyword to prevent future use of a field number or name.
Changing Field Types:
We can change the field type if the new type is compatible in terms of the binary representation. For example:
int32, int64, uint32, uint64, bool, enum: can be changed to another numeric type or enum, but note that the wire format is different for some (like varint vs fixed64).
However, changing from int32 to fixed64 would break because the wire format is different.
string and bytes: are compatible in the sense that they are both length-delimited, but changing from string to bytes (or vice versa) might break at the application level.
For embedded messages, the wire format is the same (length-delimited) but the interpretation is different. We can change a message type to another message type if the new message has the same field numbers for the fields we care about? Actually, that's not safe because the old message might have different fields. So generally, changing the type of a field to a different message type is not safe.
Safe changes (according to Protobuf documentation) are:
Changing a field from int32 to int64 (or vice versa) might be safe if the value fits? Actually, the wire format for int32 and int64 are both varint, but int64 uses more bytes for large numbers. However, when reading an int32 as int64, it will work. But when reading an int64 as int32, large numbers will be truncated. So it's one-way safe: you can upgrade from int32 to int64, but not the other way without potential data loss.
Similarly, changing from fixed32 to fixed64 is safe because both are fixed 4-byte and 8-byte respectively? Actually, fixed32 is 4 bytes and fixed64 is 8 bytes. Reading a fixed32 as fixed64 will read 4 bytes and then 4 more from the next field? That would be wrong. So fixed types are not compatible with each other if the size changes.
The official documentation says: "You can change a field from single to optional (or from optional to single) without breaking wire compatibility. However, you cannot change a field from optional to repeated (or vice versa) because the encoding is incompatible. Similarly, you cannot change a field from one numeric type to another (e.g. from int32 to fixed64) because the encoding is different."
Therefore, the only safe type changes are:
int32, int64, uint32, uint64, bool, enum: these are all varint encoded (except fixed64 and fixed32 are fixed). Actually, fixed64 and fixed32 are fixed and not compatible with varint.
So the safe changes are within the same wire type group:
Group 1 (varint): int32, int64, uint32, uint64, bool, enum.
Group 5 (start group) and Group 6 (end group) are deprecated and should not be used.
Conclusion: We can change a field's type only if the new type is in the same wire type group and the change is lossless. For example:
int32 -> int64: safe (both varint, and int64 can represent all int32 values)
int64 -> int32: not safe if the value is too big.
fixed64 -> fixed32: not safe because fixed64 is 8 bytes and fixed32 is 4 bytes.
However, note that even within the same wire type group, changing from a signed to unsigned integer might be safe in terms of wire format (both varint) but the interpretation of the bits is different. For example, changing from int32 to uint32: the wire format is the same (varint), but negative numbers in int32 would become large positive numbers in uint32. So it's not semantically safe.
Therefore, the only truly safe type changes are those that do not change the meaning of the value. For example, changing from int32 to int64 (or from int32 to sint64) because the value is the same. But changing from int32 to uint32 changes the interpretation of negative numbers.
Renaming Fields:
We can rename a field (change the field name) without breaking compatibility because the field number is what matters.
However, we must not change the field number.
Moving Fields:
We cannot move a field from one message to another because that would change the field number's context. The same field number in two different messages are independent.
Using the 'reserved' keyword:
To prevent reuse of field numbers and names that have been removed.
Using the 'optional' keyword in proto3:
In proto3, all fields are optional by default, but there is no presence tracking for singular fields. If you want to distinguish between a field being set to its default value and not set, you must use the 'optional' keyword (available in proto3.12 and above). This is important for backwards compatibility when adding new fields? Actually, when adding a new field to an old message, the old code doesn't set it, so it will be default. But if you change an existing field from non-optional (in proto2 sense) to optional (with presence) in proto3, that might break? Actually, in proto3, all fields are optional in the sense that they are not required, but without the 'optional' keyword, you cannot tell if a field was set or not. So if you have an old message that was generated by proto3 without the 'optional' keyword, and you change the field to have 'optional', then the new code will be able to tell if the field was set or not, but the old code will still set the field to the default value and not be able to tell the difference. This is a compatibility issue at the application level, not at the wire level.
Updating the Code:
When deploying a new version of the schema, we must ensure that both old and new code can read the same data.
We should test that old code can read data written by new code (with new fields) and new code can read old data (without new fields).
Migration Strategy Steps:
code
a. Audit the current schema and identify all fields, their numbers, types, and rules (required, optional, repeated).
code
b. Plan the changes:
- For adding: assign new field numbers (check that they are not used in the message or any extended message? Actually, field numbers are only unique within a message).
- For removing: mark the field number as reserved and remove the field from the schema.
c. Update the schema:
Example of adding a new field:
message Person {
int32 id = 1;
string name = 2;
// Adding a new email field, assign a new number, say 3.
string email = 3;
}
Example of removing a field:
message Person {
int32 id = 1;
string name = 2;
// We remove the email field, but we reserve the number 3 to prevent reuse.
reserved 3;
// Alternatively, we can reserve by name too.
// reserved "email";
}
d. If we are using proto3 without the 'optional' keyword and we want to add presence to an existing field, we must change the field to use the 'optional' keyword. This is a breaking change in terms of the generated code's API (because now you have a `has_` method and the field becomes a pointer or a special wrapper). But in terms of wire format, it's the same because the field is still encoded as a normal field. However, note that in proto3, without the 'optional' keyword, the field is always considered set if it's non-default? Actually, in proto3, singular fields are not presence-tracked, so when you read a message, you cannot tell if a field was set to its default value or not set. By adding 'optional', you get presence. So old code that reads a message written by new code (with optional) will set the field to the default value if it's not present, and if it is present, it will read the value. But the old code cannot tell if the field was set or not. This might be acceptable if the application doesn't require that distinction. However, if the application does, then we have a problem.
e. Consider using oneof for fields that might be mutually exclusive? This can be useful for removing a field and adding a new one with the same number? Actually, you cannot change a field to be part of a oneof without changing the field number? You can, but note that oneof fields share the same field number space? Actually, each field in a oneof must have a unique field number. So if you want to replace a field with a oneof, you would have to change the field number? That would break compatibility. So we cannot change a field to be part of a oneof without changing the field number, which is not allowed. Therefore, if we want to replace a field with a oneof, we must remove the old field (reserving the number) and then add a new oneof with a new field number.
f. Consider using custom options for versioning? This is not standard and would require extending the Protobuf compiler.
g. Test the migration thoroughly with old and new code.
11. Example Migration:
code
Let's say we have version 1 of a schema:
syntax = "proto3";
package example;
message Person {
int32 id = 1;
string name = 2;
string email = 3;
}
Now we want to remove the email field and add a new field for phone number.
We do:
syntax = "proto3";
package example;
message Person {
int32 id = 1;
string name = 2;
reserved 3; // we reserve the old email field number
string phone = 4; // new field with a new number
}
In code, we remove the email field and add the phone field.
Old code that has the old schema will ignore the phone field (because it doesn't know about field 4) and will try to read the email field (field 3). But since we removed it, the new code won't write field 3. So when old code reads a message written by new code, it won't find field 3 and will set email to default (empty string). That's acceptable.
When new code reads an old message that has field 3 (email), it will ignore it because we removed the email field. So the phone field will be set to default.
But note: if we want to support both email and phone for a while, we might not remove the email field immediately. Instead, we can add phone as a new field and mark email as deprecated. Then after some time, we can remove email.
Alternatively, if we want to replace email with phone, we can do:
Step 1: Add phone as a new field (with a new number) and deprecate email.
Step 2: After all clients have updated to use phone, then remove email and reserve its field number.
12. Handling Enums:
code
- We can add new enum values without breaking compatibility because old code will treat the new value as an unknown enum number and will usually keep it as an integer (if the enum is defined as an int32). However, if the code switches on the enum, it might not handle the new value. So we must handle unknown enum values in the code (by having a default case or using the `UNKNOWN` value if present).
- We cannot remove or rename enum values because old code might send the old number. We can only mark them as deprecated and then stop using them.
- We cannot change the numeric value of an existing enum item because that would break old code that sends the old number.
- We can add new enum items with new numbers.
13. Handling Oneofs:
code
- We can add new fields to a oneof, but we cannot remove or change the type of an existing field in the oneof without changing the field number? Actually, we can remove a field from a oneof by removing it and reserving the field number. But note that the oneof itself is just a container for fields. Removing a field from a oneof is the same as removing a field from a message, but we must be cautious because the oneof ensures that only one field is set. If we remove a field, then old code that sets that field will have that field set, but new code won't see it because the field is removed. That's acceptable because the oneof will have that field set and the new code will ignore it? Actually, when reading a message with a oneof, if the field number is not in the oneof (because we removed it), then the oneof will not have that field set? But the data is still in the wire. The parser will see the field number and since it's not in the oneof, it will treat it as an unknown field? Actually, the oneof is just a set of fields. If we remove a field from the oneof, then that field number is no longer part of the oneof. So when parsing, if we see that field number, we will not set any field in the oneof? Actually, the generated code for oneof has a case for each field. If the field number is not recognized (because we removed it), then it will be treated as an unknown field and skipped. So the oneof will not have that field set. But the old code that set that field will have written it, and the new code will ignore it. That's acceptable.
- However, if we want to replace a field in a oneof with a new field, we cannot reuse the same field number because the old data might have that field number set. So we must use a new field number for the new field and reserve the old one.
14. Summary of Best Practices:
code
- Plan field numbers carefully and leave gaps for future expansion.
- Use the 'reserved' keyword to block field numbers and names that are no longer in use.
- Add new fields with new numbers.
- Do not change the data type of existing fields unless you are sure the wire format is compatible and the change is lossless.
- Do not change the field number of any field.
- Use the 'optional' keyword in proto3 if you need presence tracking for a field, but be aware that it changes the generated API.
- For enums, only add new values, do not remove or change existing ones.
- Test with old and new versions.
15. Example of a safe type change:
code
// Before
message Example {
int32 count = 1;
}
// After: changing from int32 to int64 is safe because both are varint and int64 can represent all int32 values.
message Example {
int64 count = 1;
}
But note: the generated code for the field will change from `int32` to `int64`. If the application code uses the field, it might break if it assumes it's an int32. So at the application level, we must be cautious.
16. Example of an unsafe type change:
code
// Before
message Example {
fixed32 id = 1;
}
// After: changing to fixed64 is unsafe because the wire format changes from 4 bytes to 8 bytes.
message Example {
fixed64 id = 1;
}
17. Example of a safe change in a repeated field:
code
// Before: repeated int32
message Example {
repeated int32 values = 1;
}
// After: repeated int64? This is not safe because the wire format for repeated int32 is a series of varint, and for repeated int64 is also a series of varint? Actually, yes, both are varint. But note: in proto3, repeated fields of numeric types are packed by default? Actually, in proto3, repeated fields are packed by default for numeric types. So the encoding is a length-delimited field containing a series of varint (for int32) or varint (for int64). So if we change from int32 to int64, the packed format is the same (a series of varint). However, when reading an int32 repeated field as int64, each varint is read as an int64. That is safe because int64 can represent all int32 values. But if we do the reverse (int64 to int32) then large values will be truncated. So it's one-way.
// But wait: in proto2, repeated fields are not packed by default? Actually, in proto2, repeated fields are not packed unless you specify [packed=true]. So if we are using proto2, we must be cautious. In proto3, repeated numeric fields are packed by default.
// However, note that the wire format for a single int32 and a packed repeated int32 are different. So if we change a field from single int32 to repeated int32, that is not safe because the wire format changes from a single varint to a length-delimited packed array.
// Therefore, changing from single to repeated (or vice versa) is not safe.
18. Final Strategy Document:
code
We will follow these steps for a migration:
1. Analyze the current schema and the required changes.
2. For each change, determine if it is a compatible change (according to the rules above).
3. If we need to make an incompatible change, we must do it in a way that we have two versions of the schema? Actually, we can't have two versions of the same message with the same name and package. We would have to create a new message type? Or use a new package? That would break compatibility because the old code expects the old message type.
Instead, we must design the schema so that we can evolve it without breaking compatibility. That means we cannot change field numbers and we cannot change types in an incompatible way.
If we absolutely must make an incompatible change, we might have to:
a. Create a new message type (with a new name) and deprecate the old one.
b. Update the application to use the new message type, and then phase out the old one.
But note: if we change the package name, then it's a completely different type. That might be acceptable if we are versioning the entire API.
However, if we are in the middle of a system and we want to change a field in an incompatible way, we might have to:
- Add a new field with a new number and the new type.
- Deprecate the old field.
- Update the code to use the new field and ignore the old one (or convert it).
- Then, after all clients have moved to the new field, we can remove the old field.
But note: if the old field and new field are both present, how do we handle that? We have to write code that reads both and then decides which one to use. This is a common pattern.
Example: We have an old field `email` (string) and we want to change it to `email_address` (string) because we want to split email into two fields: `personal_email` and `work_email`. We cannot change the `email` field to become a oneof or two fields. So we:
Step 1: Add two new fields: `personal_email` and `work_email` (with new numbers).
Step 2: Mark `email` as deprecated.
Step 3: In the code, when reading, if `email` is set and the new fields are not set, then we copy `email` to `personal_email` (or some default). If the new fields are set, we ignore `email`.
Step 4: When writing, we write the new fields if they are set, and also write `email` for backwards compatibility? Actually, we don't want to write both because that would be redundant. We can write only the new fields, but then old clients won't see the email. So we must write `email` for old clients until they are all upgraded.
This becomes messy.
Therefore, the best practice is to design the schema with evolution in mind and avoid incompatible changes.
19. Conclusion:
code
The strategy for migrating Protobuf schemas while preserving backwards compatibility is:
- Always add new fields with new field numbers.
- Never change field numbers.
- Never change the wire type of a field (unless it's within the same wire type group and the change is lossless, but even then, be cautious about application semantics).
- Never change a field from singular to repeated or vice versa.
- Use the 'reserved' keyword for removed fields.
- For enums, only add new values.
- For oneofs, treat each field as a normal field and follow the same rules.
- Test with old and new code.
If an incompatible change is unavoidable, consider:
- Creating a new message type (with a new name) and migrating the system to use the new type.
- Using a wrapper message that contains both the old and new fields and then migrating the data.
But note: if we create a new message type, then the old code that expects the old message type will break. So we must update all consumers at the same time? That's not always possible.
Hence, the goal is to avoid incompatible changes at all costs.
20. Example of a safe migration over time:
code
Version 1:
message Person {
int32 id = 1;
string name = 2;
}
Version 2: We want to add an email.
message Person {
int32 id = 1;
string name = 2;
string email = 3; // new field
}
Version 3: We want to remove the email and add phone and address.
message Person {
int32 id = 1;
string name = 2;
reserved 3; // email removed
string phone = 4;
string address = 5;
}
Version 4: We want to change the `name` field from string to bytes? That is not safe because the wire type is the same (length-delimited) but the interpretation is different. However, if we are sure that all names are ASCII, it might be safe? But it's not recommended because string and bytes are semantically different. Instead, we can add a new field `name_bytes` and deprecate `name`, then migrate.
message Person {
int32 id = 1;
string name = 2; // deprecated
reserved 2; // we can reserve the old name field number after we remove it? Actually, we are not removing it yet, we are deprecating.
bytes name_bytes = 6; // new field
string phone = 4;
string address = 5;
}
Then, when reading, if `name_bytes` is set, use it, else use `name`. When writing, set `name_bytes` if we have bytes, and also set `name` for old clients? But we don't want to set both. We can set `name` only if we are writing for old clients? That's not possible because the same message is read by both. So we must set both? That would duplicate data. Alternatively, we can set only `name_bytes` and let old clients ignore it? But old clients don't know about `name_bytes`. They will only see `name` which is empty. So we must set `name` for old clients.
This is why it's better to avoid such changes.
21. Final Recommendation:
code
- Design your initial schema with evolution in mind: use field numbers generously (leave gaps) and make fields optional (or in proto3, they are optional by default).
- Avoid changing the meaning of existing fields.
- If you must change, add new fields and deprecate old ones, and have a plan to phase out the old ones.
Given the complexity, it's often better to treat the schema as immutable and create new messages for new versions, and then update the application to handle both old and new messages until the old ones are phased out.
However, in a microservices architecture, you might have multiple services with different versions. In that case, you must ensure that the messages are compatible in both directions.
Therefore, the strategy is:
- For each service, define the schema that it sends and receives.
- When evolving, ensure that the new schema can read old messages and old schema can read new messages (at least the parts that the old schema cares about).
This is the essence of backwards compatibility in Protobuf.</think>
Here's a comprehensive Protobuf schema migration strategy that maintains backwards compatibility:
Core Principles
Never reuse field numbers - Once assigned, field numbers are permanent
Wire format compatibility - Changes must preserve binary encoding compatibility
Graceful degradation - New code handles old data, old code ignores new data
// 🔒 SAFE CHANGES (Wire-compatible):
// - Add new fields with new field numbers
// - Remove fields (but reserve their numbers)
// - Change field names (only the JSON name changes)
// - Add enum values (never remove/renumber)
// - Extend embedded messages (following same rules)
// - Add oneof cases (new field numbers only)
// ⚠️ CONDITIONALLY SAFE:
// - Change singular → optional (proto3+)
// - Change numeric types within same wire type:
// * int32 ↔ int64 ↔ uint32 ↔ uint64 ↔ bool ↔ enum (all varint)
// * fixed32 ↔ sfixed32 ↔ float (32-bit)
// * fixed64 ↔ sfixed64 ↔ double (64-bit)
// * string ↔ bytes (both length-delimited, but semantic diff!)
// 🚫 UNSAFE (Break compatibility):
// - Change field numbers
// - Change singular ↔ repeated
// - Change wire type groups
// - Remove required fields (proto2)
// - Change message type (to unrelated message)
2. Versioning Strategy
Option A: Package Versioning (Recommended)
proto
// v1 schema
package myapp.v1;
message User {
int32 id = 1;
string name = 2;
}
// v2 schema (add email, reserve old field)
package myapp.v2;
message User {
reserved 2; // 'name' field number reserved
reserved "name"; // name also reserved
int32 id = 1;
string name_v2 = 3; // renamed field
string email = 4; // new field
}
Option B: Single Package with Evolution
proto
package myapp;
message User {
// Version 1
int32 id = 1;
string name = 2;
// Version 2 additions
reserved 3, 4; // pre-reserve future fields
string email = 5 [deprecated = true]; // v2 field
string phone = 6; // v3 field
}
3. Migration Patterns
Pattern 1: Additive Changes (Simplest)
proto
// Before
message Order {
int32 id = 1;
string status = 2;
}
// After - add fields only
message Order {
int32 id = 1;
string status = 2;
string customer_id = 3; // NEW
double total_amount = 4; // NEW
repeated string tags = 5; // NEW (repeated)
}
Pattern 2: Deprecate & Replace
proto
message Product {
// v1 - keep for compatibility
string sku = 1 [deprecated = true];
// v2 - new field
string product_code = 2;
// Migration logic in application:
// if sku != "" && product_code == "":
// product_code = sku
}
Pattern 3: Split/Extend Messages
proto
// Before - everything in one message
message Profile {
int32 id = 1;
string name = 2;
string address = 3; // becomes separate entity
}
// After - normalized design
message Profile {
int32 id = 1;
string name = 2;
Address address = 3; // embedded message
}
message Address {
string street = 1;
string city = 2;
// ... address fields
}
4. Handling Breaking Changes
When unavoidable breaking changes are needed:
A. Bridge Pattern with Wrapper
proto
// Old schema still exists
message UserV1 {
int32 id = 1;
string full_name = 2;
}
// New schema
message UserV2 {
int32 id = 1;
FirstName first_name = 2;
LastName last_name = 3;
}
// Migration wrapper (intermediate format)
message User {
oneof version {
UserV1 v1 = 1;
UserV2 v2 = 2;
}
}
B. Dual-Write During Migration
python
# Application layer migration logic
def write_user(data):
# Write to both formats during transition
write_old_format(data) # for old consumers
write_new_format(data) # for new consumers
def read_user():
# Try new format first, fall back to old
data = try_read_new()
if data:
return migrate_new_to_current(data)
return read_old()
Reserve removed field numbers with reserved keyword
Pre-allocate future field numbers in gaps
Add [deprecated = true] to soon-to-be-removed fields
Update .proto files with proper comments
Schema Changes
proto
// Always include:
syntax = "proto3";
package myapp.v2; // if versioning by package
import "myapp/v1/user.proto"; // if extending
message User {
// Version tracking
option protobuf_version = 2;
// Preserve old fields
reserved 2, 5, 9; // from removed fields
reserved "old_field_name";
// Existing fields (never change!)
int32 id = 1;
// New fields
string email = 10; // use high numbers for new fields
// Deprecated but preserved
string legacy_field = 3 [deprecated = true];
}
Code Generation
bash
# Generate for multiple languages
protoc \
--java_out=java_v1/ \
--python_out=python_v1/ \
--go_out=go_v1/ \
--js_out=import_style=commonjs,binary:js_v1/ \
schema/v1/*.proto
# For v2
protoc \
--java_out=java_v2/ \
--python_out=python_v2/ \
schema/v2/*.proto
6. Testing Strategy
Compatibility Matrix Testing
python
# Test all combinations
test_cases = [
# (schema_version, data_version)
("v2", "v1"), # New code reads old data ✅
("v1", "v2"), # Old code reads new data ✅ (ignores new fields)
("v2", "v2"), # Normal case
("v1", "v1"), # Normal case
]
def test_compatibility(schema_ver, data_ver):
# Generate data with data_ver schema
data = generate_data(schema_version=data_ver)
# Parse with schema_ver implementation
result = parse_with_version(data, schema_ver)
# Validate semantics preserved
assert validate_migration(result, schema_ver, data_ver)
Round-trip Testing
python
def test_round_trip():
# v1 → v2 → v1 should preserve original data
original_v1 = create_v1_data()
# Convert v1 → v2 (with defaults for new fields)
v2_data = migrate_v1_to_v2(original_v1)
# Convert v2 → v1 (drop new fields)
roundtrip_v1 = migrate_v2_to_v1(v2_data)
assert original_v1 == roundtrip_v1
7. Deployment Strategy
Phase 1: Prepare (Weeks 1-2)
Add new schema files alongside old ones
Generate dual codebases (old + new)
Implement migration utilities
Add compatibility tests to CI/CD
Phase 2: Deploy New Schema (Weeks 3-4)
Deploy read-compatible changes first
Deploy services that can read both formats
Start dual-writing (write both v1 and v2)
Monitor parsing errors/warnings
Phase 3: Migrate Producers (Weeks 5-6)
Update services that write data to use v2
Keep supporting reading v1 from storage
Use feature flags for gradual rollout
Phase 4: Cleanup (Weeks 7-8)
Deprecate v1 readers
Stop writing v1 format (after all consumers updated)
Remove v1 schema files (after 2-4 weeks)
Remove migration code
8. Monitoring & Alerting
yaml
# Key metrics to monitor:
- protobuf_parse_errors{type="unknown_field"}
- protobuf_parse_errors{type="wire_type_mismatch"}
- protobuf_missing_required_fields # proto2 only
- data_version_distribution
- migration_latency_ms
# Alerts:
- Parse error rate > 0.1%
- Unknown field count increasing (indicates old clients)
9. Common Pitfalls & Solutions
Pitfall
Solution
Reusing field numbers
Pre-allocate: Use gaps (1-100, 200-300, etc.)
Changing required → optional (proto2)
Never change requirements; add new optional field
Using oneof incorrectly
Field numbers in oneof must be unique across message
JSON mapping changes
Be cautious with json_name option; affects APIs
Default value semantics
Document defaults; 0, "", false may differ from unset
Packed repeated fields
Can only change packed ↔ non-packed if all elements same wire type
10. Emergency Rollback Plan
Keep old schema files in version control with tags
Feature flags for new field usage
Canary deployments with 1% traffic
Data backup before major changes
Rollback script:
bash
# Revert to previous schema version
git checkout v1.2.3 -- schemas/
# Redeploy old containers
kubectl rollout undo deployment/user-service
Quick Reference: Do's and Don'ts
✅ DO:
Add fields with new, high field numbers (1000+ if possible)
Use reserved for removed fields/names
Add [deprecated = true] before removal
Test all read/write combinations
Version packages for major changes
Document field number allocations
❌ DON'T:
Change existing field numbers
Change singular ↔ repeated
Change wire types
Remove required fields (proto2)
Reuse deprecated field numbers
Assume default values are semantically equivalent to unset
Example: Complete Migration
proto
// v1/user.proto (initial)
syntax = "proto3";
package myapp.v1;
message User {
int32 id = 1;
string name = 2;
string email = 3;
}
// v2/user.proto (add phone, split name)
syntax = "proto3";
package myapp.v2;
import "myapp/v1/user.proto";
message User {
reserved 2, 3; // name and email numbers reserved
reserved "name", "email";
int32 id = 1;
// New structure
FirstName first_name = 4;
LastName last_name = 5;
Email email = 6; // different type? No, same string
// New field
string phone = 7;
// Keep old fields for compatibility? No, we removed them
// But we must handle reading v1 data in application code
}
// Migration helper in application:
func migrateV1ToV2(v1 *v1.User) *v2.User {
return &v2.User{
Id: v1.Id,
FirstName: extractFirstName(v1.Name), // custom logic
LastName: extractLastName(v1.Name),
Email: v1.Email,
}
}
Key Takeaway: Protobuf's compatibility guarantees are field number-based, not schema-based. Your migration strategy must treat field numbers as permanent, immutable identifiers. When in doubt, add new fields rather than modifying existing ones.