Run analytics queries with Read Replicas
Leverage read replicas for running data-intensive analytics queries
With Neon's read replica feature, you can instantly create a dedicated read replica for running data-intensive analytics or reporting queries. This allows you to avoid disruption or performance degradation on your production database.
A read replica reads data from the same source as your primary read-write compute. There's no data replication, so creating a read replica is a near-instant process. For more information about Neon's read replica architecture, see Read replicas.
Scenario
Suppose you have a sales
table in your production database. The table and data might look something like this:
You want to find the total sale amount for each product in the past year, but due to the large number of products and sales in your database, you know this is a costly query that could impact performance on your production system.
This guide walks you through creating a read replica, connecting to it, running your query, and optionally deleting the read replica when finished.
Metabase Analytics Use Case
Metabase is an open-source business intelligence (BI) company that provides a platform for visualizing and analyzing data. With Metabase and Neon, you can:
- Create a read replica in Neon
- Configure Autoscaling to define minimum and maximum limits for compute resources
- Configure scale to zero to define how soon the read replica scales to zero when not being used
- Configure a connection to the read replica from Metabase.
With this setup, your read replica only wakes up when Metabase connects, scales to sync job requirements without affecting your production database, and scales back to zero after the job sync is finished.
Create a read replica
Creating a read replica involves adding a read replica compute to a branch.
You can add a read replica compute- to any branch in your Neon project by following these steps:
- In the Neon Console, select Branches.
- Select the branch where your database resides.
- Click Add Read Replica.
- On the Add new copmpute dialog, select Read replica as the Compute type.
- Specify the Compute size settings. You can configure a fixed size compute with a specific amount of vCPU and RAM (the default) or enable autoscaling by configuring a minimum and maximum compute size using the slider. You can also configure a Scale to zero time setting, which is the amount of idle time after which a compute suspends due to inactivity. The default setting is 5 minutes.
note
The compute size configuration determines the processing power of your database.
- When you finish making your selections, click Create.
Your read replica is provisioned and appears on the Computes tab of the Branches page. The following section describes how to connect to your read replica.
Alternatively, you can create read replicas using the Neon CLI or Neon API, providing the flexibility required to integrate read replicas into your workflows or CI/CD processes.
Connect to the read replica
Connecting to a read replica is the same as connecting to any branch, except you connect via a read replica compute instead of your primary read-write compute. The following steps describe how to connect to your read replica with connection details obtained from the Neon Console.
-
On the Neon Dashboard, under Connection Details, select the branch, the database, and the role you want to connect with.
-
Under Compute, select the Replica compute.
-
Select a Database and the Role you want to connect with.
-
Copy the connection string. This is the information you need to connect to the read replica from your client or application.
The connection string appears similar to the following:
postgresql://alex:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname
If you expect a high number of connections, select Pooled connection to add the
-pooler
flag to the connection string.The information in your connection string corresponds to the following connection details:
- role:
alex
- password:
AbC123dEf
- hostname:
ep-cool-darkness-123456.us-east-2.aws.neon.tech
- database name:
dbname
. Your database name may differ.
When you connect to a read replica, no write operations are permitted on the connection.
- role:
-
Connect to your application from a client such as
psql
or add the connection details to your application. For example, to connect usingpsql
, issue the following command:psql postgresql://alex:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname
Run the analytics query on the read replica
An analytics query on your sales
table might look something like this:
SELECT product_id, SUM(sale_amount) as total_sales
FROM sales
WHERE sale_date >= (CURRENT_DATE - INTERVAL '1 year')
GROUP BY product_id;
If you have a lot of products and sales, this query might impact performance on your production system, but running the query on your read replica, which has its own dedicated compute resources, causes no disruption.
Delete the read replica
When you are finished running analytics queries, you can delete the read replica if it's no longer required. Deleting a read replica is a permanent action, but you can quickly create a new read replica when you need one.
tip
Alternatively, you can let the read replica scale to zero so that it's readily available the next time you need it. Neon's Scale to Zero feature will suspend the compute until the next time you access it. The default scale to zero setting is 5 minutes, meaning that the read replica compute will automatically suspend after 5 minutes of inactivity.
To delete a read replica:
- In the Neon Console, select Branches.
- Select a branch.
- On the Computes tab, find the read replica you want to delete.
- Click Edit → Delete compute.
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.