---
title: "Deploy MongoDB Replica Set"
description: "Instant MongoDB replica set for high availability and redundancy."
category: "Storage"
url: https://railway.com/deploy/ha-mongo
---

# Deploy MongoDB Replica Set

Instant MongoDB replica set for high availability and redundancy.

**[Deploy MongoDB Replica Set on Railway](https://railway.com/template/ha-mongo)**

- **Creator:** Railway Templates
- **Category:** Storage
- **Total deploys:** 231

## Template content

### mongo1 https://devicons.railway.app/mongo

- **Source:** railwayapp-templates/mongo-replica-set

### mongo3 https://devicons.railway.app/mongo

- **Source:** railwayapp-templates/mongo-replica-set

### mongo2 https://devicons.railway.app/mongo

- **Source:** railwayapp-templates/mongo-replica-set

### Init Replica Set

- **Source:** railwayapp-templates/mongo-replica-set

## Documentation

This template deploys a MongoDB replica set for a more highly available and redundant data layer.

### Inter-node communication

All communication between nodes is done over the [private network](https://docs.railway.app/reference/private-networking).

### Configuration tuning

To tune the configuration of the MongoDB nodes, refer to the [MongoDB documentation](https://www.mongodb.com/docs/manual/replication/) or the [documentation page in Docker Hub](https://hub.docker.com/_/mongo).

### Authentication

The MongoDB replica set is deployed using [keyfile authentication](https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set-with-keyfile-access-control/).

### Connecting to the replica set

To connect to the replica set from another service in Railway, you should create a URI in the service's environment variables that includes all nodes.  As an example:

```
MONGO_HOSTS=${{mongo1.RAILWAY_PRIVATE_DOMAIN}}:27017,${{mongo2.RAILWAY_PRIVATE_DOMAIN}}:27017,${{mongo3.RAILWAY_PRIVATE_DOMAIN}}:27017
REPLICA_SET_NAME=${{mongo1.REPLICA_SET_NAME}}
MONGO_URI=mongodb://${{mongo1.MONGO_INITDB_ROOT_USERNAME}}:${{mongo1.MONGO_INITDB_ROOT_PASSWORD}}@${{MONGO_HOSTS}}/?replicaSet=${{REPLICA_SET_NAME}}
```

With these variables set, you can then reference the `MONGO_URI` environment variable when configuring the client in your service code.  You can find some examples [here](https://github.com/railwayapp-templates/mongo-replica-set/tree/main/exampleApps).

*NOTE: If you need to connect to the set from your local machine, or from a service outside of the Railway project in which it is deployed, you will need to setup a tunnel into the Railway private network.  Check out this [tutorial](https://docs.railway.app/tutorials/set-up-a-tailscale-subnet-router) for information on how to use Tailscale as a tunnel.*

### Template source

The source for this template can be found in the [Railway Templates GitHub](https://github.com/railwayapp-templates/mongo-replica-set/tree/main).

The MongoDB nodes are built almost directly from the [official MongoDB image in Docker Hub](https://hub.docker.com/_/mongo), with the only customization being the setup of the Keyfile.

### Resources
- [MongoDB documentation](https://www.mongodb.com/docs/manual/replication/)
- [Example apps](https://github.com/railwayapp-templates/mongo-replica-set/tree/main/exampleApps) for reference on setting up a client to connect to the replica set
- Tutorial: [Deploy and Monitor a Mongo Replica Set](https://docs.railway.app/tutorials/deploy-and-monitor-mongo)

## Similar templates

- [Garage S3 Storage](https://railway.com/deploy/garage-s3-storage) — Ultra-light S3 server: fast, open-source, plug-and-play.
- [Redis](https://railway.com/deploy/redis-1) — Self Host Latest Redis with Railway
- [EasyImg](https://railway.com/deploy/easyimg) — Simple self-hostable Nuxt.js personal image hosting system.

Open this page in a browser: https://railway.com/deploy/ha-mongo
