QA
Beginner LevelREST APIs & Web Services

Compare Offset-based Pagination vs Cursor-based Pagination for large REST collections.

❓ Interview Prompt: Explain `limit` & `offset` vs `cursor` (keyset pagination), SQL performance ($O(N)$ scanning), and handling dynamic data insertions.

APIs returning large datasets must paginate responses to prevent memory overload:

| Feature | Offset Pagination ('?page=2&limit=20') | Cursor Pagination ('?cursor=eyJpZCI6NDJ9&limit=20') |

|---|---|---|

| SQL Query | 'SELECT FROM items LIMIT 20 OFFSET 40;' | 'SELECT FROM items WHERE id > 42 ORDER BY id ASC LIMIT 20;' |

| Performance | Slow $O(N)$ on high offsets (scans & drops rows) | Fast $O(1)$ constant time using indexed column |

| Data Shifts | Skips or duplicates rows if items inserted | Immune to data shifts during pagination |

| Use Case | Page numbers UI (e.g. 1, 2, 3...) | Infinite scrolling feeds (Twitter, Feed API) |

Code Implementation Example

json
{
  "data": [{ "id": 101, "title": "Post A" }],
  "pagination": {
    "limit": 20,
    "nextCursor": "eyJpZCI6MTAxLCJjcmVhdGVkQXQiOjE3NTM2OTkyMDB9",
    "hasMore": true
  }
}
💡Cursor-based pagination API JSON response structure.
🎯 Interview Key Takeaway

Use cursor-based pagination for high performance and consistent scrolling over large dynamic datasets.

#Pagination#Cursor#Performance#Beginner
Back to all REST APIs & Web Services Questions→

More REST APIs & Web Services Interview Questions

Beginner LevelREST APIs & Web Services

What is HTTP Method Idempotency and how do you implement Idempotency Keys for REST APIs?

Beginner LevelREST APIs & Web Services

What are standard HTTP Status Codes (200, 201, 204, 400, 401, 403, 404, 409, 429, 500, 503)?

Beginner LevelREST APIs & Web Services

What are HTTP Verbs (GET, POST, PUT, PATCH, DELETE) and when to use PUT vs PATCH?