Fork me 🍴

Willian Antunes

Index Rollover in Elasticsearch: A Beginner's Tutorial

• 2 minute read

elasticsearch, terraform, python

Rollover is a crucial action. It allows you to create a new write index when the current one meets a defined threshold. It makes your system more optimized and scalable. How does it work? Well, as always, let's observe its behavior through practice. Check out this project. It has an agent that executes GET _cat/thread_pool?v and GET /_tasks?pretty=true&human=true&detailed=true through a Python code. It parses the answers into JSON documents and sends them to index alias es-thread-pool or es-tasks. It requires an infrastructure in Elasticsearch to be created first. Execute the following command to create it:

./scripts/start-development.sh

Access the index templates page and use the credential elastic:elastic when it's complete. You'll see two custom index templates:

  • es-tasks-index-template
  • es-thread-pool-index-template

Now access the tab Data Streams. You'll see three system data streams created by Elasticsearch. Now execute the command:

docker compose run --rm -e TEST_COLLECT_METRICS=1 integration-tests python -m unittest tests.agents.test_elasticsearch.TestElasticsearch.test_collect_metrics

Two new data streams will appear, each one with one index:

Screenshot of the Kibana Index Management page showing a list of data streams with their health, indices, and data retention details.

If you click on the number of indices on es-tasks, you'll see its indices. In my case, it says there are 5 documents:

Screenshot of the Kibana Index Management page showing a single index named "ds-es-tasks-2025.03.24-000001" with health status "green", 5 documents, and 9.2kb storage size.

The rollover trigger is configured to happen when an index has at least 2 documents. After no more than 1 minute (know this), this is what happens:

Screenshot of the Kibana Index Management page showing two indices, "ds-es-tasks-2025.03.24-000001" and "ds-es-tasks-2025.03.24-000002", both with health status "green" and belonging to the "es-tasks" data stream.

A new index is created! It will occur every time a trigger criterion is matched. Keep in mind that this definition is for the sake of this post.

Don't need data streams? Change the flag enable_data_streams in the Terraform code and see how the rollover works when only indices are used without data streams.

I hope this may help you. See you! 😄


Have you found any mistakes 👀? Feel free to submit a PR editing this blog entry 😄.