Senior
How did you approach versioning gRPC contracts?
sobes.tech AI
Answer from AI
Versioning of gRPC contracts in Golang is usually based on the following principles:
- Backward Compatibility: we try not to break existing services and clients.
- Adding new methods and messages: new RPCs and fields are added with new numbers, old ones are not removed.
- Using optional fields: new fields in messages are made optional so that old clients can ignore them.
- Versioning of packages or services: sometimes separate packages with versions are created, for example
v1,v2, to clearly distinguish different API versions.
Example:
syntax = "proto3";
package example.v1;
service MyService {
rpc GetData(Request) returns (Response);
}
message Request {
int32 id = 1;
}
message Response {
string data = 1;
string extra_info = 2; // new optional field
}
If an incompatible change is needed, a new version of the service is created, for example example.v2.MyService.
Thus, clients can gradually migrate to the new version without interruption.