Engineering & Architecture
When reporting needs its own database
A soft, steady sound, a bit like rain, that helps some people focus while they read.
It usually starts with a report that takes a minute to load.
When reporting is the crux of the business, people run a lot of reports every day, and the application creates a lot of data just for those reports. All of it runs on the same SQL database that serves the application. Over time, that puts immense load on the database, and the application starts to feel it.
That's usually when someone suggests another database. Sometimes they are right. Before going there, it helps to understand what each kind of database is good at, and what the quick fixes actually give you.
Two kinds of work
Transactional and analytical databases both use SQL, but they are built for very different kinds of work. Updating an order is transactional work, or OLTP. Adding up a year of orders is analytical work, or OLAP.
| Transactional (OLTP) | Analytical (OLAP) | |
|---|---|---|
| What it's for | Running the application day to day | Answering reporting questions |
| A typical request | Read or change a few records | Scan, join and add up many records |
| How data is stored | Row by row | Column by column, as in Redshift |
| How the data is shaped | Around the application's records and transactions | Around the questions reports ask |
| How fresh it must be | Immediately, after every change | As fresh as the report needs |
When one database does both all day, the two kinds of work compete for the same memory, CPU and disk.
What people try first
Read replicas are usually the first idea. You point the reports at a replica and the main database gets some relief. It works for a while, but every replica is another full database instance to pay for, and a replica only serves reads. If preparing the report data involves heavy writes, that work still runs on the main database.
A bigger RDS instance is the other quick answer. It can give the database some relief when it is short on memory or CPU, and sometimes that is the right call while you investigate. But it costs more every month, and an inefficient query stays inefficient.
Aurora is worth considering. It separates the writer from the readers, so the application keeps writing on one node while the reports run on others. That separation is genuinely useful.
But Aurora can get costly if the queries are badly written or the data structure is not optimal. On Aurora Standard, you pay for every I/O request, so a query that reads far more data than it needs shows up on the bill. Aurora I/O-Optimized removes that charge, but you still pay for the compute those queries use. Every reader is another instance. And Aurora Serverless will scale up to handle work that shouldn't be there in the first place.
So Aurora is not a solution for everything. Nor is a bigger RDS instance.
When I'd choose an analytical database
I'd start considering an analytical database like Redshift when:
- Reporting is a substantial workload in its own right, not a few slow queries
- Reports scan long periods of history, join large datasets and repeat the same calculations
- Preparing data for reports is itself putting load on the application's database
- Reports can be a few hours behind the application and still be useful
- The data structure is a poor fit for reporting, and changing it inside a live application would be substantial and risky
Redshift is built for this kind of work. It stores data by column and spreads a query across many nodes, so a report that needs a few columns across millions of rows reads far less data.
How the data gets to Redshift
The SQL database stays the source of truth for the application. Redshift holds a copy for reporting, and the reports query Redshift.
Changes from the SQL database are captured as they happen and land in Amazon S3. Every hour, they are loaded into staging tables in Redshift. Every four hours, the staging tables are merged into the target tables, which hold the reporting copy of the data. Once that is done, a Step Functions workflow rebuilds the reporting tables from the latest data.
This is a common pattern I've seen. You can decide how often to load the data based on how people use the reports, and how old the numbers can be before they stop being useful.
One rule matters more than the schedule. Rebuild the reporting tables only once the target update has finished, rather than at a fixed time. Otherwise a report can mix old and new numbers.
When the data structure is part of the problem
More often than not, I've found that badly structured data is one of the causes of the bottleneck. Reports have to join and recalculate the same things again and again, because the data was shaped for the application, not for the questions people ask.
The right fix is to correct the structure. But once a business has grown around an application, the engineering effort to rearchitect the data underneath it is substantial and risky. This is where moving to Redshift can help.
If the data structure is less than ideal, build materialized tables for reporting. They make it much easier to serve the data. By materialized tables I mean tables your own workflow builds after every load, not Redshift's materialized views, though the idea is similar.
Of course, they should only cover a finite period, based on what people commonly ask for. Older requests can fall back on the regular Redshift tables, so the expensive work never goes back to the SQL database.
That does not remove the technical debt in the application. But you can make the reports faster without fixing it first.
Keep Redshift Serverless efficient
Redshift Serverless can do wonders here, provided the query shapes stay consistent. When the same query comes in again and the data hasn't changed, Redshift returns the cached result without running the query. It also caches compiled query code, so familiar query shapes start faster. Serverless bills per second while it works, with a 60-second minimum, and nothing while it is idle, so every answer that comes from the cache is a query you didn't pay to run again.
I wouldn't run queries on a timer just to keep them warm, though. Redshift already keeps compiled code in a cache that persists, and every extra run is compute you pay for.
What I've seen when comparing them
When I compared the two, a report query that took a minute on the SQL database ran in less than 10 seconds on Redshift with materialized tables.
Even the queries that fell back on the less ideal table structure in Redshift outperformed the SQL database. So with large datasets, Redshift was a clear winner.
With small datasets, Redshift was on par with the SQL database or performed slightly better, but only with the materialized tables.
When I'd stay with the transactional database
If the expectation is real-time data that reflects changes made elsewhere in the application, I'd definitely recommend staying with the transactional database. A copy loaded in batches will always be a little behind. Fix the data structure, the way the application queries the data and the queries themselves instead.
I'd also stay when the reports are straightforward, or when better queries and a sensible amount of capacity solve the problem at a reasonable cost.
Before you add another database
So when that report takes a minute to load, I'd start with the query, the data structure and the way the application asks for the data. If that fixes it, you're done, and you have one less system to maintain.
If reporting has grown into a workload of its own, and the numbers can be a few hours old, an analytical database is worth the extra pipeline. Keep the SQL database as the source of truth, prepare the tables people ask for most, and measure before and after.
What matters is an application that stays responsive and reports people can rely on.
Sometimes that means another database. Sometimes it means finally fixing the query.
