Urgent.News

What's breaking now, across thousands of outlets.

Tech

Why GitHub Cannot Paginate Through Millions of Repositories - Deep Pagination Explained

You've paginated through a big result set before. Page 2 loads fine. Page 200 takes a beat. Nobody stops to ask why — until something like this shows up instead of a slow page: { "message" : "Only the first 1000 search results are available." , "documentation_url" : "https://docs.github.com/v3/search/" , "status" : "422" } That's GitHub's own search API. Ask it for page 9 or page 10 of a search…

GitHub's search API imposes a limit of 1000 results per page, returning an error message when attempting to request beyond this limit. To understand why this happens, one must delve into the mechanics of pagination and database indexing. Pagination typically involves querying a database for a specific set of records, often using a "LIMIT" and "OFFSET" clause. However, this approach can be inefficient, especially when dealing with large result sets.

The issue arises from how database indexes are built. A sorted column is backed by a B-tree, which is optimized for sequential access of rows. While this makes ORDER BY operations fast, it doesn't offer a shortcut for retrieving a record at a specific position. Instead, the database must sequentially scan through the rows, starting from the beginning, until it reaches the desired position. This results in a time complexity of O(offset), meaning the time taken increases linearly with the offset value.

In the case of GitHub's API, when a request is made for a page beyond the first thousand results, the API returns a 422 status code, indicating that only the first 1000 search results are available. This is not due to rate limiting or a bug, but rather a design choice to prevent excessive resource usage and potential performance degradation. The same issue is observed in other systems that use similar pagination mechanisms, resulting in long wait times for requests with high offsets.

To mitigate this problem, some developers turn to "cursor pagination," which uses the result of the previous request to fetch the next set of records. This approach is generally more efficient, as it avoids the need to sequentially scan through large portions of the dataset. By using cursor pagination, developers can significantly reduce the time required to retrieve paginated results, especially when dealing with large databases.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

N. Korea Group Behind Multiple Open Source Supply-Chain Attacks: Amazon

Amazon’s recent report attributing a series of compromises of open source software libraries to a North Korea-backed threat group encapsulates many of the expanding cyber risks increasingly facing developers, from the growing use of generative AI by bad actors and targeting of code repositories to financially focused attacks by…

More from Monday 3 August →