Design efficient API pagination using offset, cursor, or keyset approaches with performance optimization.
You are a database and API performance expert specializing in large-scale data retrieval patterns.
## CONTEXT
I need to implement pagination for my API endpoints.
**Requirements:**
- Data Volume: {{DATA_VOLUME}}
- Access Patterns: {{ACCESS_PATTERNS}}
- Sort Requirements: {{SORT_OPTIONS}}
- Client Types: {{CLIENTS}}
## TASK
Design an optimal pagination strategy:
### 1. PAGINATION METHOD ANALYSIS
**Offset-Based:**
```
GET /api/items?offset=20&limit=10
```
- Pros: Simple, random access
- Cons: Performance degrades, inconsistent with live data
**Cursor-Based:**
```
GET /api/items?cursor=abc123&limit=10
```
- Pros: Consistent, performant
- Cons: No random access
**Keyset (Seek):**
```
GET /api/items?after_id=100&limit=10
```
- Pros: Best performance
- Cons: Requires ordered, unique key
**Recommendation:** [Based on your requirements]
### 2. RESPONSE FORMAT
```json
{
"data": [...],
"pagination": {
"totalCount": 1000,
"pageSize": 10,
"currentPage": 3,
"totalPages": 100,
"hasNextPage": true,
"hasPreviousPage": true,
"cursors": {
"before": "cursor_abc",
"after": "cursor_xyz"
}
},
"links": {
"self": "/api/items?page=3",
"first": "/api/items?page=1",
"prev": "/api/items?page=2",
"next": "/api/items?page=4",
"last": "/api/items?page=100"
}
}
```
### 3. PERFORMANCE OPTIMIZATION
- Index strategies
- Count optimization
- Caching approaches
### 4. IMPLEMENTATION CODE
SQL and API code examples.Or press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{DATA_VOLUME][{ACCESS_PATTERNS][{SORT_OPTIONS][{CLIENTS]{
"data": [...],
"pagination": {
"totalCount": 1000,
"pageSize": 10,
"currentPage": 3,
"totalPages": 100,
"hasNextPage": true,
"hasPreviousPage": true,
"cursors": {
"before": "cursor_abc",
"after": "cursor_xyz"
}{
"self": "/api/items?page=3",
"first": "/api/items?page=1",
"prev": "/api/items?page=2",
"next": "/api/items?page=4",
"last": "/api/items?page=100"
}