Back to all posts

Two Main Methods to Handle Data-Heavy Interfaces

This article uses React.js for the explanation and examples, but the concepts are the same for all frontend libraries. I recommend focusing on the underlying architecture, instead of coding practices.

For frontend applications it's common to control and present heavy data. For example, a table with 50 rows is easy to render and show. If data grows to 50,000 rows, the solution we used before might fail. There are many reasons behind it:

These 3 issues can be solved with one of these solutions, depending on business requirements and needs.

Virtualization

Virtualization is also called windowing. It's a concept to keep one long, continuously scrollable list but only render the rows inside the viewport, plus a small buffer above and below. Users can scroll through 50,000 rows but the DOM still holds only 30 of them.

How it works in practice

  1. Container of elements with a scroll gets the full height the list would have. For example, if one row is 48px, for 50,000 rows it means 50,000 * 48 = 2.4 million px.
  2. On every scroll, the code reads scrollTop and works out which rows are visible. firstIndex = Math.floor(scrollTop / rowHeight). For example, if scrollTop is 320px, firstIndex will be 6.
  3. The code renders just those rows, absolutely positioned to where they belong.
  4. Rows that leave the window are unmounted or recycled for the next ones coming in.

The user always sees a normal scrollbar and a list that seems complete. Under the hood, we play a game with the user: showing only the rows visible in the viewport.

Virtualization: only the rows inside the viewport, plus a small overscan, exist in the DOM while the user scrolls through 50,000 rows

How to do it

We rarely need to write this ourselves. There are already well-known libraries, such as @tanstack/react-virtual or react-window. Virtualization can be built from scratch, but generally apps prefer these libraries because they are battle tested. These solutions are open source, and thousands of apps have already found the bugs: handling rows of different heights, scroll anchoring, row flickering, and stale rows when items are reordered.

For example, with TanStack Virtual:

import { useRef } from "react"; import { useVirtualizer } from "@tanstack/react-virtual"; function ProductList({ products }: { products: Product[] }) { const parentRef = useRef<HTMLDivElement>(null); const virtualizer = useVirtualizer({ count: products.length, getScrollElement: () => parentRef.current, estimateSize: () => 48, overscan: 5, }); return ( <div ref={parentRef} style={{ height: 600, overflow: "auto" }}> <div style={{ height: virtualizer.getTotalSize(), position: "relative" }}> {virtualizer.getVirtualItems().map((row) => ( <div key={row.key} style={{ position: "absolute", top: 0, width: "100%", height: row.size, transform: `translateY(${row.start}px)`, }} > {products[row.index].name} </div> ))} </div> </div> ); }

It's the same 4 steps from above. getTotalSize() gives the full height of the list (step 1), getVirtualItems() returns only the visible rows (step 2), and each row is positioned with translateY (step 3). overscan: 5 is the small buffer: 5 extra rows above and below, so the user doesn't see empty space during fast scrolling.

Downsides

Virtualization is not free. Since the rows outside of the viewport don't exist in the DOM:

Pagination

Pagination is another method to handle heavy data. Instead of showing all items on the same page, pagination is focused on splitting data into pages. Size of each page generally can be customized (10-25-50-100) and to see the next set of data the user moves to the next page. The most correct way of pagination requires changing the API contract, introducing query parameters and updating backend code. That's why there are 2 methods generally used.

Pagination: each page click sends a new request with a different offset, and only 5 rows are loaded and rendered at a time

Keep in mind that client-side pagination only solves the DOM issue. The full dataset is still downloaded and kept in memory. It's a good solution for a few thousand rows, but not for 50,000. Also search, sorting and filtering should work on the same side as pagination. If the data is paginated on the server, but sorted on the client, the user only sorts the current page, not the whole data.

Server-side pagination can use 2 logics under the hood: offset and cursor.

Offset pagination

Offset pagination is the classic one. The client says how many rows to skip, and how many rows to return:

GET /products?offset=40&limit=20

On the backend it usually becomes a simple SQL query:

SELECT * FROM products ORDER BY id LIMIT 20 OFFSET 40;

Page number is easy to calculate: offset = (page - 1) * limit. That's why offset pagination is great for tables with page numbers. The user can jump directly from page 1 to page 57, and we can show "Page 3 of 2,500".

