Deploy π Redis HA Cluster (Sentinel)
π High-Availability Redis featuring Master-Replica with Redis Sentinel
redis-sentinel
Just deployed
redis-replica
Just deployed
redis-master
Just deployed
Deploy and Host Redis HA Cluster on Railway
Redis HA Cluster is a production-ready, high-availability Redis deployment featuring Master-Replica replication with Redis Sentinel for automatic failover. This template provides enterprise-grade caching, session storage, and real-time messaging with zero-downtime failover capabilities.
About Hosting Redis HA Cluster
Deploying this Redis cluster on Railway provides an instant production environment with automatic failover. The template includes three nodes: a Master for read/write operations, a Replica for read scaling and data redundancy, and a Sentinel that monitors the cluster and automatically promotes the replica if the master fails. All nodes feature auto-tuning based on Railway resources, ensuring optimal memory usage without manual configuration. Simply set your password and deployβthe cluster handles all internal communication automatically.
π Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Railway Project β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββ βββββββββββββββββββ β
β β redis-master β β redis-replica β β
β β β βββββββΊ β β β
β β (Read/Write) β Async β (Read-Only) β β
β β Port: 6379 β Sync β Port: 6379 β β
β ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ β
β β β β
β βββββββββββββ¬ββββββββββββββββ β
β β β
β ββββββββββΌβββββββββ β
β β redis-sentinel β β
β β β β
β β β’ Monitoring β β
β β β’ Auto-failoverβ β
β β Port: 26379 β β
β βββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Common Use Cases
- Session Storage: Store user sessions with automatic expiration and high availability.
- Application Cache: Speed up database queries with sub-millisecond response times.
- Real-time Messaging: Build pub/sub systems, chat applications, and live notifications.
- Rate Limiting: Implement API rate limits and request throttling.
- Queue Management: Use Redis Lists for job queues and task processing.
- Leaderboards: Build real-time gaming leaderboards with sorted sets.
Dependencies for Redis HA Cluster Hosting
- Redis 7.4: Latest stable Redis with enhanced performance and security.
- Redis Sentinel: Automatic failover and cluster monitoring.
- Alpine Linux: Lightweight container base for minimal resource usage.
Deployment Dependencies
Implementation Details
To get started, configure the REDIS_PASSWORD environment variable. Once deployed, connect through the master node for write operations or replica for read scaling.
Environment Variables
redis-master:
REDIS_PASSWORD=your_secure_password
redis-replica:
REDIS_PASSWORD=${redis-master.REDIS_PASSWORD}
MASTER_HOST=${redis-master.RAILWAY_PRIVATE_DOMAIN}
redis-sentinel:
REDIS_PASSWORD=${redis-master.REDIS_PASSWORD}
MASTER_HOST=${redis-master.RAILWAY_PRIVATE_DOMAIN}
π°οΈ Internal / Private Connection (Recommended)
Use this for applications running within the same Railway project.
Master (Read/Write):
- Host:
redis-master.railway.internal - Port:
6379 - Password: Your
${REDIS_PASSWORD}
Replica (Read-Only):
- Host:
redis-replica.railway.internal - Port:
6379 - Password: Your
${REDIS_PASSWORD}
Sentinel:
- Host:
redis-sentinel.railway.internal - Port:
26379
Connection URL:
redis://:${REDIS_PASSWORD}@redis-master.railway.internal:6379
π External / Public Connection
For local development or Redis management tools (RedisInsight, Another Redis Desktop Manager).
- Navigate to the redis-master service in Railway.
- Go to Settings > Public Networking > TCP Proxy.
- Use the generated Public Domain and Port.
Features
| Feature | Description |
|---|---|
| Auto-Failover | Sentinel promotes replica if master fails |
| Auto-Tune Memory | Automatically uses 50% of available RAM |
| Persistence | RDB snapshots + AOF for data durability |
| Replication | Async replication with configurable lag |
| Security | Password authentication enabled by default |
π» Connection Examples
Node.js (ioredis)
Direct Connection:
const Redis = require("ioredis");
// Connect to Master (Read/Write)
const master = new Redis({
host: "redis-master.railway.internal",
port: 6379,
password: process.env.REDIS_PASSWORD,
});
// Connect to Replica (Read-Only)
const replica = new Redis({
host: "redis-replica.railway.internal",
port: 6379,
password: process.env.REDIS_PASSWORD,
});
Sentinel Connection (Recommended for HA):
const Redis = require("ioredis");
const redis = new Redis({
sentinels: [{ host: "redis-sentinel.railway.internal", port: 26379 }],
name: "mymaster",
password: process.env.REDIS_PASSWORD,
sentinelPassword: process.env.REDIS_PASSWORD,
// Auto-reconnect on failover
retryDelayOnFailover: 100,
enableReadyCheck: true,
});
redis.on("connect", () => console.log("Connected to Redis via Sentinel"));
redis.on("error", (err) => console.error("Redis error:", err));
Go (go-redis)
Direct Connection:
package main
import (
"context"
"os"
"github.com/redis/go-redis/v9"
)
func main() {
ctx := context.Background()
// Connect to Master (Read/Write)
master := redis.NewClient(&redis.Options{
Addr: "redis-master.railway.internal:6379",
Password: os.Getenv("REDIS_PASSWORD"),
DB: 0,
})
// Connect to Replica (Read-Only)
replica := redis.NewClient(&redis.Options{
Addr: "redis-replica.railway.internal:6379",
Password: os.Getenv("REDIS_PASSWORD"),
DB: 0,
})
// Test connection
_, err := master.Ping(ctx).Result()
if err != nil {
panic(err)
}
}
Sentinel Connection (Recommended for HA):
package main
import (
"context"
"fmt"
"os"
"github.com/redis/go-redis/v9"
)
func main() {
ctx := context.Background()
// Sentinel client with automatic failover
rdb := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{"redis-sentinel.railway.internal:26379"},
Password: os.Getenv("REDIS_PASSWORD"),
SentinelPassword: os.Getenv("REDIS_PASSWORD"),
DB: 0,
})
defer rdb.Close()
// Test connection
pong, err := rdb.Ping(ctx).Result()
if err != nil {
panic(err)
}
fmt.Println("Connected:", pong)
// Usage
err = rdb.Set(ctx, "key", "value", 0).Err()
if err != nil {
panic(err)
}
val, err := rdb.Get(ctx, "key").Result()
if err != nil {
panic(err)
}
fmt.Println("key:", val)
}
π Why Use Sentinel Connection?
| Feature | Direct Connection | Sentinel Connection |
|---|---|---|
| Auto-Failover | β Manual reconnect needed | β Automatic |
| Service Discovery | β Hardcoded host | β Dynamic |
| High Availability | β οΈ Single point of failure | β Full HA |
| Complexity | Simple | Slightly more setup |
Recommendation: Use Sentinel Connection for production workloads to ensure your application automatically handles failover scenarios.
Why Deploy Redis HA Cluster on Railway?
Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.
By deploying Redis HA Cluster on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.
Support This Project
If this template saves you time and money, consider supporting its development! β
Template Content
redis-sentinel
icueth/railway-template-redis-ha-clusterredis-replica
icueth/railway-template-redis-ha-clusterredis-master
icueth/railway-template-redis-ha-cluster