Use DataTables server-side processing for large tables
Stop downloading thousands of table rows just to paginate them locally. Send only the page you need.
16 Sept 2026, 06:05 UTC

The problem: pagination that still downloads everything
You add a DataTable to an admin page, turn on pagination, and deploy. It works in staging with fifty rows. In production the table holds fifty thousand rows. The browser still downloads every row on the first load, keeps them in memory, and then paginates locally. Sorting a column blocks the main thread, and the initial payload can be megabytes of JSON or HTML that the user never sees.
Useful takeaway: When the full result set is too large or expensive to deliver, switch DataTables to server-side processing. The table then requests one page at a time and sends the pagination, search, and sorting parameters to your backend. The browser only holds the rows it renders.
The request and response contract
Set serverSide: true and provide an Ajax URL. On every page change, search, or sort, DataTables sends request state to the server. Your endpoint applies those inputs and returns only the requested page.
What the browser sends
| Parameter | Meaning |
|---|---|
draw | Request sequence number. The response must echo it unchanged. |
start | Zero-based index of the first row on the requested page. |
length | Number of rows requested for this page. |
search.value | Current global search string. |
order[0].column and order[0].dir | Sorted column index and direction, such as asc or desc. |
What the server must return
| Field | Meaning |
|---|---|
draw | Must match the request's draw value. |
recordsTotal | Total rows in the unfiltered dataset. |
recordsFiltered | Total rows after search filters are applied. |
data | Array of row objects for the current page only. |
Returning the same number for recordsTotal and recordsFiltered breaks pagination when a search is active. The two counts serve different purposes: one drives the total record badge, the other drives filtered page counts.
Worked example
This configuration assumes a table with the ID people and a JSON endpoint at /api/people. Replace the URL with your own route.
$('#people').DataTable({
serverSide: true,
ajax: {
url: '/api/people',
type: 'GET',
data: function (d) {
return {
draw: d.draw,
start: d.start,
length: d.length,
search: d.search.value,
orderColumn: d.order[0]?.column,
orderDir: d.order[0]?.dir
};
}
},
columns: [
{ data: 'id' },
{ data: 'name' },
{ data: 'email' }
]
});
The backend must turn those parameters into a database query. Whitelist sortable column names so a user cannot sort by an arbitrary field. Parameterize the query to avoid injection. Calculate recordsTotal and recordsFiltered separately, then apply start and length to return only the current page. Enforce a server-side maximum page length because a client can request an arbitrarily large length.
Verify before you adopt it
- Enable server-side processing against a small fixture and open your browser's network panel. Confirm the outgoing request includes
draw,start,length,search, and ordering values. - Inspect the JSON response directly.
drawshould match the request,recordsTotalshould represent the unfiltered set,recordsFilteredshould represent the filtered set, anddatashould contain no more rows than requested. - Exercise the first, middle, and last pages, an empty search result, both sort directions, and rapid successive searches. Stale responses must not replace newer table state.
- Run a representative large dataset and measure response payload, browser memory, query duration, and count-query cost. Cache stable counts only after you know the count query is the bottleneck.
You can also probe the endpoint from a terminal where curl is available. No special permissions are required beyond network access to the API:
curl -i 'https://api.example.com/people?draw=1&start=0&length=10&search=&orderColumn=1&orderDir=asc'
Expect an HTTP 200 JSON response with draw equal to 1, a data array of ten or fewer rows, and separate total and filtered counts. If the endpoint returns every row or echoes the wrong draw, the table's pagination controls will desynchronize.
Trade-offs and limitations
- Backend complexity: DataTables no longer sorts or filters locally. Your endpoint owns search semantics, sorting, and counting.
- Count cost:
recordsFilteredoften requires a second aggregate query, which can be expensive on large tables. - Untrusted input: Treat
start,length, search text, and ordering parameters as untrusted. Whitelist sortable fields and use parameterized queries. - Extension compatibility: Client-side extensions may behave differently in server-side mode. Test every extension your application uses.
- Version sensitivity: Option names and behavior vary by DataTables release. Confirm behavior against your installed version.
Rollback is configuration-only: remove serverSide: true and the ajax block, then reload the page so the full dataset loads into the browser again.
Closing recommendation
Before adopting server-side processing broadly, capture the current full-table payload size in your browser's network panel. Then run a representative filtered and sorted query and time the data and count queries. If the full payload is small and the queries are cheap, client-side processing is simpler. If either is large, server-side processing is usually the better trade-off.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.