Skip to content

Instantly share code, notes, and snippets.

@renatocron
Created June 25, 2024 00:46
Show Gist options
  • Save renatocron/9a6edb82b21148b50835259043ff72ea to your computer and use it in GitHub Desktop.
Save renatocron/9a6edb82b21148b50835259043ff72ea to your computer and use it in GitHub Desktop.

Pagination Strategies & Resources

  1. Offset-based Pagination: How it works: You request a specific number of records (limit) starting at a particular position (offset). Advantages: Simple to implement. Disadvantages: Performance: Can be slow for large offsets as the database needs to skip over many records. Data Inconsistency: New records added during pagination can lead to skipping or repeating data. Resources: REST API Design Guide: https://cloud.google.com/apis/design/design_patterns#list_pagination (Focuses on REST APIs but provides good general guidelines).
  2. Cursor-based Pagination: How it works: You use a unique, opaque cursor returned from the previous request to fetch the next set of records. The cursor typically represents a point in the dataset based on a specific field (e.g., ID, timestamp). Advantages: Efficient: Usually more performant than offset-based pagination for large datasets. Consistent: Avoids data inconsistencies caused by insertions or deletions during pagination. Disadvantages: Implementation Complexity: More complex to implement than offset-based pagination. Limited Features: Features like jumping to a specific page are not directly supported. Resources: Understanding Pagination: REST, GraphQL, SQL, and NoSQL https://www.prisma.io/blog/graphql-pagination-tutorial-relay-connection-args Building APIs with Django REST Framework - Pagination: https://www.django-rest-framework.org/api-guide/pagination/ (Django-specific but provides excellent examples and explanations).
  3. Keyset-based Pagination: How it works: Similar to cursor-based pagination but uses a set of values (the "keyset") from the last returned record to fetch the next page. Advantages: Performance: Efficient for large, ordered datasets. Consistent: Less prone to data inconsistencies compared to offset-based pagination. Disadvantages: Complexity: Requires a well-defined sort order and understanding of database indexing. Resources: The Ultimate Guide to Keyset Pagination https://planetscale.com/blog/the-ultimate-guide-to-keyset-pagination Keyset Pagination: An Introduction: https://use-the-index-luke.com/no-offset Choosing the Right Approach: Dataset Size: For small datasets, offset-based pagination might be sufficient. For large datasets, consider cursor-based or keyset-based pagination. Data Consistency: If consistency is critical, use cursor-based or keyset-based pagination. Implementation Complexity: Balance performance benefits with the complexity of implementation. Tips for Implementation: Clear Documentation: Document your API's pagination scheme for client developers. Error Handling: Handle invalid or missing pagination parameters gracefully. Default Values: Provide sensible default values for pagination parameters (limit, cursor, etc.).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment