RayDB LogoRayDB

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

  1. Log in to the RayDB Dashboard.
  2. Navigate to the Clusters section and select your primary cluster.
  3. Open the Replication tab.
  4. Click Create Read Replica.
  5. Choose the region and instance size for the replica.
  6. Click Create to deploy the read replica.

Connecting to a Read Replica

  1. Go to the Cluster Details page.
  2. Locate the Replica Connection String.
  3. 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.

On this page