MasterServerService | v1.0.0 Master Server Election

MasterServerService v1.0.0 - Master Server Election for Roblox

A Roblox module for master server coordination across multiple game servers.
Features automatic master election, state persistence, and cross-server broadcasting

link: GitHub - V1nyI/roblox-master-server-service
https://create.roblox.com/store/asset/89450003406777/MasterServerService


Features

  • Automatic Master Election - No manual intervention required
  • Fast Failover - Detects master crashes and elects a new one within seconds
  • Persistent State (PassedData) - New master inherits the last known state
  • Cross-Server Broadcasting - Publish and subscribe to topics across all servers
  • Graceful Shutdown Handoff - Transfers master role and data before shutdown
  • Optional Audit Logging - Record master changes, passed data updates, and more
  • Configurable Parameters - Heartbeat intervals, timeouts, and max server limits

Installation

  1. Place MasterServerService.lua in ServerScriptService.
  2. Require it in a server script (Call Initialize() as early as possible in your server’s main script):
local MasterServerService = require(ServerScriptService.MasterServerService)
MasterServerService.Initialize()

Basic Usage

Master Election Callback

MasterServerService.OnBecameMaster(function(passedData)
    print("This server is now the master. Reason:", passedData.reason)
end)

Non-Master Callback

MasterServerService.OnBecameServer(function(info)
  print("This server is now a regular server. Reason:", info.reason)
end)

Cross-Server Messaging

-- Publish an event to all servers
MasterServerService.Publish("MyEvent", {foo = "bar"})

-- Subscribe to the topic
MasterServerService.Subscribe("MyEvent", function(from, payload, timestamp)
  print("Message from:", from, "Payload:", payload.foo)
end)

Using PassedData

PassedData is only for transferring state between the current master and the next master during a master change

It is not a real-time global data store for all servers, for that, use Publish().

-- Set shared state for the next master server to inherit
MasterServerService.SetPassedData({number = 143})

-- Get shared state (only meaningful if this server is or becomes master)
local data = MasterServerService.PassedData.GetAsync("data")
if data then
    print("Current number:", data.number)
end

API Overview

  • Initialize() - Starts the service and participates in master election
  • IsMaster() - Returns true if this server is the master
  • GetMasterId() - Returns the current master server ID
  • GetServerList() - Returns a list of active server IDs
  • Publish(topic, payload) - Sends a message to all servers
  • Subscribe(topic, callback) - Listens for a topic across all servers
  • SetPassedData(data) - Sets shared state for the next master server
  • PassedData.GetAsync(key) - Gets a specific field from the shared state
  • PassedData.GetAll() - Returns the full shared state table
  • OnBecameMaster(fn) - Sets a callback when this server becomes master
  • OnBecameServer(fn) - Sets a callback when this server loses master status
  • OnServerShutdown(fn) - Sets a callback before this server shuts down
  • Status() - Returns {alive, isMaster, masterId, listCount}

License

MIT — see LICENSE for details.

26 Likes

the Module is now available at roblox creator store: https://create.roblox.com/store/asset/89450003406777/MasterServerService

What are some use-cases for this? I’ve never heard of something like this before

Pretty sure this is just for cross-server connections. Anytime you would want to have cross-server capabilities or broadcast states across multiple servers.

2 Likes

this is pretty cool, i was trying to think about how i would achieve something like this recently. great for things that might typically need to be done in an external server like a stock or event tracker. nice job!

Thank you for sharing this resource!
I was just about to implement one today so this came in handy :laughing:

Seems really useful. Thanks for sharing this with everyone!

I wanted to make an external server for some global store stock but this will save me some time!

2 Likes

Very cool, I’ll consider using this for an eventual rewrite of my game.

1 Like

Hello!

check out GlobalStockService, It handles global stock and automatic restocks

1 Like

Hi!

Here are some examples of what it can be used for:

  1. Cross Server Competitions - Players in different servers can compete in the same event with live results

  2. Shared Economy or Resources - Games with global currencies, items, or vaults can track totals across all servers

  3. Global Events & Notifications - The master server can trigger things like “Double XP” or other special events for all servers

  4. Persistent World Changes - For example, running a global stock market or unlocking content that affects every server

2 Likes

You could probably just rent a linux box and do http requests to that and itd be cheaper and easier

  1. Ban Wave System - The master server would keep track of users in a ban wave and wait for the right moment to ban them all.

Roblox servers are free for us, and it’s pretty easy to do knowing that you have this resource also for free.

Let’s say we have an API key. We only ever request from it ~20 times a minute, regardless of how many players are in-game. However, the more players are in-game, the more times we use the API, because each server uses it! That’s not good. So, only the master server calls the API, then messages all the other servers with the new info. That way, you don’t run into issues with the API.

2 Likes