Cursor Pagination for a Comments API That Survives New Inserts
Last updated: November 29, 2026
Quick Overview
Design and implement a newest-first comments endpoint that returns pages with an opaque cursor, so clients never see duplicates or skip comments even when new comments arrive while they scroll.
Meta
November 29, 20260
0
0 solved
Design and implement a newest-first comments endpoint that returns pages with an opaque cursor, so clients never see duplicates or skip comments even when new comments arrive while they scroll.
The Graph API and internal GraphQL endpoints at Meta page through comments, reactions and feed items with cursors rather than offsets. In an API design round the interviewer checks whether you understand why offset pagination breaks under concurrent inserts, how to pick a unique sort key, and what goes into a stable cursor contract.
What the Interviewer Expects
- Explains concretely how OFFSET produces duplicates when new rows are inserted at the top
- Uses a unique, stable ordering key such as (created_at, id)
- Makes the cursor opaque so the server can change its encoding later
- Fetches limit + 1 rows to compute has_more without a count query
- Validates and caps the limit and rejects malformed cursors with a clear error
- Gives the matching SQL with a row-value comparison and a supporting index
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you support paging in both directions (before and after)?
- What happens if a comment on the current page is deleted or edited?
- How would you paginate a ranked list (top comments) whose scores change between requests?
- Should the cursor be signed or encrypted, and why?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
GET /posts/{id}/comments?limit=20&after=<cursor>returns{data, paging: {next}}.- Newest first. Inserting new comments must not shift what the next page returns.
nextis null when there are...
High-Level Design
With OFFSET 20, a comment posted between page 1 and page 2 pushes everything down one slot, so the first item of page 2 is a repeat. Keyset pagination fixes this by remembering where the client st...