undo
Go Beyond the Code
arrow_forward_ios

True Scalability with OpenSearch Aggregations: Move Compute, Not Data

Samuel Román
Software Engineer & Solver
September 3, 2026
To learn more about this topic, click here.

OpenSearch Aggregations: Move Compute, Not Data, for Real Scalability

In modern software engineering, we take the easy way out when designing data intensive systems: we use our search engines and databases as simple storage. We tend to extract a lot of data (SELECT * or match_all) and then process it in our application layer using complex logic. If you are building an MVP with 100 users, it can work. However, when you are designing a system to scale, this strategy is the perfect recipe to have red bottlenecks, memory spikes, and a slow user experience. Today, we are going to talk about a fundamental paradigm for scalability: move compute, not data. Specifically, we will explore how the Open Search Aggregations let us do complex analysis and operations in milliseconds.


1. Antipattern: "Bring all the data and sum up in the backend"

Let’s imagine we work in an E-commerce platform and we need to make an operation to know what is the average sale price per product category within the last 30 days.


The wrong approach (Application-Side Processing)
  1. Consult OpenSearch filtering by the last 30 days.
  2. Configure a 10000 size (or even worse, using iteration) and get all the JSON documents.
  3. Transfer those megabytes through the network from the cluster to the microservices.
  4. Deserialize the JSONs to objects in memory (Java, Python, Node.js, etc).
  5. Iterate in the list, group by category and calculate the average.

Why does this not escalate?

The solution should not be to request more RAM. The solution is Aggregations.


2. Why are OpenSearch aggregations so fast?

In order to understand why aggregations are much faster than the application processing, we must look into the motor: Apache Lucene.

OpenSearch uses two principal data structures:

  1. Inverted Index: Optimized for searching ("Which documents do the word 'Shirt' appear in?").
  2. Doc Values: Optimized for aggregating and ordering.

The "Doc Values" secret

SQL databases are used to be orientated to rows (save all the user data together), whereas OpenSearch uses storage in columns for the fields we will aggregate.

When you request an aggregation, OpenSearch does not load the entire document (JSON _sorce), but it directly accesses the columnar structure in disk (or in the SO cache system).

Analogy: A library.

This allows us to scan millions of values per second with predictable memory usage.


3. Aggregation Query Architecture

An aggregation query has an interesting structure. The most important thing is the size: 0. This explicitly tells the motor: "Do not look for documents, I just want the answers".

There are different type of aggregations that cover most of the use cases:


A. Metric Aggregations

Calculates numeric values


B. Bucket Aggregations

Group documents into containers based on criterias.


C. Pipeline Aggregations

It is when we take the result of an aggregation and use it as input for another.


4. Use case: Sales Performance Dashboard

Let's go back to our initial problem. We want the total of sales per day and, inside each day, the breakdown per category.


Query en OpenSearch (DSL):



            


Result (Simplified):

The motor returns us a compact JSON of barely a few kilobytes, independently of if it processed 100 registers or 10 millions.



            


Our backend now acts simply as a light proxy or a minimal transformation layer, freeing resources to attend more concurrent HTTP petitions.


5. Performance Comparative: Fetch vs. Aggs

To illustrate the architectural impact with empirical data, we can look at official benchmarks and community case studies comparing traditional data extraction versus engine-side aggregations.

Metric "Fetch All" / Scroll Approach (App-Side) Aggregations Approach (size: 0) Source
Response Time (1 Million Docs) ~19 - 20 seconds Milliseconds Elastic Official Discuss Forum
Latency (Top 100 hits vs Top 100 buckets) 54.82 ms (High variance) 9.38 ms (Stable) OpenSearch GitHub Issue
Memory Overhead High (Prone to CircuitBreakingException) Low (Optimized via Doc Values) Elastic Performance Tuning Guide


6. Considerations and Trade-offs

We know that not everything is exactly perfect. Although aggregations are potent, they have their risks if they are not managed correctly.


Explosive Combinatory

If you nest many aggregations of terms type (e.g. Group by Country > then by City > then by User), the number of buckets grows exponentially. This can saturate the Heap memory of the OpenSearch nodes.


High Cardinality

Making aggregations over fields with millions of unique values (like user IDs or UUIDs) is expensive.


Circuit Breakers

OpenSearch has security mechanisms (indices.breaker.request.limit). If an aggregation tries to reserve too much memory, the cluster will abort the petition to protect itself.

DevOps Advice: Monitor the logs in search of CircuitBreakingException. It is an indicator that your queries need optimization, not necessarily that you need more hardware.


7. Conclusion

Aggregations in OpenSearch are not only a functionality to make pretty graphics in Kibana or OpenSearch Dashboards. They are a fundamental tool of backend architecture.

By adopting aggregations, we principally achieve:

  1. Stateless and Light Backend: Our services do not save state nor process gigabytes of data in each request.
  2. Real Scalability: The system supports a massive growth of data without linearly degrading the performance of the application.

Infrastructure Cost Reduction: By eliminating the need to transfer and process massive JSON payloads in memory, our application layer requires significantly less CPU and RAM. This allows us to deploy smaller, cheaper containers or microservices, directly reducing our cloud hosting bills while avoiding network bottlenecks.

Samuel Román
Software Engineer & Solver
Arrow icon go to top

Start Your Digital Journey Now!

Which capabilities are you interested in?
You may select more than one.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.