Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful API sorting dilemma

I have REST API implemented following the principle that rest just gives back the basic documents and refs in those documents howto get other things etc etc

For example a /car/5 will give me model:blabla , user_id: 1 and then if you need the owner you'll get /user/1 to get user data..

This way JOINS and stuff are avoided in DB..all things are linking between themselves and data interconnected on rest client part - keeping things simple easy to cache/drop cache, scale etc.

But what happens when you need sorting?

Imagine we have some view on frontend to display following data: Car model, User name, etc... and you wanna sort by user name for example.

You can't really tell the /car/5 to sort by username cause it only knows of user ids...

One option I see is sorting from user /user/list?sortby=username and then interconnecting which of those returned ids actually refer to car. but this means we need to get ALL users..and only use fraction of those which seems killer performance bottleneck.

Thanks for any pointers

like image 744
Julian Davchev Avatar asked Aug 18 '10 08:08

Julian Davchev


People also ask

Why we should support filtering sorting and pagination in RESTful APIs?

Filtering, Sorting, and Pagination to Retrieve the Data Requested. Filtering, sorting, and pagination are critical features of successful REST APIs. They allow you to retrieve only the data you want in a simple, efficient manner. Filtering allows you to retrieve only those resources needed by your application.

Should API sort data?

Like filtering, sorting is an important feature for any API endpoint that returns a lot of data. If you're returning a list of users, your API users may want to sort by last modified date or by email.


2 Answers

I think you're trying to avoid joining for all the wrong reasons, as by taking this approach, you're going to have to service a lot more requests.

If you instead bring back all the information that you'd show (i.e. do the joins database side), then the clients are going to have to make less queries, and you could do your sort without having to (lazy) load all the child objects.

The other option would be to bring back the children objects as child XML elements (in a similar manner to how OpenStreetMap's RESTful interface works). You still have a single hit from the client, and you should be able to optimise the queries to minimise load on the DB.

like image 122
Rowland Shaw Avatar answered Sep 24 '22 04:09

Rowland Shaw


If you are avoiding all joins in the server and leaving it up to the client then in this case you must leave sorting to the client too. This is because a join has to take place in order to sort the cars by Username.

You can create another resource called CarsAndOwners or similar which returns the joined list, at which point it becomes reasonable to sort on the server.

like image 38
Jack Ryan Avatar answered Sep 20 '22 04:09

Jack Ryan