Card 07/ 08

ExerciseDifficulty: Intermediate1 min

Practice: Paginating a Feed and an Admin Report

One scrolls forever, the other jumps to page 12.

Choosing offset or cursor pagination is practised here on two lists from one product. For each, choose a strategy, write the request for the second page, and say what could go wrong, before opening the answers.

The brief. A recipe site has two lists.

  1. The home feed shows new recipes, newest first, 20 at a time, as the reader scrolls. Hundreds of recipes are posted every hour.
  2. The moderators' report lists flagged recipes, 50 a page, with page numbers along the bottom. Moderators often jump to page 12. A few recipes are flagged each day.
Check your answers
  1. Cursor. New recipes arrive at the top constantly, so offsets would repeat recipes at page boundaries, and long scrolls would grow slow. The second page is GET /feed?limit=20&cursor=<token for the last recipe seen>, and the query is WHERE id < <last id> ORDER BY id DESC LIMIT 20.
  2. Offset. Moderators need page numbers, and the list changes a few times a day, so drift is rare. The second page is GET /flagged?limit=50&offset=50; page 12 is offset=550. The risk is a recipe flagged or cleared while a moderator pages, which shifts the list by one.