We were working on a surveillance platform that processes over 20 million messages every day to detect potential compliance violations. The platform stores incoming messages in Amazon S3 so they can be searched and reprocessed later.
One of the requirements was to support historical reprocessing. When a compliance policy was added or updated, the platform needed to run it against previously ingested messages to check for anomalies that might have been missed. With a search window of up to 30 days, this meant searching through hundreds of millions of messages.
We used Amazon Athena to query the historical data stored as JSON files in S3. One such query took nearly 30 minutes and scanned 230 GB of data.
We found two reasons for this: Athena was reading more data than necessary, and it had to process a large number of small JSON files. We moved to Parquet and introduced a daily compaction job to address both problems.
Why Athena struggled with small JSON files
Our ingestion pipeline continuously wrote incoming messages as individual JSON files in S3, partitioned by tenant and day. File sizes ranged from a few KB to tens of MB, with an average of around 50 KB. A single tenant could accumulate thousands of files in one day’s partition.
We used Amazon Athena to query these files. We had created an external table in Athena that pointed to the JSON files stored in S3. The table defined the schema of the records, allowing us to query them using SQL without loading the data into a separate database.
The existing layout had two problems.
Athena had to read more data than necessary: Our archived messages were stored as JSON files in S3. When Athena queried these files, it had to read and parse the JSON records to find the requested data. JSON also stores field names alongside their values, which adds to the amount of data being read. With millions of records, Athena ended up scanning hundreds of gigabytes for a single search. A more compact storage format could reduce the amount of data Athena needed to read.
There were too many small files: Each file introduced overhead for planning, opening, and reading it. With a large number of small files across the queried partitions, that overhead added up, even when individual files contained very little data.
We needed to reduce both the data scanned and the number of files Athena had to process.
Why we chose Parquet
Apache Parquet is a columnar file format, meaning it stores values from the same column together instead of storing complete records one after another, as JSON does.
This layout makes Parquet more efficient in two ways. First, when a query needs only a few columns, Athena can read those columns without reading the rest. Second, storing similar values together makes encoding and compression more effective. For example, if a column contains the value INFO thousands of times, Parquet can use techniques such as dictionary encoding and run-length encoding to represent repeated values more efficiently instead of storing each occurrence in full.
In our case, searches accessed around 11 of the 13 stored fields, so reading fewer columns wasn’t the main benefit. We expected a larger reduction from how Parquet encoded and compressed the data.
Several of our fields contained the same values repeatedly. Parquet can store these values more efficiently using techniques such as dictionary encoding and run-length encoding. We also used Snappy compression to reduce the file size further. Unlike JSON, Parquet doesn’t repeat field names for every record.
We’d seen similar benefits when working with sparse data in ClickHouse, where the way data is stored affects how much the query engine needs to read.
However, Parquet alone wouldn’t solve our second problem: the large number of small files. If we converted each JSON file into a separate Parquet file, Athena would still have to process thousands of small files. We needed to combine the data from many JSON files into fewer, larger Parquet files, a process known as compaction.
The next question was how often to run this conversion and how to control the number of files Athena generated.
Choosing a compaction approach
We considered a few approaches before settling on daily compaction.
Approach | Reduces data scanned? | Reduces file count? | Decision |
|---|---|---|---|
Keep raw JSON | No | No | Existing 30-minute search |
Hourly | Yes | Not enough | Still produces too many small files |
Hourly Create Table As Select (CTAS) | Yes | Yes, with suitable output sizing | Requires managing new tables |
Daily compaction into a fixed table | Yes | Yes, relative to hourly writes | Chosen approach |
Why hourly INSERT INTO didn’t work
Our plan was to create a separate Parquet-backed table in Athena while keeping the original JSON table unchanged. A background job would periodically read data from the JSON table and write the converted records into the Parquet table.
We initially considered running this job every hour, using INSERT INTO to convert the last hour’s JSON data into Parquet.
When Athena executes an INSERT INTO on a Parquet-backed table, it writes new Parquet files to the table’s S3 location. These files contain the records returned by the query, converted into Parquet format. A single query can produce multiple files because Athena uses multiple workers to write the output in parallel.
During testing, we noticed that each INSERT INTO generated around 30 Parquet files, regardless of how much data we were converting. Athena doesn’t let us directly specify the exact number of output files for an INSERT INTO query. The number depends on Athena’s internal execution plan and writer parallelism.
Running this job every hour would therefore generate hundreds of small Parquet files per tenant per day. We would reduce the data scanned by searches, but Athena would still have to process many small files, leaving part of the original performance problem unresolved.
We would also pay for scanning the JSON data during every hourly conversion.
Why not hourly CTAS?
We also considered using CREATE TABLE AS SELECT (CTAS) to convert the JSON data into Parquet. Unlike our INSERT INTO approach, CTAS allowed us to influence the number and approximate size of output files using bucketing. By configuring bucketed_by and bucket_count, we could group records into fewer, larger files.
The problem was that every hourly run would create a separate table. Our search queries could span up to 30 days, so we would need to keep track of which tables contained data for the requested date range and include those tables in the read query. This would make the query generation logic more complicated as the number of tables grew.
Another option was to maintain a single Parquet table and register the files generated by each CTAS run as partitions of that table.
Athena tables reference data stored in S3. For a partitioned table, each partition points to an S3 location containing the files for a particular set of partition values. We could have configured each CTAS run to write its output to a separate S3 location and then registered that location as a partition of the common table using ALTER TABLE ADD PARTITION.
For example:
This would allow searches to query a single table instead of tracking individual CTAS tables. However, our compaction job would now need to manage partition registration, handle failed or repeated registrations, and clean up the temporary CTAS tables created during conversion.
We didn’t think this additional catalog management was worth it, so we moved away from hourly CTAS.
What we shipped: daily compaction into one table
Instead of converting JSON data into Parquet every hour, we decided to compact an entire day’s data in a single run. This would reduce how often Athena generated output files while keeping the conversion process simple. We would also maintain a single Parquet table for all the converted data, so searches wouldn’t need to track multiple tables.
We created a separate Parquet-backed table in Athena, partitioned by tenant_id and day, with a schema matching the existing JSON table. A background job would pick up each day’s data after that day had ended and a configurable settling period had passed. For example, Monday’s data would become eligible for compaction on Tuesday, after the settling period. This gave late-arriving messages some additional time to reach S3 before conversion.
The job converted the eligible day’s JSON data into Parquet using INSERT INTO:
Both the original JSON table and the new Parquet table were partitioned by tenant and day. Each partition represented data belonging to a particular tenant on a particular day, stored under its corresponding S3 location.
For example, when converting data for tenant_1 on 2026-01-01, the WHERE clause allowed Athena to read only that tenant’s data for that day, rather than scanning files belonging to other tenants or dates. Athena then wrote the converted records as Parquet files under the corresponding partition of the destination table.
Unlike hourly conversion, which would run 24 inserts per tenant per day, we now ran one insert per tenant per day. Athena could still generate multiple output files for each insert, but running the conversion once a day avoided repeatedly creating new batches of small files.
Since all conversions wrote to the same Parquet table, searches could query it directly without tracking separate tables or managing CTAS output.
Keeping daily compaction safe
We implemented the compaction job in one of our Spring Boot services. The job periodically identified tenant-day combinations eligible for compaction and submitted INSERT INTO queries to Athena to convert their JSON records into Parquet.
Since these queries ran asynchronously, we needed to track which tenant-day combinations had been processed, which were still in progress, and which needed to be retried. We maintained this information in a MongoDB collection, with three possible states for each tenant and day:
A tenant-day started as NOT_COMPACTED. Once the job submitted its conversion query to Athena and stored the query execution ID in MongoDB, it moved to IN_PROGRESS. A separate scheduled job periodically checked the status of these queries using their execution IDs. If a query completed successfully, we marked the tenant-day as COMPACTED. If it failed or was cancelled, we moved it back to NOT_COMPACTED so the compaction job could retry it later.
We didn’t compact a day immediately after midnight. The job waited until the day had ended and a configurable settling period had passed. This allowed time for late-arriving messages to be written to S3 before conversion.
We also limited the number of concurrent compaction queries submitted to Athena. During the initial backfill, the last 30 days were eligible for conversion, but submitting all of them at once could exhaust Athena’s concurrent query capacity or hit its API rate limits. To avoid this, we limited how many queries the job could submit in each run, processing the backlog in smaller batches.
Since our Spring Boot service ran on multiple replicas, we used ShedLock to ensure only one replica scheduled compaction work at a time.
While a tenant-day’s JSON records were being converted into Parquet, searches continued reading from the original JSON table. We only allowed searches to use Parquet after all the tenant-day combinations required by the search were marked as COMPACTED.
Switching reads to Parquet during the backfill
Converting the data wouldn’t improve search performance unless we also changed where searches read from.
We already had historical data in JSON, and converting the trailing 30 days would take time. During that period, some days would have Parquet copies while others would still be available only in JSON.
We introduced a feature flag and used the compaction status to decide which table a search should query.
For example, suppose a search covers 30 days. If 29 days are compacted but one is still IN_PROGRESS, the search reads the entire range from JSON. Once the remaining day is marked COMPACTED, the same search can use Parquet.
Our existing search implementation queried a single source table. We could have split the date range between JSON and Parquet and combined the results, but that would have required additional query generation and result-handling logic.
Instead, we chose an all-or-nothing approach. A search uses Parquet only when every day in its requested range is ready.
With the feature flag disabled, searches behave exactly as before. The compaction job can continue converting data without affecting the read path. If we need to switch back, we can disable the flag and route searches to JSON again.
Rolling out on a live archive
The platform was already running in multiple environments. We needed to backfill the historical data without affecting existing searches.
We rolled out the change in three steps:
Backfill with Parquet reads disabled. We deployed the compaction job while searches continued reading JSON. The job processed historical days in bounded batches, prioritising the most recent days.
Enable Parquet reads environment by environment. We enabled the feature in the performance testing environment first, followed by staging and production. Even with the flag enabled, searches used Parquet only when all requested days were marked
COMPACTED.Keep rollback simple. Each environment had its own feature flag. If we encountered a problem, we could switch searches back to JSON without stopping the compaction jobs or modifying the original archive.
How much faster did the searches get?
We ran the same search against the original JSON table and the compacted Parquet table in our performance environment. Both queries used the same filters and date range and returned the same 13,415 rows.
Metric | Raw JSON | Parquet |
|---|---|---|
Query time | 29 min | 18.1 sec |
Data scanned | 230.23 GB | 3.19 GB |
Cost per search (at $5/TB) | ~$1.12 | ~$0.015 |
The Parquet query was roughly 96 times faster, while the data scanned dropped by 98.6%. At Athena’s $5/TB query pricing, the cost per search was nearly 75 times lower.
The data also occupied less space in S3. The original JSON files took around 231 GB, while the Parquet files occupied approximately 38 GB.
But why did Athena scan only 3.19 GB when the Parquet files occupied 38 GB?
The storage size represents all the data in those Parquet files, while the scanned data represents what Athena needed to read for this particular query. Because Parquet stores data in columns, Athena can avoid reading columns that aren’t needed. Depending on the query and the file metadata, it can also skip some groups of records that don’t match the filters.
Together with Parquet’s more compact encoding and compression, this allowed Athena to scan much less data than it did with JSON.
What about the compaction cost?
Each conversion required an additional scan of the original JSON data to produce the Parquet output.
For the 230 GB of JSON behind our benchmark, a conversion scanning the same amount would cost approximately $1.12 at Athena’s $5/TB rate. After compaction, each search would cost around $0.015 instead of $1.12, saving roughly $1.10 per search.
At that rate, the conversion’s scanning cost would be recovered after roughly two searches of the same data. Since reviewers could repeatedly search historical messages when evaluating policies, the one-time conversion cost was worth it.
Trade-offs
Daily compaction came with a few trade-offs.
Additional storage. We kept both JSON and Parquet copies of the compacted data in S3. Although Parquet occupied considerably less space, maintaining both copies increased storage costs.
All-or-nothing search routing. A search could use Parquet only after its entire date range was compacted. During the initial backfill, even one unconverted day meant the search would fall back to JSON for the entire range.
Summary
Moving to Parquet reduced the amount of data Athena scanned, but converting the files once per day also helped us avoid generating too many small output files.
The remaining work was making that conversion safe to run against a live archive, without changing how existing searches behaved. We handled that through compaction states, bounded query submissions, and a feature flag that switched reads only when the requested data was ready.
With those changes, a search that previously took 29 minutes completed in 18 seconds.











