Protobuf best practices

3,954 views
Skip to first unread message

Igor Dmitriev

unread,
Jan 26, 2017, 8:57:04 AM1/26/17
to Protocol Buffers
Hi guys, I've just started to work with Protobuf and I am wondering how to write good .proto files.
Are there any best practices, guides, conventions or smth similar??
I got an idea about services:
1. UserService {
   rpc CreateUser(CreateUserRequest) return (CreateUserResponse)  - good
   rpc CreateUser(string first_name, string last_name, int32 age) return (ClientResponse)  - ugly
}

2. But what do you think about messages segregation ?
I think it's good strategy of putting all the messages in separate files instead of a single file.
In this case it's easier to maintain and reuse messages, but there are a lot of examples on the internet and on official protocol-buffers
website where all messages are placed in one file.

My founds:

Adam Cozzette

unread,
Jan 30, 2017, 12:22:49 PM1/30/17
to Igor Dmitriev, Protocol Buffers
The style guide you linked to is the main resource I know of for recommendations on writing .proto files.

About your point 1), you must use a single message type for the request--using multiple arguments or non-message types is not supported. So fortunately, your "good" example is the required way of doing things.

About message segregation, I don't think there's a hard rule but it's more of a judgment call. I would tend to break up .proto files based on the dependencies between the messages and how they relate to each other. If you have a bunch of small messages that are all logically related and have some dependencies between them, I would put them in the same file. But if the message definitions start to grow large or become less closely related, I would start thinking about breaking them up into separate files.

If your .proto files start to become really huge and you're using C++, you might eventually need to start worrying about compile times getting too long, and at that point you would probably want to start reorganizing your .proto files to minimize the amount of extra work the compiler has to do. But unless you're working on a particularly large codebase, that's not likely to become much of an issue.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+unsubscribe@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages