gRPC repeated field vs stream
Asked Answered
T

2

6

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

Torras answered 18/9, 2019 at 17:7 Comment(0)
S
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.

Syllabus answered 18/9, 2019 at 17:13 Comment(2)
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 messagesTorras
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.Syllabus
B
0

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.
Bolshevist answered 25/6 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.