Felix Stumvoll
Felix Stumvoll

Reputation: 152

gRPC repeated field vs stream

Hi im currently looking into grpc and im curious about the use usage of a repeated field vs a stream. For example let's say i want to implement a reservation service for movie seats. The issue im facing is, that i want to inform the service for which movie i want to reserve the seats for. I can think of 2 solutions, first: I send the id of the movie with every seat i want to reserve, or with oneof at the beginning of the stream like this:

rpc ReserveSeatsForShowing(stream SeatReservationRequest) returns(Reservation);

message SeatReservationRequest{
    oneof reservationOneOf{
        int32 showingId = 1;
        SeatReservation seatReservation = 2;
    }
}

Or using a repeated field like this

rpc ReserveSeatsForShowing(SeatReservationRequest) returns(Reservation);

message SeatReservationRequest{
    int32 showingId = 1;
    repeated SeatReservation seatReservation = 2;
}

Since i haven't really worked with grpc before im not quite sure which option to choose or if other options are available.

Looking forward for your recommendations

Upvotes: 6

Views: 6441

Answers (2)

Archimedes Trajano
Archimedes Trajano

Reputation: 41450

Reasons to use streaming

  1. GRPC has a default maximum message limit of 4MB. So if you expect your result to be more than 4MB of data then you have a technical reason to use stream otherwise just use repeated because it's simpler to work with.
  2. If your service can take a while between each value and the client can work with the partial data, and the server can send partial data then stream because that way clients can start processing earlier. Generally you can probably do this with BatchGet style operations.
  3. Related to #1, you don't want to implement paging, this will at least prevent you from having to deal with really long lists.

Upvotes: 0

creamsoup
creamsoup

Reputation: 878

For the seat reservation, I think it would make sense to use repeated field. Just like real world scenario, the request is like "I want seat A, B, C for movie X", which is more like repeated manner than streaming. thus, the payload is very small. Also, this way should use less server resource since it is a batch process.

Upvotes: 2

Related Questions