Offset is not only for page numbers. Imagine the products list has a Load more button at the bottom. Instead of replacing the rows, each click appends the next rows to the end of the list. Here the offset is simply the number of rows we already loaded:

Load more with offset pagination: each click requests the next rows with offset equal to the number of loaded rows, and after a new product is added on the server, one row is loaded twice

But it has 2 problems:

With a Load more button, the second problem is even more visible. Pages are not replaced, they are added to the same list. So if the list is sorted by newest first and a new product is added between 2 clicks, the duplicated product appears twice on the screen, one under the other, and React warns about 2 children with the same key. This is one of the reasons why infinite lists usually use cursor pagination.

Cursor pagination

Cursor pagination doesn't skip rows. Instead, the client says "give me the rows after this one". The server returns the data with a cursor, which points to the last row of the current page:

GET /products?limit=20&after=eyJpZCI6NDB9
{ "items": [...], "nextCursor": "eyJpZCI6NjB9" }

The cursor is usually an encoded value of the last row, for example the id. On the backend it becomes:

SELECT * FROM products WHERE id > 40 ORDER BY id LIMIT 20;

Here the main difference is WHERE id > 40 instead of OFFSET 40. The database uses the index and goes directly to the right row, so page 2,000 is as fast as page 1. And if somebody adds or deletes a product, the next page still starts right after the last row the user saw. No duplicates, no skipped rows.

Here is the same Load more example from the offset section, but with a cursor. To keep it readable, the cursor is the plain id instead of an encoded value. The list is sorted by newest first, so after=49993 means WHERE id < 49993. Even after a new product is added, the next rows start right after the last row the user saw:

Load more with cursor pagination: each click requests the rows after the last loaded id, so a new product added on the server doesn't cause duplicates

The price is flexibility. With a cursor, we can only go to the next (or previous) page. We can't jump to page 57, and usually we don't know the total number of pages. Also the cursor depends on the sort order. If the user sorts by price, the cursor should contain the price and the id together, because many products can have the same price.

There is also page pagination, GET /products?page=3&limit=20. It's the same as offset under the hood: the server calculates offset = (page - 1) * limit. The only difference is that the client works with page numbers instead of rows, so it can't start from a random row.

WhenPageOffsetCursor
The user needs page numbersyesyesno
The user needs to jump to any pageyesyesno
The user changes page size while browsingnoyesyes
The dataset is very largenonoyes
Data changes often (feeds, logs, chat)nonoyes
Infinite scrollnonoyes
Simplicity matters more than speedyesyesno

Which one to choose?

Virtualization and pagination solve different parts of the problem. Virtualization reduces the DOM, pagination reduces the network. That's why the choice mostly depends on how the user works with the data:

WhenVirtualizationPagination
The user scrolls and scans the datayesno
The user needs to find a specific page/rownoyes
The full dataset is small enough to downloadyesno
The dataset is huge or grows all the timenoyes
Backend API can't be changedyesno
The user shares or bookmarks a positionnoyes

Using them together

In real applications, the best answer is often both. Infinite scroll is a good example: the user scrolls one long list, but under the hood the data comes page by page with cursor pagination, and only the visible rows are rendered with virtualization.

const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = useInfiniteQuery({ queryKey: ["products"], queryFn: ({ pageParam }) => fetchProducts({ after: pageParam, limit: 50 }), initialPageParam: undefined as string | undefined, getNextPageParam: (lastPage) => lastPage.nextCursor ?? undefined, }); const products = data?.pages.flatMap((page) => page.items) ?? []; const virtualizer = useVirtualizer({ count: products.length, getScrollElement: () => parentRef.current, estimateSize: () => 48, }); const virtualItems = virtualizer.getVirtualItems(); const lastItem = virtualItems[virtualItems.length - 1]; useEffect(() => { // The user reached the end of loaded rows, so load the next page if ( lastItem && lastItem.index >= products.length - 1 && hasNextPage && !isFetchingNextPage ) { fetchNextPage(); } }, [lastItem, products.length, hasNextPage, isFetchingNextPage, fetchNextPage]);

Pagination solves the network issue, because we load only 50 rows at a time. Virtualization solves the DOM and memory issues, because even after the user loads 5,000 rows, only around 30 of them are in the DOM. All 3 issues from the beginning of the article are solved together.