Read replicas
Read replicas in RayDB allow you to scale read workloads by distributing queries across multiple instances. This improves performance, enhances availability, and reduces latency for read-heavy applications.
How Read Replicas Work
- Asynchronous Replication: Data changes on the primary cluster are replicated to read replicas with minimal lag.
- Read-Only Access: Replicas can only handle SELECT queries and cannot process writes.
- Automatic Failover: Replicas can be promoted to a primary instance in case of failure (if high availability is enabled).
When to Use Read Replicas
- Scaling Read Traffic: Offload read-heavy queries from the primary instance.
- Geographical Distribution: Deploy replicas in different regions to improve query latency.
- Analytics & Reporting: Run intensive reporting queries on replicas without impacting the primary database.
Creating a Read Replica
- Log in to the RayDB Dashboard.
- Navigate to the Clusters section and select your primary cluster.
- Open the Replication tab.
- Click Create Read Replica.
- Choose the region and instance size for the replica.
- Click Create to deploy the read replica.
Connecting to a Read Replica
- Go to the Cluster Details page.
- Locate the Replica Connection String.
- Use the replica connection string in your application to route read queries.
Example Connection (Python psycopg2):
import psycopg2
conn = psycopg2.connect("postgres://user:password@replica-host:port/database?sslmode=require")
cursor = conn.cursor()
cursor.execute("SELECT * FROM my_table;")
print(cursor.fetchall())
conn.close()Considerations
- Replication Lag: Queries on replicas might not reflect the latest changes immediately.
- Read-Only Limitation: Ensure your application routes only read queries to replicas.
- Failover Strategy: If a read replica is promoted to a primary, update connection settings accordingly.
Best Practices
- Use Connection Pooling: Optimize read traffic with a connection pooler like PgBouncer.
- Monitor Replica Lag: Regularly check replication lag to ensure up-to-date data.
- Distribute Read Queries: Load balance across multiple replicas for better performance.
For more details on scaling strategies, refer to Horizontal Scaling.