MongoDB replica set for Prisma on Railway
Short answer: Prisma’s MongoDB connector needs MongoDB to run as a replica set, because it relies on transactions, and a standalone mongod rejects them. On Railway you can get one with a template that runs MongoDB 8.0 as a single-node replica set (rs0) and sets up the root user, keyFile and replica set on first boot. It also runs a daily backup job to a Railway Bucket. It has one member, so there is no failover.
Why Prisma needs a replica set
A plain MongoDB container starts as a standalone server. A standalone mongod does not support multi-document transactions or change streams, so Prisma’s MongoDB connector, Mongoose sessions and watch() fail against it. A replica set enables both, and a replica set with a single member is enough for that purpose.
The same applies to anything in your app that needs transactions (orders, payments, inventory) or change streams (notifications, cache invalidation, sync).
Which MongoDB templates people deploy on Railway
| Template | Creator | Deployments | Services |
|---|---|---|---|
| MongoDB | Railway Templates | 40,707 | 1 |
| ExpressJS Mongoose | opqr's Projects | 1,169 | 2 |
| LibreChat Private AI Chat (MongoDB + Meilisearch) | Tracklayer | 8 | 5 |
| MongoDB 8 Replica Set (Prisma-Ready) with Backups | Tracklayer | 1 | 2 |
Railway marketplace data as of 2026-11-19. Full ranking: Best MongoDB templates on Railway.
Template names do not always say whether the database runs as a replica set, which version it pins or whether it has backups. Open the template page and read its services and description before you pick one.
Deploy MongoDB as a replica set, step by step
These steps use Tracklayer’s MongoDB 8 Replica Set (Prisma-Ready) with Backups template, which pins MongoDB Community Server 8.0.34 (the official mongo image).
- Deploy the template. It creates a MongoDB service with a volume at
/data/db, a MongoDB Backup cron job and a Railway Bucket. - Wait for the ready line. Open the MongoDB logs and look for
[mongodb-rs-init] replica set rs0 ready: PRIMARY .... There is no healthcheck, because Railway healthchecks are HTTP-only. - Find the credentials. They are generated. Open the MongoDB service, then Variables. The user is
mongo, matching Railway’s own MongoDB template, and the variable names (MONGOHOST,MONGOPORT,MONGOUSER,MONGOPASSWORD,MONGO_URL) are the same. - Check the replica set. From your machine, run
mongoshwithMONGO_PUBLIC_URL, thenrs.status().members[0].stateStr. It should returnPRIMARY. - Connect your app. In your app service, add a variable that references the database:
- Prisma:
DATABASE_URL=${{MongoDB.DATABASE_URL}}. It is the same URL with the databaserailwayin the path. - Drivers and Mongoose:
MONGO_URL=${{MongoDB.MONGO_URL}}, which ends in/?replicaSet=rs0&authSource=admin.
- Prisma:
- Point Prisma at it. Your schema reads the URL from the environment:
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
To run prisma db push from your machine, use DATABASE_PUBLIC_URL. It goes through Railway’s TCP proxy. Proxy traffic is not encrypted and is billed as egress; delete the TCP proxy under Settings, Networking if you want the database private.
- Run the first backup. Run
railway cron run --service "MongoDB Backup"or wait for 03:15 UTC, then check the Bucket’s Files tab.
Data, backups and upgrades
- Persistence. Data lives on the volume at
/data/db, so it survives redeploys. - Backups. The backup job runs daily at 03:15 UTC. It streams
mongodump --archive --gzip --oploginto the Bucket, verifies the archive and keeps 14 days (at least 3 runs are always kept). - Upgrades. The image is pinned by tag and digest in the template’s two Dockerfiles. For a patch update, change both. For a major upgrade, take a backup first, change both images, redeploy, then raise the feature compatibility version.
- Password.
MONGO_INITDB_ROOT_PASSWORDonly takes effect on the first boot. To rotate it, change the user’s password in MongoDB first, then update the variable.
What it costs
Railway bills usage. The template’s own figures are estimates, not measurements, based on Railway’s published prices: about $2.50 per month idle and about $14.50 per month for a typical small app with 1 GB of RAM, a 5 GB volume and daily backups. A busier setup (4 GB RAM, 1 vCPU, 30 GB volume) is estimated at about $70. RAM and volume size drive most of it.
Limits and licence
- One member means no failover. Scale vertically; Railway service replicas cannot share a volume, so keep one replica.
- Prisma support depends on your Prisma version (see the FAQ).
- MongoDB Community Server is under the Server Side Public License v1. See the FAQ and get legal advice if you resell database access.
- This is a community template, not affiliated with MongoDB, Inc. or Prisma.
Other options
For a relational alternative, Tracklayer also maintains Postgres 18 with pgvector. For other databases, see MongoDB templates on Railway and Postgres templates on Railway.
FAQ
Does Prisma 7 work with MongoDB?
According to the template README, which cites Prisma's docs as checked on 2026-10-31, MongoDB is not yet supported in Prisma ORM 7 and is early access in Prisma ORM 8 (it needs MongoDB 8.0 or newer). Prisma ORM 6.x is the line the README describes as working with a replica set. Check Prisma's current docs before you upgrade.
Does this setup have failover?
No. It is one replica-set member, so there is no failover. If you need automatic failover, the template README points to Railway's own Mongo HA template, which runs three members. You scale this one vertically by raising RAM, CPU and volume size, and you keep a single replica.
What licence does MongoDB use?
MongoDB Community Server is under the Server Side Public License v1 (SSPL). Using it as your app's database is common, but offering MongoDB itself as a service to third parties has conditions. This is not legal advice; ask a lawyer if your product resells database access.
How do I restore a backup?
Download mongodb.archive.gz from the Bucket and run mongorestore with --gzip, --drop and exclusions for admin.system.users and admin.system.roles. Alternatively set BACKUP_RESTORE_RUN to a run folder on the MongoDB Backup service and trigger the job, then clear the variable.
Why can't I connect from my laptop with the private URL?
The replica-set member is registered under the private hostname mongodb.railway.internal, which only resolves inside Railway. From your machine use MONGO_PUBLIC_URL or DATABASE_PUBLIC_URL, which go through the TCP proxy with directConnection=true. Do not add replicaSet= to those URLs.