Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination in gRPC

I'm using gRPC to paginate a call and am trying to figure out the options for doing it/approximation for it. Is this a sensible question to ask? What are some resources I can use to do this?

like image 357
sparkFinder Avatar asked May 03 '16 00:05

sparkFinder


3 Answers

This question is quite old but I feel there is something missing to the answer.

While streaming is IMHO preferred, I have cases where "traditional" pagination is quite useful. Lets imagine a user service which enables CRUD access to a user store and has a ListUsers and a SearchUsers rpc. Having the result chunked into pages is far more convenient here.

I personally use Googles approach to this: https://github.com/googleapis/googleapis/blob/master/google/cloud/resourcemanager/v2/folders.proto

like image 135
LuMa Avatar answered Oct 27 '22 16:10

LuMa


Pagination is very similar to chunking binary payloads. My response in gRPC + Image Upload is probably worth a read.

That said, pagination may have different trade-offs, as it is usually much lower throughput and sometimes isn't as hard to use separate requests. Low throughput can prevent flow control from kicking in soon enough for it to be useful. Using separate requests is harder for fully dynamic results, like search results, but may not be much of an issue for more static data, like children of a resource.

Since gRPC flow control may buffer too much, an additional option is to use streaming but introduce application-level flow control. With application-level flow control you would use messages on the stream the request how many responses you want, which isn't too hard to use or implement. There's been talk of supporting precise message-based flow control in gRPC natively (which would produce similar results in this case), but it's unclear if and when that would happen.

like image 39
Eric Anderson Avatar answered Oct 27 '22 15:10

Eric Anderson


Google have themselves written up a good design document about this: https://cloud.google.com/apis/design/design_patterns#list_pagination

  • define a string field page_token in the List method's request message. The client uses this field to request a specific page of the list results.
  • define an int32 field page_size in the List method's request message. Clients use this field to specify the maximum number of results to be returned by the server. The server may further constrain the maximum number of results returned in a single page. If the page_size is 0, the server will decide the number of results to be returned.
  • define a string field next_page_token in the List method's response message. This field represents the pagination token to retrieve the next page of results. If the value is "", it means no further results for the request.

The part about using FieldMask for partial responses is also worth a read since this is a common api design pattern

like image 34
jontro Avatar answered Oct 27 '22 16:10

jontro