Improve API perfomance.
07 November 2023
Click on image to see full size. Read the comments for description.
One comment “Improve API perfomance.”
Add comment
You need to register , for comment of article.
Click on image to see full size. Read the comments for description.
You need to register , for comment of article.
November 7, 2023 в 1:54 pm
How to Improve API Performance with Pagination
The diagram below shows 5 optimizations to improve API performance. Today, I’ll focus on the first one – pagination.
Pagination is a technique to divide large datasets into smaller, more manageable chunks that can be retrieved separately. This improves performance and user experience by preventing huge amounts of data from being loaded at once.
There are two common pagination techniques:
Page Number Pagination
- Simple to implement
- Allow jumping to specific pages easily
1. Decide the number of items per page (fixed or configurable).
2. To get page N, calculate the start index as (N – 1) * page_size. Use LIMIT and OFFSET to fetch that subset of data. For example: `SELECT * FROM table LIMIT 10 OFFSET 20;`
Cursor-Based Pagination
- Efficient for large, constantly updating datasets
- Resilient to shifting rows, e.g. deleted records
- Works well with infinite scroll designs
1. Choose an indexed column as the cursor.
2. Hash the cursor value for security.
3. The client provides the hashed cursor value of the last item they saw.
4. The server uses this cursor to filter and fetch the next page of results after that value.
5. The server returns the hashed cursor value of the last item in the current page.
6. The client uses the new cursor to fetch the next page.
Best Practices For Large Datasets
- Avoid OFFSET for large datasets. OFFSET scans the entire dataset which gets slower as the offset increases.
- Use cursor on indexed columns for efficient lookup of pages.