Best practice for running cron jobs with multiple replicas

Hi,

I'm running a service on Railway with 4 replicas in production, and I've run into an issue with my cron jobs.

Since the cron logic lives inside the service itself, every replica fires the same cron job at the same time. This causes two problems:

Duplicated execution — the same job runs 4 times in parallel instead of once.

Race conditions — for example, one job uploads and processes CSV files and then deletes the temp file. Because all replicas run simultaneously, one replica deletes the temp file while another replica tries to access it and fails with a "file not found" error.

I'm aware of a couple of common approaches to solve this:

Distributed lock (e.g. Redis) — the first replica to pick up the job acquires a lock so the others skip that run. This is the option I'm leaning toward.

A dedicated service just for cron jobs — but that feels like unnecessary duplication for our setup.

My questions:

Is there a Railway-native way to designate a single replica as the "leader"/master (or otherwise guarantee a scheduled job runs only once across replicas), instead of me implementing a Redis lock myself?

If not, is the Redis distributed-lock approach the recommended pattern on Railway, or is there a better practice you'd suggest for this scenario?

Thanks!

Solved$20 Bounty

1 Replies

Railway
BOT

a month ago

This thread has been opened as a bounty so the community can help solve it.

Status changed to Open Railway about 1 month ago


IIRC there isn't a way to mark a replica as "leader." You'd need some kind of data source (eg, Redis) that all services can access simultaneously without R/W issues.


Status changed to Solved adriancoll-stowlog about 1 month ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...