Gameball Logo

Infrastructure

Scaling Analytics with PostgreSQL Rollup Tables

7 min read
#Database
#Optimization
Omar Alfar, CTO
Omar Alfar

Introduction

In the world of e-commerce, data is generated at an astonishing rate. Every click, purchase, and interaction tells a story about customer behavior and market trends. Imagine running an e-commerce platform that processes millions of transactions daily. Now, imagine needing to generate a report that shows the volume of transactions per day for the last three months or to plot a comparison between the days of the week. Attempting to query this data directly from your operational database can be overwhelming, leading to slow performance and potential system downtimes.

In this article, we'll cover:

  • The challenge of querying large transactional datasets.
  • How rollup tables in PostgreSQL can help improve performance.
  • Steps to implement rollup tables for transaction data.
  • Customizing the frequency of data rollups.
  • Architectural approaches to optimize database performance.

The Challenge

Generating timely analytics directly from transactional data in an operational database can be very demanding. If your e-commerce site handles millions of transactions daily, querying this data to produce summaries or comparisons between days of the week can be slow and resource-intensive. This high demand can lead to slower performance for other critical operations.

Introducing Rollup Tables

To solve this challenge, we can use rollup tables. A rollup table is a database table that stores summarized data over specific periods. Instead of querying large transactional data tables, applications can query these pre-aggregated rollup tables to get results quickly. This reduces complexity and significantly improves performance.

Structuring Rollup Tables

To effectively use rollup tables, it's important to design them in a way that optimally supports the types of queries you need. Consider structuring your rollup table with the following columns:

  • Period: The time frame for the aggregation (e.g., hourly, daily). This column helps you group transactions by specific time intervals.
  • Customer ID: The identifier for the customer. This allows you to analyze data at the customer level.
  • Transaction Count: The number of transactions during the specified period. This gives a quick summary of transaction volume.
  • Total Amount: The total monetary value of transactions during the specified period. This helps in understanding the revenue generated.

Here's an example schema for such a table:

CREATE FUNCTION aggregate_transactions()
RETURNS void LANGUAGE plpgsql AS $$
BEGIN
    INSERT INTO transaction_rollup (period, customer_id, transaction_count, total_amount)
    SELECT date_trunc('hour', transaction_time), customer_id, COUNT(*), SUM(amount)
    FROM transactions
    WHERE transaction_time >= now() - interval '1 hour'
    GROUP BY 1, 2
    ON CONFLICT (period, customer_id)
    DO UPDATE SET transaction_count = transaction_rollup.transaction_count + EXCLUDED.transaction_count,
                  total_amount = transaction_rollup.total_amount + EXCLUDED.total_amount;
END;
$$;

In this function:

  • date_trunc('hour', transaction_time) truncates the transaction time to the nearest hour, allowing for hourly aggregation.
  • COUNT(*) counts the number of transactions in each group.
  • SUM(amount) calculates the total amount of transactions in each group.
  • The ON CONFLICT clause ensures that if a record for the same period and customer already exists, it updates the existing record instead of inserting a new one. This avoids duplicate records.

Automating Aggregation with pg_cron

To keep your rollup tables up-to-date, you need to run the aggregation function at regular intervals. The pg_cron extension allows you to schedule these tasks within PostgreSQL.

For example, to run the aggregation function every 5 minutes, you can use the following command:

SELECT cron.schedule('*/5 * * * *', $$
    SELECT aggregate_transactions();
$$);

This schedules the aggregate_transactions function to run every 5 minutes, ensuring that your rollup table is always up-to-date with the latest aggregated data.

Customizing the Frequency of Rollups

Depending on your needs, you can control how frequently your analytics are updated. For example, you might choose to roll up every 5 minutes for near real-time data or every 24 hours for daily summaries. Adjust the cron schedule according to your requirements:

-- Rollup every 5 minutes
SELECT cron.schedule('*/5 * * * *', $$ SELECT aggregate_transactions(); $$);

-- Rollup every 24 hours
SELECT cron.schedule('0 0 * * *', $$ SELECT aggregate_transactions(); $$);

Aggregation Based on Time and Another Dimension

Let’s extend our example to aggregate data based on time and another dimension, such as product category. This provides insights into sales performance across different categories.

CREATE TABLE category_transaction_rollup (
    period timestamptz,
    product_category text,
    transaction_count bigint,
    total_amount numeric,
    PRIMARY KEY (period, product_category)
);

