Java Tutorial 0/145 lessons ~6 min read Lesson 68

    Pagination

    pagination returning 100,000 rows in one api call will crash your app and your database. spring data's pageable interface provides standard offset/limit

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Returning 100,000 rows in one API call will crash your app and your database. Spring Data's Pageable interface provides standard offset/limit pagination with sorting — essential for every list endpoint.

    Informative example

    Paginated REST endpoint:

    ts
    @GetMapping("/products")
    public Page<ProductDto> list(
    @RequestParam(defaultValue = "0") int page,
    @RequestParam(defaultValue = "20") int size,
    @RequestParam(defaultValue = "name,asc") String[] sort
    ) {
    Pageable pageable = PageRequest.of(page, size, Sort.by(parseSort(sort)));
    return productRepo.findAll(pageable).map(ProductDto::from);
    }
    // Repository
    Page<Product> findByCategory(String category, Pageable pageable);
    // Response includes: content, totalElements, totalPages, number, size

    Best practices

    • Cap max page size (e.g. 100) to prevent abuse.
    • For deep pagination (page 10,000+), use keyset/cursor pagination instead of offset.
    • Return total count only when the UI needs it — it's expensive on large tables.

    Purpose of this lesson

    Master Pagination so you can apply it confidently in production Java code, technical interviews, and code reviews.

    Step-by-step explanation

    1. Understand the core idea behind Pagination.
    2. Walk through the runnable example and tweak it in the playground.
    3. Apply the pattern in a small Spring Boot or CLI exercise of your own.
    4. Re-read the common mistakes and interview Q&A to lock the concept in.

    Interactive workflow diagram

    1Pagination — typical flow
    1 / 4

    Identify use case

    Recognize when pagination is the right tool for the problem.

    Debugging tips

    • Read the full stack trace — Java's exception messages name the offending class and line.
    • Reproduce in the smallest possible main() method before fixing in the real app.
    • Use IntelliJ's debugger breakpoints and 'Evaluate Expression' rather than scattering System.out.

    Optimization strategies

    • Keyset pagination: WHERE id > :cursor ORDER BY id LIMIT 20 — O(1) vs OFFSET which scans skipped rows.

    Enterprise example

    Teams at Netflix, Uber and Goldman Sachs apply Pagination daily — usually wrapped behind Spring Boot services with observability hooks (Micrometer + OpenTelemetry).

    Interview questions & answers

    Q1Explain Pagination in one minute.
    Describe what problem it solves, the JDK APIs involved, and one production trade-off.
    Q2When would you avoid Pagination?
    Mention performance, complexity, or readability cases where a simpler approach wins.

    Summary

    In this lesson you learned Pagination — the concept, syntax, a runnable example, and the production pitfalls to avoid. Apply it in the playground before moving on.

    Ready to mark this lesson complete?Track your journey across the entire course.