Skip to content
Mongo HA deployment via IaC
codan84
OP

4 hours ago

Is there a way to deploy Railway Mongo HA via IaC (railway/iac)? Right now in my dev environment I have mongo single-node replicaset running:

import { image, service, volume } from 'railway/iac';

const data = volume('mongo-data-v2', { region: 'europe-west4-drams3a', sizeMB: 2048 });
service('mongo', {
    source: image(
      'mongo:8.0@sha256:6657bf2413fefc5be649390318f5b4cd7328e982722f8ce2ac6dfa6f0a42cb03',
    ),
    start: `bash -c ${shellQuote(startup)}`,
    tcp: context.environment === 'dev' ? [27017] : [],
    replicas: { 'europe-west4-drams3a': 1 },
    volumeMounts: { '/data/db': data },
    deploy: {
      limitOverride: { containers: { cpu: 0.5, memoryBytes: 1024 * 1024 * 1024 } },
    },
....

Obviously this wouldn't do for a production environment, but I struggle to see anything HA-related in the sdk...

Awaiting User Response

3 Replies

Railway
BOT

4 hours ago

MongoDB High Availability is set up by converting an existing MongoDB service from its Database → Config → High Availability section. That turns it into a replica set (your original node plus 2, 4 or 6 replicas) behind HAProxy, with automatic failover. The MongoDB HA guide covers the steps.

The IaC reference provides a managed mongo("mongo") database helper, and database provisioning is handled by our product workflows rather than by the volume and replica settings on a plain service(). We have not confirmed whether HA can be declared through railway/iac, so the dashboard conversion is the path we can point you to for now.

Your current definition would not qualify for conversion as written. Conversion requires the official mongo image with a versioned major tag (7 or 8, which mongo:8.0 meets) and no custom start command, because the cluster image manages its own startup. Your bash -c start command would need to be removed. A single-node replica set you initiated yourself is also yours to manage, not the managed HA cluster.


Status changed to Awaiting User Response Railway • about 4 hours ago


codan84
OP

4 hours ago

Will re-applying IaC not remove the HA setup done via the dashboard?


Status changed to Awaiting Railway Response Railway • about 4 hours ago


Railway
BOT

4 hours ago

We have not verified how railway/iac treats a MongoDB service after it has been converted to HA in the dashboard, so we can't confirm whether re-applying your current file would leave the cluster intact.

What is established is that railway config apply always runs a plan first and asks you to confirm the exact changes before anything is applied. Destructive changes such as deleting a service are marked, and non-interactive runs additionally require --confirm-destructive. An apply is also rejected if the environment changed after the plan, for example through a dashboard edit (IaC docs).

Since your file still declares the mongo:8.0 image, a custom start command and a single replica, run railway config plan against that environment after converting and review it before applying anything that would change or remove the cluster's services.


Status changed to Awaiting User Response Railway • about 4 hours ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...