A field number is the unique numeric identifier assigned to a field within a Protocol Buffers message. It appears beside the field’s type and name in a `.proto` definition and identifies that field in the serialized binary message. In a gRPC order request, `user_id = 1` assigns field number 1 to the user ID. section 3.1.1
syntax = “proto3” #1
message CreateOrderRequest {
int64 user_id = 1; #2
repeated Item items = 2; #3
float amount = 3; #4
}
The `CreateOrderRequest` definition assigns distinct field numbers to `user_id`, `items`, and `amount`. section 3.1.1
Treat an assigned number as part of the message’s compatibility contract. Do not change it when updating a field. When removing a field, reserve its number and, where appropriate, its name so a later definition cannot reuse them. Reusing an old number for a different type can cause data compatibility problems when a client and server run different message definitions. Before adding a field, check whether its proposed number or name is reserved. section 3.1.1 section 3.4.1
Field numbers also affect encoding size. Protocol Buffers sends field metadata alongside the value; numbers from 1 through 15 fit in a one-byte metadata block, while numbers from 16 through 2,047 take two bytes. Assign low numbers to frequently used fields when planning a message. This choice concerns the field identifier, not the size of its value: a larger integer value can require additional data bytes. section 3.1.1 section 3.1.2
