Horizontal Scaling with Redis
Mana uses a PubSub-based message backbone to synchronize rooms across multiple server nodes. This allows your application to scale from a single core to a globally distributed cluster with millions of concurrent users.
1. Choose Your Backend
By default, Mana uses a high-performance In-Memory Hub for a single server. To scale, you can swap to:
- Redis: The industry standard for distributed PubSub.
- NATS: For high-throughput, low-latency clustering.
2. The Shared Hub
Mana synchronizes room states (who is connected, what messages were sent) across all nodes. When a message is sent to Node A, Mana automatically broadcasts it via Redis so users on Node B receive it instantly.
3. Load Balancing
With clustering enabled, you can place a standard load balancer (like Nginx or AWS ALB) in front of your Mana nodes. Sticky sessions are not required because Mana's architecture is stateless from the user's perspective.
main.go
package main
import (
"log"
"github.com/Aswanidev-vs/mana"
)
func main() {
cfg := mana.DefaultConfig()
// 1. Enable Redis PubSub for scaling.
cfg.PubSubBackend = "redis"
cfg.RedisAddr = "localhost:6379"
// 2. Assign unique Node ID for monitoring.
cfg.ClusterNodeID = "node-alpha"
app := mana.New(cfg)
// 3. Start the engine
log.Printf("Clustered node %s starting...", cfg.ClusterNodeID)
log.Fatal(app.Start())
}