1

After reading this excellent thread REST Complex/Composite/Nested Resources about nested structures in REST responses, I still have a question. What's the best choice in terms of performance about the response ?

Let's take an example.

I have an Category object, which contains some Questions. Those Questions contains some Answers. All of these structures have meta-informations.

Now, when querying an url like GET http://<base_url>/categories/, should I include a description of the Categories only, include Question description ? Which one, full description or simplified one ?

In other terms, what's the best solution between those :

{
    "results":[
        {
          'id':1,
          'name':'category1',
          'description':'foobar',
          'questions':[
                   {
                       'id':1234,
                       'question':'My question',
                       'author' : 4235345,
                       'answers':[
                              {
                                  'id':56786,
                                  'user':456,
                                  'votes':6,
                                  'answer':'It's an answer !'
                               },
                               {
                                  'id':3486,
                                  'user':4564,
                                  'votes':2,
                                  'answer':'It's another answer !'
                               },
                           ]
                   },
                   ...
               ]
           }
           ...
      ]
}

OR SOLUTION 2 :

{
    "results":[
        {
          'id':1,
          'name':'category1',
          'description':'foobar',
          'questions':[
                   {
                       'id':1234,
                       'url':'http://foobar/questions/1234'
                       'answers':[
                              {
                                  'id':56786,
                                  'url':'http://foobar/answers/56786'
                               },
                               {
                                  'id':3486,
                                  'url':'http://foobar/answers/3486'
                               },
                           ]
                   },
                   ...
               ]
           }
           ...
      ]
}

OR SOLUTION 3 :

{
    "results":[
        {
          'id':1,
          'name':'category1',
          'description':'foobar',
          'questions':'http://foobar/categories/1/questions'
        }
        ...
     ]
}

Or maybe another solution ?

Thanks !

Community
  • 1
  • 1
Kobz
  • 469
  • 6
  • 17

1 Answers1

1

That depends on what the application will do with the data. If it is only going to display a list of categories, then it is very inefficient to transfer all the data it ever needs at once, especially if the categories are many, which will decrease response time of user (absolute no no).

These scenarios depend heavily on application and usage of data.

One optimization that we can do is, we can create two requests,

GET http://<base_url>/categories

Which will return minimal data immediately and another request,

GET http://<base_url>/categories?all=true

Which will return all data.

Then the client app can make some clever optimizations like, when user requests for categories, request one is sent and it will immediately render the data. Then after getting the list of categories the user will be idle for some time looking and we can use this opportunity to request all data using request two.

However, as I said this will largely depend on the application.

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
sadiq.ali
  • 526
  • 1
  • 6
  • 16
  • 1
    Thanks for your answer, you guessed right, it's only to display categories in the first time. What I had in mind is to request the full data then store it in application cache. By doing so, I won't have to request questions when accessing to the category because those are already known. It's the duality between "One big request" or "Multiple small requests". But by saying that it should be designed according to the application usage, it's clearer. Thanks ! – Kobz Jul 17 '14 at 13:29