Getting started with Readyset on PostgreSQL
5 min read
•
9 days ago
PostgreSQL is one of the most potent relational databases, but performance can become a bottleneck as queries become complex. One of the most effective ways to reduce query latency without modifying application logic is caching, and Readyset provides a seamless solution.
Readyset sits between your application and PostgreSQL, automatically caching queries and speeding up their response times considerably.
In this guide, we’ll walk through:
Installing Readyset on Ubuntu 24.04 using a .deb package;
Configuring Readyset to connect to your upstream PostgreSQL database;
Running queries and caching results using the Northwind sample database;
Managing cached queries to optimize performance.
The Northwind PostgreSQL Sample Database can be downloaded and imported as below:
You need to pay attention to security. We recommend creating a GROUP and a USER with the relevant configurations and permissions before you connect Readyset to your databases, which we call upstream databases. On the upstream database, you need to configure the wal_level as logical so Readyset can snapshot the database you have queries for caching and set up a logical replication to keep caching up-to-date.
Some other considerations for configuring your PostgreSQL with Readyset are at https://readyset.io/docs/reference/configure-your-database/postgres/generic-db-directions
Step 1: Prerequisites
Before setting up Readyset, ensure that a PostgreSQL database is already running. Readyset requires access to the database to snapshot the schema and cache queries.
Key Recommendations
Readyset should be installed on a different host than the PostgreSQL database to avoid resource contention.
The database server must allow Readyset to connect remotely.
For this tutorial, we assume:
- PostgreSQL is running on 10.0.0.11:5432;
- The Readyset is running on 10.0.0.12:5433;
- A database named northwind is already restored;
- A PostgreSQL user (readyset) with appropriate permissions exists.
Step 2: Installing Readyset on Ubuntu 24.04
2.1 Download the Readyset .deb Package
2.2 Install Readyset Using dpk
After installation, you should see:
You also need to install the psql on the Readyset host:
To make sure Readyset will be able to connect to the upstream database host, give it a try:
Step 3: Configuring Readyset
Before starting Readyset, configure it in /etc/readyset/readyset.conf.
3.1 Open the Configuration File
3.2 Set the Database Connection
- UPSTREAM_DB_URL: PostgreSQL connection string for Readyset to snapshot the schema and proxy queries when needed.
- LISTEN_ADDRESS: 5433 is the default Readyset port, ensuring compatibility when running on a separate host.
3.3 Start the Readyset Service
To check if it’s running:
If you want to check logs:
Step 4: Connecting to Readyset
Once Readyset is running, connect using the PostgreSQL client:
For more convenience when running queries, I will disable the “pager less” for returning result sets and enable the timing for the Readyset psql PostgreSQL client.
Since Readyset acts as a proxy, queries sent here will be ready to be cached.
Step 5: Running Your First Cached Query
Run a real-world query using PostgreSQL’s Northwind database:
Since this is the first run, PostgreSQL processes it as usual. The time is ~54 ms.
We can use the EXPLAIN LAST STATEMENT Readyset command to see where this query was executed:
As expected, the query was proxied to the upstream database.
Step 6: Checking Proxied Queries
To check if Readyset intercepted the query:
Expected output:
The query is “readyset supported”, as seen on the above output.
Step 7: Creating a Cache for the Query
To explicitly cache this query:
Step 8: Verifying the Cache
Check if the query is cached:
At this point, Readyset is caching this query!
Step 9: Running the Cached Query
Rerun the query:
The same query execution, now using the Readyset cache took ~1 ms.
If we again EXPLAIN LAST STATEMENT:
🚀 The query execution time dropped more than 50 times! Readyset serves the result from the created cache, avoiding a full database load.
Step 10: Removing a Cached Query
To remove the cache, run:
Verify that it's gone:
Conclusion
With Readyset, you can significantly improve PostgreSQL query performance without modifying application code.
Using Readyset improves efficiency and performance orders of magnitude;
Queries are cached reducing upstream PostgreSQL load;
With this strategy you don't need to add more replicas or hardware;
Create custom caches for the queries you want fully optimized;
Using SHOW CACHES helps track performance improvements;
The EXPLAIN LAST STATEMENT shows you where the last statement was executed;
You don’t need any code changes, you can start with readyset now.
Connect with us!
Play with our Cloud Demo: https://readyset.cloud/
Our Slack: https://readysetcommunity.slack.com/
Our Github: https://github.com/readysettech/readyset
Authors