INSERT INTO category_transaction_rollup
SELECT date_trunc('day', transaction_time), product_category, COUNT(*), SUM(amount)
FROM transactions
WHERE transaction_time >= now() - interval '1 day'
GROUP BY 1, 2
ON CONFLICT (period, product_category)
DO UPDATE SET transaction_count = category_transaction_rollup.transaction_count + EXCLUDED.transaction_count,
              total_amount = category_transaction_rollup.total_amount + EXCLUDED.total_amount;

Sample Data:

Period            | Product Category | Transaction Count | Total Amount
------------------|------------------|-------------------|-------------
2023-06-01        | Electronics      | 150               | 50000
2023-06-01        | Clothing         | 200               | 30000

Architectural Approaches

To ensure that your operational database remains unaffected by the additional load of analytics queries, consider the following architectural approaches:

Dedicated Database for Rollups

You can maintain a dedicated database for rollup tables, separating it from your operational database. This way, heavy analytical queries won't impact the performance of your primary operations.

Example Setup
  1. Staging Table: Use a staging table in your dedicated database to collect transactional data before aggregating it.
  2. Data Flow: Implement a method to move data from the operational database to the staging table. This can be done using replication or a message-based approach like Kafka.

Replication Approach:

Using PostgreSQL's logical replication, you can continuously replicate changes from the operational database to the dedicated database.

-- On the operational database
CREATE PUBLICATION my_publication FOR TABLE transactions;

-- On the analytics database
CREATE SUBSCRIPTION my_subscription
CONNECTION 'dbname=mydb host=primarydb.example.com user=myuser password=mypassword'
PUBLICATION my_publication;

-- Aggregating data from staging to rollup table
INSERT INTO transaction_rollup (period, customer_id, transaction_count, total_amount)
SELECT date_trunc('hour', transaction_time), customer_id, COUNT(*), SUM(amount)
FROM staging_transactions
WHERE transaction_time >= now() - interval '1 hour'
GROUP BY 1, 2;

Message-based Approach with Kafka:

  1. Setup Kafka to Stream Data: Stream transactional data from the operational database to a Kafka topic.
  2. Microservice for Aggregation: Create a microservice that consumes data from Kafka, aggregates it, and stores it in the rollup table.

Example:

  • Kafka Producer: Streams transactional data to Kafka.
from kafka import KafkaProducer
import json

producer = KafkaProducer(bootstrap_servers='localhost:9092',
                         value_serializer=lambda v: json.dumps(v).encode('utf-8'))

# Example transactional data
transaction_data = {
    "transaction_id": 1,
    "transaction_time": "2023-06-01T12:00:00Z",
    "customer_id": 123,
    "amount": 100.00,
    "product_category": "Electronics"
}

producer.send('transactions', transaction_data)

Kafka Consumer Microservice: Consumes data from Kafka, processes it, and inserts it into the staging table for aggregation.

from kafka import KafkaConsumer
import json
import psycopg2

consumer = KafkaConsumer('transactions', bootstrap_servers='localhost:9092',
                         value_deserializer=lambda x: json.loads(x.decode('utf-8')))

conn = psycopg2.connect("dbname=mydb user=myuser password=mypassword")
cur = conn.cursor()

for message in consumer:
    transaction = message.value
    cur.execute("INSERT INTO staging_transactions (transaction_id, transaction_time, customer_id, amount, product_category) VALUES (%s, %s, %s, %s, %s)",
                (transaction['transaction_id'], transaction['transaction_time'], transaction['customer_id'], transaction['amount'], transaction['product_category']))
    conn.commit()

Conclusion

Using rollup tables in PostgreSQL for e-commerce analytics can significantly improve the performance and efficiency of your data queries. By periodically aggregating data, leveraging automation tools like pg_cron, and utilizing dedicated databases or microservices, you can build a robust analytics system that scales with your data.

For businesses seeking to gain immediate insights from their data, implementing rollup tables is a proven strategy that combines simplicity, efficiency, and performance. While having a data warehouse and a dedicated data team to build analytical data models is ideal, it may not always be feasible. Rollup tables offer an effective alternative to achieve fast, efficient analytics even without extensive infrastructure or resources.


More Stories

Backend

Boosting Web Performance with Brotli: A Practical Guide to Compression in .NET

5 min read
Mohaned Mashaly
Mohaned Mashaly

Backend

How to update 1 billion rows without blocking the table

6 min read
Galal
Galal Shaban

Infrastructure

Building a URL Shortener with AWS, Golang, and the AWS CDK

12 min read
Mohamed Ashraf
Mohamed Ashraf
Gameball Logo
Install Gameball on Shopify
Install Gameball on SallaInstall Gameball on Salla