Skip to content

Convert MongoDB from standalone to replica set#4184

Closed
Copilot wants to merge 25 commits intomasterfrom
copilot/convert-standalone-to-replica-set
Closed

Convert MongoDB from standalone to replica set#4184
Copilot wants to merge 25 commits intomasterfrom
copilot/convert-standalone-to-replica-set

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 26, 2026

MongoDB standalone mode does not support multi-document transactions, which are required for atomic merge operations across FrontierCollection and MergeCollection. This converts to a single-member replica set (rs0) in both development and Docker/Kubernetes environments.

Development (npm start)

  • scripts/startDatabase.js (new): Replaces predatabase+database npm scripts and the now-removed setupMongo.js. Spawns mongod --replSet rs0, polls until ready, calls rs.initiate(), then stays alive until mongod exits. Failures are fatal: SIGINT/SIGTERM handlers are registered early, startup/init failures call process.exit(1), and mongod's exit code is propagated via process.exit(exitCode ?? 1).
  • scripts/setupMongo.js (removed): No longer needed; its directory-creation logic is handled by startDatabase.js.
  • package.json: database script → node scripts/startDatabase.js; predatabase removed.
  • .gitignore: Replaced setupMongo.js exception with startDatabase.js exception.

Docker / Kubernetes

  • database/init/00-replica-set.js (new): Placed in /docker-entrypoint-initdb.d/, runs on first container startup. Calls rs.initiate() with a configurable advertised hostname via MONGO_INITDB_REPLICA_HOST (defaults to localhost:27017). Waits up to 30s for PRIMARY election before allowing subsequent init scripts to run.
  • database.yaml (Kubernetes Helm): Adds args: ["--replSet", "rs0"] and sets MONGO_INITDB_REPLICA_HOST to the pod IP so the replica set member hostname is tracked per pod. The postStart lifecycle hook initializes the replica set on first start and reconfigures the member host (rs.reconfig) when the pod IP changes after a restart, keeping the replica set config in sync.

Configuration

  • appsettings.json: Connection strings now explicitly include ?replicaSet=rs0.
  • .vscode/tasks.json: run-mongo task updated to invoke npm run database (which runs scripts/startDatabase.js) instead of calling mongod directly, ensuring replica set initialization is performed automatically on first run.
  • README.md: MongoDB Compass connection string updated to mongodb://localhost:27017/?replicaSet=rs0.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.


This change is Reviewable

Summary by CodeRabbit

  • New Features

    • Enabled MongoDB replica set (rs0) support for both local development and Kubernetes deployments.
    • Improved database initialization with automatic replica set configuration and validation.
  • Documentation

    • Updated MongoDB connection instructions and database maintenance commands with replica set configuration.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend dependencies Pull requests that update a dependency file deployment docker documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants