title: How to implement robust pagination with a RESTful API when the resultset can change?
body: <p>I'm implementing a RESTful API which exposes Orders as a resource and supports pagination through the resultset:</p>
  
  <pre><code>GET /orders?start=1&amp;end=30
  </code></pre>
  
  <p>where the orders to paginate are sorted by <code>ordered_at</code> timestamp, descending. This is basically approach #1 from the SO question <a href="https://stackoverflow.com/questions/776448/pagination-in-a-rest-web-application">Pagination in a REST web application</a>.</p>
  
  <p>If the user requests the second page of orders (<code>GET /orders?start=31&amp;end=60</code>), the server simply re-queries the orders table, sorts by <code>ordered_at DESC</code> again and returns the records in positions 31 to 60.</p>
  
  <p>The problem I have is: what happens if the resultset changes (e.g. a new order is added) while the user is viewing the records? In the case of a new order being added, the user would see the old order #30 in first position on the second page of results (because the same order is now #31). Worse, in the case of a deletion, the user sees the old order #32 in first position on the second page (#31) and wouldn't see the old order #31 (now #30) at all.</p>
  
  <p>I can't see a solution to this without somehow making the RESTful server stateful (urg) or building some pagination intelligence into each client... What are some established techniques for dealing with this?</p>
  
  <p>For completeness: my back-end is implemented in Scala/Spray/Squeryl/Postgres; I'm building two front-end clients, one in backbone.js and the other in Python Django.</p>