Skip to content

infra: add read replica support and query routing #47

@snowrugar-beep

Description

@snowrugar-beep

Problem Statement

A single PostgreSQL instance handles all reads and writes. There is no read replica support, meaning read scalability is limited and there is a single point of failure for all database operations.

Evidence

  • Backend/src/database/database.module.ts — Single TypeORM datasource
  • infrastructure/terraform/rds.tf — Single db.t3.medium instance

Impact

No read scalability. Single point of failure for all database operations. Read-heavy workloads (gist listing) compete with writes (new gists) on the same instance.

Proposed Solution

  1. Configure read replica in Terraform (add read replica instances)
  2. Configure TypeORM read/write replication: replication: { master, slaves: [] }
  3. Route read queries (findNearby, findByGistId) to replica(s)
  4. Route write queries (create) to master
  5. Handle replica lag with staleness tolerance configuration

Technical Requirements

  • Must use TypeORM's built-in replication support
  • Must handle replica lag gracefully (eventual consistency for reads)
  • Must fall back to master if replicas are unavailable
  • Terraform must create replica(s) with matching instance class

Acceptance Criteria

  1. Terraform creates read replica instance(s)
  2. TypeORM configured with master and slave(s)
  3. CREATE operations go to master
  4. SELECT operations go to replica(s)
  5. Replica failure falls back to master transparently
  6. Replica lag is monitored and reported
  7. All existing tests pass

File Inventory

  • infrastructure/terraform/rds.tf
  • Backend/src/database/database.module.ts
  • Backend/src/database/data-source.ts

Dependencies

None.

Testing Strategy

  • Integration test: verify reads go to replica, writes to master
  • Failover test: stop replica, verify reads fall back to master
  • Performance test: compare read throughput with and without replicas

Security Considerations

Replicas must have the same security group restrictions as master. Data at rest encryption must apply to replicas.

Definition of Done

  • Read replica configured in Terraform
  • TypeORM replication configured
  • Query routing implemented
  • Fallback working
  • Tests passing

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions