3 min read

Test pagination with a page size of one

One day in August, a customer lost 2,590 user accounts.

Nothing was hacked. Nothing crashed. A sync job asked our identity provider for the customer’s users. What came back was a short list. The job compared it with ours, removing everyone who wasn’t on it. It did exactly what it was written to do. The list was wrong.

How a list gets short

The provider’s list endpoint pages through results with a cursor. You get a page and a pointer to the next one. When the pointer comes back empty, you’re done.

The docs said results came in one order by default. Without an explicit order, the endpoint behaved differently. On a full page, the “next” pointer could come back empty. Our loop read that as the end. It stopped after one page with no error, holding a normal-looking list of users that was much too short.

A second thing turned a bug into an outage. The step that compares lists and removes the difference was meant as a one-time migration command. Instead, people ran it to fix drift. On a short list, “remove what’s missing” means removing almost everyone.

Why the tests passed

We had tests for this code. They ran against about a hundred users.

A hundred users fit on one page. The loop never asked for a second page. That cursor behavior never came up. The tests passed for the same reason production failed: they only ever saw the first page.

The tests passed for the same reason production failed: they only ever saw the first page.

The test that would have caught it is embarrassingly simple: set the page size to one. With three test users, the loop has to follow the cursor twice. If the cursor misbehaves, the test sees one user instead of three.

What changed

The direct fix was to always send an explicit order on every paged request. We also wrapped pagination so one place handles the cursor.

A grey warning triangle with an arrow to a black build box with a cross in it

Then we made sure nobody had to remember that:

A person had to find this once. Nobody should have to find it twice. That’s why it became a check instead of a note in a postmortem.

The general lesson

Pagination is the most common example. The pattern is everywhere. Tests with small data never reach the code that only runs on big data.

A short list with an arrow into a bin that takes almost everyone