Introduction
When working with Elasticsearch for data analysis, you often need to query millions or even tens of millions of records. Traditional pagination with from/size suffers from severe performance degradation at deeper pages. That’s where the Scroll API comes to the rescue.
This article covers the Scroll API with real-world examples and best practices.
What is the Scroll API?
The Scroll API is a batch retrieval mechanism in Elasticsearch. Unlike traditional pagination, it creates a snapshot of the query context in memory, allowing you to paginate through all matching documents efficiently — without the deep pagination overhead.
Why Use Scroll?
- Performance: Avoids deep pagination penalties
- Memory-friendly: Processes data in batches rather than loading everything at once
- Ideal for: Data exports, batch processing, ETL pipelines, analytics
Step-by-Step Guide
Step 1: Initialize a Scroll Query
Start by sending a search request with a scroll parameter to set the context lifetime:
| |
Parameter Breakdown:
| Parameter | Description |
|---|---|
scroll=5m | Scroll context lives for 5 minutes; auto-freed on expiry |
size: 50000 | Returns 50,000 hits per batch |
_source: false | Skip full _source to reduce payload size |
fields | Only return specific fields |
Note: The initial response includes a
_scroll_id. Some clusters return a new_scroll_idon every subsequent call — always use the latest one.
Step 2: Fetch Subsequent Batches
Use the _scroll_id from the previous response to get the next batch:
| |
Key Points:
- Each response may return a new
_scroll_id— always use the most recent one - When you receive an empty
hitsarray, you’ve reached the end of the dataset - Re-set
scrollon every request to reset the TTL (prevents premature expiry)
Step 3: Clean Up Resources
Always clear the scroll context when done to free server memory:
| |
You can also clear multiple scroll IDs at once:
| |
Practical Use Cases
1. Data Export (Python)
| |
2. Batch Data Analysis
Process large datasets in chunks to avoid out-of-memory errors in your analysis pipeline.
3. Cross-Cluster Migration
Scroll API is the standard approach for reading data from a source cluster during index migration.
Best Practices
1. Set Appropriate TTL
- Keep scroll context TTL between 1–5 minutes
- Reset the TTL on each request by re-sending
scroll - Long TTLs consume more memory — avoid excessive durations
2. Control Batch Size
- Recommended
size: 5,000 — 50,000 - Too large → slow queries and higher memory pressure
- Too small → excessive network round-trips
3. Always Clean Up
- Manual cleanup is more reliable than relying on TTL expiry
- Orphaned scrolls consume cluster resources
4. Consider search_after for Real-Time
For user-facing search features, use search_after instead of scroll — it doesn’t create a snapshot and has lower overhead.
Important Caveats
- Not real-time: Scroll queries work on a snapshot. Updates made after the initial query won’t appear.
- Memory cost: Each scroll context uses memory. Too many concurrent scrolls can degrade cluster performance.
- Not for user requests: Scroll is designed for background batch jobs, not real-time search.
- Sorting: For best performance with scroll, use
_docsorting.
Summary
The Elasticsearch Scroll API is an essential tool for batch data processing at scale. Key takeaways:
- Set a reasonable scroll TTL
- Choose an appropriate batch size
- Always clean up after yourself
- Use
search_afterfor real-time search instead
This guide should help you handle large-scale data exports, migrations, and analysis with confidence!
References: