logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramlinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Design a Social-Media News Feed
API GatewayAPIBatchingCacheCAPConsistent HashingCounting At ScaleCQRSCRDTDatabase ScalingDatabase Selection StrategyDeployment StrategyDNSEvent SourcingHandling FailureVertical vs Horizontal ScalingHTTPId GenerationIndexesKafkaKubernetesLarge FilesLoad BalancingPaginationProxyReal Time PushRedisScaling ReadsStateless vs Stateful SystemsScaling Writes
System Design/topics

Pagination

3 min read
On this pageTypes of pagination
Types of paginationOffset-based paginationCursor-based pagination (keyset pagination)Page-number paginationToken-based paginationPagination in distributed systems

Pagination is the technique of splitting a large result set into smaller, manageable chunks instead of returning everything in one API response.

  • Reduces response size and network bandwidth.
  • Controls database load and memory consumption.
  • Improves API response latency.
  • Prevents expensive large-result queries.
  • Supports browsing large datasets efficiently.

# Types of pagination

# Offset-based pagination

Client specifies how many records to skip and how many to return.

GET /posts?limit=20&offset=40
 
# SQL
 
SELECT *
FROM posts
ORDER BY created_at DESC
LIMIT 20 OFFSET 40;

# Advantages

  • Simple to implement.
  • Easy to jump to a particular page.
  • Useful for small, relatively static datasets.

# Disadvantages

  • Large offsets can be expensive because the database may scan and discard many rows.
  • Inserts/deletes between requests can cause duplicates or skipped records.
  • Performance often degrades on deep pages.

# Cursor-based pagination (keyset pagination)

The client sends a cursor representing the last item received.

SELECT *
FROM posts
WHERE (created_at, post_id) < (:last_time, :last_id)
ORDER BY created_at DESC, post_id DESC
LIMIT 20;

A good cursor should be based on a deterministic ordering. Multiple posts can have identical timestamps. Without a unique tie-breaker, records may be skipped or repeated.

# Advantages

  • Efficient for deep pagination with a suitable index.
  • Avoids scanning and discarding large offsets.
  • More stable when new records are inserted before the current cursor.
  • Ideal for feeds, timelines, and large collections.

# Disadvantages

  • Cannot easily jump directly to page 100.
  • Requires a deterministic ordering.
  • Cursor generation and validation add complexity.
  • Deletions and updates can still affect traversal semantics.

Cursor query

Cursor pagination is only efficient when the database has an appropriate index. The index allows the database to seek near the cursor instead of scanning and discarding a large number of rows.

# Production example

{
  "items": [
    {"id": "p101", "createdAt": "2026-11-25T10:00:00Z"},
    {"id": "p100", "createdAt": "2026-11-25T09:59:00Z"}
  ],
  "hasMore": true,
  "nextCursor": "opaque-token"
}

Fetch limit + 1 if the client requests 20 records, fetch 21:

  • Return the first 20.
  • If a 21st record exists, set hasMore = true
  • Generate the next cursor from the last returned record.
  • Otherwise, set hasMore = false

# Page-number pagination

The client requests a page number and page size GET /products?page=3&pageSize=20

offset=(page−1)×pageSizeoffset = (page - 1) \times pageSizeoffset=(page−1)×pageSize

This is usually implemented using offset pagination.

# Token-based pagination

About

Token-based pagination is an API design approach, not a fundamentally different database pagination algorithm. The token may encode an offset, keyset cursor, or other continuation state.

The server returns an opaque continuation token rather than exposing the underlying cursor.

{
  "items": ["A", "B", "C"],
  "nextPageToken": "eyJjcmVhdGVkQXQiOi..."
}
 
GET /posts?pageToken=eyJjcmVhdGVkQX...

Useful when:

  • Pagination involves multiple data sources.
  • Queries have complex ordering or filtering.
  • The backend wants to hide cursor implementation details.
  • The token needs to carry query state or a snapshot identifier.

# Pagination in distributed systems

Each shard returns its local top N results. The service merges them into a globally ordered result and returns the requested page.

←PreviousLoad BalancingNext→Proxy

On this page

Types of paginationOffset-based paginationCursor-based pagination (keyset pagination)Page-number paginationToken-based paginationPagination in distributed systems