Debashis
Debashis

Reputation: 81

Why pagination is used in REST APIs if it involves multiple server calls

I was just reading about API pagination and it seems like a good usecase, when the response needs to be sorted in some order and we need to provide the response on chunks.

But even if the response is provided in chunks, it does so by making multiple API calls.

Considering, if I'm providing 100 paginated records each, I'll still be getting multiple backend API calls and there is going to be traffic and DB resources being used.

So when is the most ideal use-case where pagination should be used?

Upvotes: 0

Views: 2001

Answers (1)

Evert
Evert

Reputation: 99687

If you know for a fact that the client is going to want every item in a list, then in many cases it's better to let the client just get the entire list, but there's a few reasons why paging is useful:

  • If clients need less data, it ensures that the client isn't fetching more than needed.
  • If a smaller request/response fails, it's quicker to retry that request instead of retrying the really big response.
  • Some clients and servers don't work well with streaming data, so getting a ton of items means they all have to fit in memory. Splitting up responses means that it might be easier to work on 1 chunk at a time. It might also result in quicker rendering for a user.
  • Similarly, if the data is meant for a UI, it might be better to load in additional pages/data only when the user needs it. You never know how far the user will scroll down.

That's just some stuff I can come up with.

We typically start paging when things have over a 100 items, or less if the items are large

Upvotes: 1

Related Questions