Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Fine-tuning your REST service
Lecture: Pagination options and performance optimizations
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
As we saw endpoint pagination is enabled by default in order to improve performance and preserve bandwidth.
0:08
When a client requests data, the first items matching the query are server and links to the next and previous pages are included with the response.
0:17
Default page size as well as maximum number of allowed items per page is configurable, but first let's see how we can disable pagination altogether.
0:28
Well, this is very simple we have as you now can imagine a boolean setting which is true by default, pagination we can simply turn it off like this,
0:41
pagination is a delicate feature because it can have a huge impact on your server performance
0:47
so if you don't need it for some reason, you can simply turn it off and clients won't be able to slow down your service at all,
0:56
and because pagination is so important you can also set it at a local level for example here, I might want to have pagination
1:09
still enabled at the people endpoint while I keep it disabled at the global level so all the other endpoints will have pagination disabled
1:19
except for the people endpoint and whatever else endpoint I decide it should have it. Even if it is less likely to happen, you might want to change
1:30
the word used in your queries to ask for a specific page, like we did with where and sort, you can simply set query page to whatever value you want.
1:43
Likewise, we might want to change the word used to define a maximum number of items the clients would receive per page.
1:56
The default is max results, but you can change it whatever you want. Now speaking of max results, it has a default value,
2:09
if you go back to Postman and try to send a request for a single page here we get the items and we see that the max result is 25.
2:20
We can change this value to whatever we please here we're setting the max result default value to 50 documents per page.
2:32
You can also set a limit on these max result number pagination limited the setting you need for that and the default value is 50
2:47
so you probably want to stay between 50 and 100 maybe but that might be already a bit too much, honestly. Pagination default again is 25
2:58
so you want to stay around these values for your service unless you have a very good hardware
3:04
and you are not very worried about pagination performance; or you are optimizing for pagination speed
3:11
because there is an optional optimized pagination for speed option it is disabled by default but you can switch it on
3:20
it can greatly improve performance on large collections but it comes with a few consequences. Firstly, no document count is returned with a response.
3:30
Secondly, pagination links are less accurate as no last page link is available. while next page link is always included, even on the last page.
3:40
Again, on big collections switching this feature on can greatly improve performance. It defaults to false for slower performance
3:49
but document count is included, and the accurate link pages are provided.