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
0:03
in order to improve performance and preserve bandwidth.
0:07
When a client requests data, the first items matching the query are server
0:11
and links to the next and previous pages are included with the response.
0:16
Default page size as well as
0:18
maximum number of allowed items per page is configurable,
0:22
but first let's see how we can disable pagination altogether.
0:27
Well, this is very simple we have as you now can imagine
0:31
a boolean setting which is true by default, pagination
0:36
we can simply turn it off like this,
0:40
pagination is a delicate feature because it can have a huge impact
0:44
on your server performance
0:46
so if you don't need it for some reason, you can simply turn it off
0:51
and clients won't be able to slow down your service at all,
0:55
and because pagination is so important you can also set it at a local level
1:01
for example here, I might want to have pagination
1:08
still enabled at the people endpoint
1:11
while I keep it disabled at the global level
1:15
so all the other endpoints will have pagination disabled
1:18
except for the people endpoint
1:21
and whatever else endpoint I decide it should have it.
1:25
Even if it is less likely to happen, you might want to change
1:29
the word used in your queries to ask for a specific page,
1:33
like we did with where and sort,
1:35
you can simply set query page to whatever value you want.
1:42
Likewise, we might want to change the word used
1:49
to define a maximum number of items the clients would receive per page.
1:55
The default is max results, but you can change it whatever you want.
2:06
Now speaking of max results, it has a default value,
2:08
if you go back to Postman and try to send a request for a single page here
2:14
we get the items and we see that the max result is 25.
2:19
We can change this value to whatever we please
2:26
here we're setting the max result default value to 50 documents per page.
2:31
You can also set a limit on these max result number
2:36
pagination limited the setting you need for that
2:43
and the default value is 50
2:46
so you probably want to stay between 50 and 100 maybe
2:50
but that might be already a bit too much, honestly.
2:54
Pagination default again is 25
2:57
so you want to stay around these values for your service
3:00
unless you have a very good hardware
3:03
and you are not very worried about pagination performance;
3:06
or you are optimizing for pagination speed
3:10
because there is an optional optimized pagination for speed option
3:16
it is disabled by default but you can switch it on
3:19
it can greatly improve performance on large collections
3:23
but it comes with a few consequences.
3:26
Firstly, no document count is returned with a response.
3:29
Secondly, pagination links are less accurate as no last page link is available.
3:35
while next page link is always included, even on the last page.
3:39
Again, on big collections switching this feature on can greatly improve performance.
3:45
It defaults to false for slower performance
3:48
but document count is included, and the accurate link pages are provided.