Video in progress
Business reports run on the main OLTP database and slow down customer requests during the day. What architecture do you propose?
Reports need to be separated from the operational database: in the short term indexes and materialized views, in the medium term a separate replica, and in the long term I move the data with CDC into an analytical database (ClickHouse, BigQuery and the like) and build the reports there.
📖 Answer
An OLTP database is built for short, indexed queries. A report like "sales by region over the last 12 months" scans millions of rows, pushes out the buffer cache, takes the I/O, and meanwhile the p99 of the payment endpoint spikes.
I propose the solution in stages, because the business expects a result tomorrow.
Now (days):
- Find the 5 heaviest queries with
pg_stat_statementsand add an index or rewrite them. - Put a
statement_timeouton report queries and use a separate DB user, so they are at least bounded. - Move aggregates into materialized views and refresh them at night.
Near term (weeks):
- A read replica, and the reporting service connects only to it. No load lands on the main database. But the replica is still in the OLTP format, so big aggregates are still slow; you have to account for replica lag and for long queries conflicting with replication (the
hot_standby_feedbacktrade-off in PostgreSQL).
Long term (months):
- CDC (Debezium) → Kafka → a columnar analytical warehouse (ClickHouse, BigQuery, Snowflake). The data lags by a few seconds or minutes, but aggregates are 100 times faster.
- The reporting model (star schema, denormalization) is independent of the OLTP model, so service teams can change their tables freely.
- A BI tool (Metabase, Superset) goes straight to the warehouse.
Trade-offs:
- Analytical data is not real time: agree with the business in writing on what lag each report allows.
- CDC exposes the internal schema to the outside. Renaming a column now also breaks the analytics pipeline; that is why I prefer a "data contract" or separately exported events.
- How personal data gets into the warehouse (masking, access rights) has to be decided from day one.
One more thing: close the "direct SQL against the database for reports" path. Otherwise new reports drift back to OLTP.
Sign up to mark studied questions and save your progress.