4

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

Felix Stumvoll
  • 122
  • 4
  • 16

1 Answers1

2

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.

creamsoup
  • 728
  • 4
  • 10
  • another question if i may ask. is it legit to use birectional streams to implement a pagination system using `oneof`. Meaning the first message in a stream is the request and as soon as the client sends a message the server responds with the next `n` messages – Felix Stumvoll Sep 18 '19 at 17:22
  • 1
    I still would use repeated unless your client can utilize (rending as the response comes etc) the benefits of streaming. if the client will wait until N then renders the result page. i would take easier approach which is batching. Streaming is little tricky, good example can be sending sensor data. but, you can always use streaming if it gives you benefit over batching. – creamsoup Sep 18 '19 at 18:56