flowfi icon indicating copy to clipboard operation
flowfi copied to clipboard

[Backend] Database Read Replicas & Connection Pooling Optimization with PgBouncer

Open blurbeast opened this issue 3 months ago • 9 comments

Overview & Background

As event volume and active stream queries grow, database connection limits and read query contention can degrade API latency. Setting up explicit read/write splitting in Prisma along with connection pooling optimization ensures backend responsiveness during high-traffic streaming volume.


Technical Specification & Architecture

  1. Read/Write Splitting with Prisma:
    • Configure Prisma read-replica extension (@prisma/extension-read-replicas) in backend/src/lib/prisma.ts.
    • Direct write mutations (indexer worker, user creations) to primary DB instance.
    • Route high-volume read endpoints (/streams, /events, /summary) to read replicas.
  2. Connection Pool Tuning:
    • Tune pg.Pool parameters in backend/src/lib/pg-pool.ts:
      • Max connections per container: 20
      • Idle timeout: 10,000ms
      • Connection timeout: 2,000ms
  3. Docker Compose & Production Setup:
    • Include PgBouncer sidecar configuration in docker-compose.yml.

Target Files

  • backend/src/lib/prisma.ts
  • backend/src/lib/pg-pool.ts
  • docker-compose.yml

Acceptance Criteria

  • [ ] Prisma routes read queries to read replica when DATABASE_READ_REPLICA_URL is configured.
  • [ ] Eliminates connection exhaustion under load tests (1,000 concurrent requests).
  • [ ] Graceful fallback to primary DB when replica is unavailable.

blurbeast avatar Aug 29 '26 22:08 blurbeast

let me work on this issue

jchinedu avatar Aug 29 '26 22:08 jchinedu

Hi @maintainers, I am ready to resolve this!

Execution Plan for [Backend] Database Read Replicas & Connection Pooling Optimization with PgBouncer:

  • Backend Implementation: Structure [Backend] Database Read Replicas & Connection Pooling Optimization with PgBouncer with strict input validation, idempotent handlers, and proper error codes.
  • Integration Tests: Provide automated tests covering both valid payloads and failure scenarios.

Why assign me?

  • Proven Proof-of-Work: #1 contributor on Mova Store (28 merged Web3 PRs) and official ElizaOS contributor.
  • Fast Turnaround: Dedicated to shipping a tested, production-grade PR within 12–24 hours.
  • Zero Maintainer Burden: Self-documenting code with green CI and tests included so you don't spend time debugging.

Ready to deliver within 12–24h—please feel free to assign it to me!

Ranjeet2063 avatar Sep 06 '26 10:09 Ranjeet2063

Hi everyone,

I can tackle [Backend] Database Read Replicas & Connection Pooling Optimization with PgBouncer. Proposed implementation roadmap:

  • [ ] Analysis: Inspect current behavior and edge cases in LabsCrypt/flowfi
  • [ ] Implementation: Structure backend handlers for [Backend] Database Read Replicas & Connection Pooling Optimization with PgBouncer with strict input validation, proper HTTP error codes, and idempotent execution.
  • [ ] Testing: Cover implementation with end-to-end integration tests for standard and edge-case inputs.
  • [ ] PR: Atomic commit with clean git hygiene and passing CI

Looking forward to contributing—please feel free to assign!

ranjeet150 avatar Sep 12 '26 23:09 ranjeet150

@benedict102 has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

I would like to work on this issue

ℹ️ Repo Maintainers: To accept this application, review their application or assign @benedict102 to this issue.

benedict102 avatar Sep 27 '26 15:09 benedict102

@Marvellous805 has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

Hi maintainer I can fix this.

ℹ️ Repo Maintainers: To accept this application, review their application or assign @Marvellous805 to this issue.

Marvellous805 avatar Sep 27 '26 15:09 Marvellous805

@aduagba4133 has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

I am a detail-oriented and reliable individual with strong skills in research, data entry, online tasks, and following instructions. I am committed to delivering accurate and high-quality work within deadlines and am available to start tasks immediately.

ℹ️ Repo Maintainers: To accept this application, review their application or assign @aduagba4133 to this issue.

aduagba4133 avatar Sep 27 '26 15:09 aduagba4133

Congratulations, @Marvellous805! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on September 30, 2026.

🧑‍💻 @Marvellous805: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

[!WARNING] When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

🌊 Happy Wave 🌊

drips-wave[bot] avatar Sep 27 '26 15:09 drips-wave[bot]

🎉 This issue has been marked as completed on GrantFox!

@Marvellous805's PR #1558 was approved and merged by @K1NGD4VID.

🏆 @Marvellous805: You earned 35 FoxPoints for this contribution! Your current tier: Explorer (175 total points). Track your full progress on GrantFox.

👏 Great work, @Marvellous805! Keep contributing to LabsCrypt.

grantfox-oss[bot] avatar Oct 08 '26 06:10 grantfox-oss[bot]

This issue has been marked as completed by a Drips Wave moderator for @Marvellous805 as part of the Stellar Wave Program's 9th Wave 🥳

Moderator's reason: [Auto-assessed] The contributor's PR #1558 was successfully merged by maintainer @K1NGD4VID on 2026-10-08. The changes correctly implement Prisma read replicas, connection pooling configuration updates, and PgBouncer integration as requested in issue #1325.

😎 @Marvellous805: You earned 150 Points for completing this issue! Once the Wave's rewards are finalized, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here.

🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

ℹ️ Note: This issue was marked complete via moderation. Points were issued even though the GitHub issue remains open.

drips-wave[bot] avatar Oct 13 '26 00:10 drips-wave[bot]