title: What pagination schemes can handle rapidly-changing content lists?
body: <p>Pagination is hard when your content rankings can change quickly, and even harder when those rankings differ per-user. (Let's treat infinite scroll as a type of pagination where the links are invisible.)  There are two hard problems: newly-added content at the top, and reranked content. </p>
  
  <p>Let's forget about newly-added content, and accept that you'll have to refresh page 1 to see it. Let's also pretend we're doing pure <code>ORDER BY position</code>; if you're ordering by something else, you might have to use window functions.  Our pages have 4 rows of animals per page. They start out:</p>
  
  <pre><code>+----+----------+-----------+
  | id | position^|  animal   |
  +----+----------+-----------+
  |  1 |        1 | Alpacas   |
  |  2 |        2 | Bats      |
  |  3 |        3 | Cows      |
  |  4 |        4 | Dogs      |
  |  5 |        5 | Elephants |
  |  6 |        6 | Foxes     |
  |  7 |        7 | Giraffes  |
  |  8 |        8 | Horses    |
  +----+----------+-----------+
  </code></pre>
  
  <p>After we fetch page 1, and before we fetch page 2, a lot of items move around. The DB is now:</p>
  
  <pre><code>+----+----------+-----------+
  | id | position^|  animal   |
  +----+----------+-----------+
  |  4 |        1 | Dogs      |
  |  2 |        2 | Bats      |
  |  1 |        3 | Alpacas   |
  |  5 |        4 | Elephants |
  |  6 |        5 | Foxes     |
  |  7 |        6 | Giraffes  |
  |  3 |        7 | Cows      |
  |  8 |        8 | Horses    |
  +----+----------+-----------+
  </code></pre>
  
  <p>There are three common approaches:</p>
  
  <p><strong>Offset/limit approach</strong></p>
  
  <p>This is the typical naive approach; in Rails, it's how <a href="https://github.com/mislav/will_paginate" rel="noreferrer" title="will_paginate">will_paginate</a> and <a href="https://github.com/amatsuda/kaminari" rel="noreferrer" title="kaminari">Kaminari</a> work.  If I want to fetch page 2, I'll do</p>
  
  <pre><code>SELECT * FROM animals
  ORDER BY animals.position
  OFFSET ((:page_num - 1) * :page_size) 
  LIMIT :page_size;
  </code></pre>
  
  <p>which gets rows 5-8. I'll never see Elephants, and I'll see Cows twice.</p>
  
  <p><strong>Last seen ID approach</strong></p>
  
  <p>Reddit takes a different approach. Instead of calculating the first row based on page size, the client tracks the ID of the last item you've seen, like a bookmark.  When you hit "next", they start looking from that bookmark onward:</p>
  
  <pre><code>SELECT * FROM animals
  WHERE position &gt; (
    SELECT position FROM animals 
    WHERE id = :last_seen_id
  ) 
  ORDER BY position
  LIMIT :page_size;
  </code></pre>
  
  <p>In some cases, this works better than page/offset. But in our case, Dogs, the last-seen post, zoomed right to #1.  So the client sends up <code>?last_seen_id=4</code>, and my page 2 is Bats, Alpacas, Elephants and Foxes. I haven't missed any animals, but I saw Bats and Alpacas twice.</p>
  
  <p><strong>Server side state</strong></p>
  
  <p>HackerNews (and our site, right now) solves this with server-side continuations; they store the <em>entire</em> result set for you (or at least several pages in advance?), and the "More" link references that continuation. When I fetch page 2, I ask for "page 2 of my original query". It uses the same offset/limit calculation, but since it's against the original query, I simply don't care that things have now moved around.  I see Elephants, Foxes, Giraffes, and Horses.  No dups, no missed items.</p>
  
  <p>The downside is that we have to store a lot of state on the server. On HN, that's stored in RAM, and in reality those continuations often expire before you can press the "More" button, forcing you to go all the way back to page 1 to find a valid link. In most applications, you can store that in memcached, or even in the database itself (using your own table, or in Oracle or PostgreSQL, using holdable cursors). Depending on your application, there might be a performance hit; in PostgreSQL, at least, you have to find a way to hit the right database connection again, which requires a lot of sticky-state or some clever back-end routing.</p>
  
  <p>Are these the only three possible approaches? If not, are there computer-science concepts that would give me Google juice to read about this? Are there ways to approximate the continuation approach without storing the entire result set? Long term, there's complex event-streaming/point-in-time systems, where "the result set as of the moment I fetched page 1" is forever derivable. Short of that... ?</p>