I have written my own Restful API and am wondering about the best way to deal with large amounts of records returned from the API.
For example, if I use GET
method to myapi.co.uk/messages/
this will bring back the XML for all message records, which in some cases could be 1000's. This makes using the API very sluggish.
Can anyone suggest the best way of dealing with this? Is it standard to return results in batches and to specify batch size in the request?
You can change your API to include additional parameters to limit the scope of data returned by your application.
For instance, you could add
limit
andoffset
parameters to fetch just a little part. This is how pagination can be done in accordance with REST. A request like this would result in fetching 10 resources from the messages collection, from 21st to 30th. This way you can ask for a specific portion of a huge data set:Another way to decrease the payload would be to only ask for certain parts of your resources' representation. Here's how facebook does it:
Remember that while using either of these methods, you have to provide a way for the client to discover each of the resources. You can't assume they'll just look at the parameters and start changing them in search of data. That would be a violation of the REST paradigm. Provide them with the necessary hyperlinks to avoid it.
I strongly recommend viewing this presentation on RESTful API design by apigee (the screencast is called "Teach a Dog to REST"). Good practices and neat ideas to approach everyday problems are discussed there.
EDIT: The video has been updated a number of times since I posted this answer, you can check out the 3rd edition from January 2013
if you are using .net core you have to try this magic package
Microsoft.AspNetCore.ResponseCompression
then use this line in configureservices in startup file
then in configure function
There are different ways in general by which one can improve the API performance including for large API sizes. Each of these topics can be explored in depth.
Source: https://apievangelist.com/2018/04/20/delivering-large-api-responses-as-efficiently-as-possible/