# Server wide variable?

**URL:** <https://forum.heroiclabs.com/t/server-wide-variable/4982>\
**Category:** Runtime Framework\
**Created:** [February 21, 2024, 11:37am UTC](https://forum.heroiclabs.com/t/server-wide-variable/4982 "2024-02-21T11:37:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaggred](https://avatars.discourse-cdn.com/v4/letter/j/ccd318/32.png) [@jaggred](https://forum.heroiclabs.com/u/jaggred)\
**Post date:** [February 21, 2024, 11:37am UTC](https://forum.heroiclabs.com/t/server-wide-variable/4982/1 "2024-02-21T11:37:30Z")

</div>

Hello  
Is it possible to set server-wide variables?  
For example, in my init function, I would like to set a variable  
`StillBooting = true`  
and check this variable in all onBeforeAuth functions and don’t allow users to log in if this variable is not set to false in another part of the code.

---

<div class="post-metadata">

**Author:** ![mofirouz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mofirouz/32/1523_2.png) [@mofirouz](https://forum.heroiclabs.com/u/mofirouz)\
**Post date:** [February 21, 2024, 12:57pm UTC](https://forum.heroiclabs.com/t/server-wide-variable/4982/2 "2024-02-21T12:57:08Z")

</div>

We strongly suggest that you don’t use a global variable. What are you trying to achieve? A maintenance blocker? If so, why?

---

<div class="post-metadata">

**Author:** ![jaggred](https://avatars.discourse-cdn.com/v4/letter/j/ccd318/32.png) [@jaggred](https://forum.heroiclabs.com/u/jaggred)\
**Post date:** [February 21, 2024, 1:45pm UTC](https://forum.heroiclabs.com/t/server-wide-variable/4982/3 "2024-02-21T13:45:29Z")

</div>

Whenever our instance restarts, we store all active matches in the database, to restore them later. After every restart, we experience about 15 minutes of heavy load to the database, so users who try to log in gets timeout on the database query.

We need to investigate this further, but the idea is, to block connections before all matches are restored successfully.

---

<div class="post-metadata">

**Author:** ![mofirouz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mofirouz/32/1523_2.png) [@mofirouz](https://forum.heroiclabs.com/u/mofirouz)\
**Post date:** [February 21, 2024, 2:55pm UTC](https://forum.heroiclabs.com/t/server-wide-variable/4982/4 "2024-02-21T14:55:30Z")

</div>

You should be doing match migration during reboots, and we strongly recommend against saving matches to the database.

> **[Authoritative Multiplayer](https://heroiclabs.com/docs/nakama/concepts/multiplayer/authoritative/#match-migration)**
>
> Authoritative multiplayer is suited for gameplay which depends on central state managed by the game server. The Nakama authoritative multiplayer engine introduces a way to run custom match logic with a fixed tick rate. Messages can be validated and...

---

<div class="post-metadata">

**Author:** ![jaggred](https://avatars.discourse-cdn.com/v4/letter/j/ccd318/32.png) [@jaggred](https://forum.heroiclabs.com/u/jaggred)\
**Post date:** [February 21, 2024, 3:41pm UTC](https://forum.heroiclabs.com/t/server-wide-variable/4982/5 "2024-02-21T15:41:50Z")

</div>

We are running a lot of matches, and our nodes are using more than 50% of their RAM.  
Migrating all matches to one node will cause an OutOfMemmory exception with all matches disappearing (been there, done that 🙂 )  
Also storing matches in a database feels a lot safer than migrating them on flight to another node.

Any solution on how to make matches backups to be safe?  
We don’t have a lot of client messages, (ie one client message in 1 min) so maybe save matches every client message into the database?

---

<div class="post-metadata">

**Author:** ![Eatos](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/eatos/32/1656_2.png) [@Eatos](https://forum.heroiclabs.com/u/Eatos)\
**Post date:** [February 21, 2024, 4:19pm UTC](https://forum.heroiclabs.com/t/server-wide-variable/4982/6 "2024-02-21T16:19:53Z")

</div>

@jaggred You could abuse localcache and use that in matter of checking is game services still boothing.  
in Lua you have nk.localcache\_get , nk.localcache\_put, nk.localcache\_delete, nk.localcache\_clear  
[https://github.com/heroiclabs/nakama/blob/master/server/runtime\_lua\_nakama.go](https://github.com/heroiclabs/nakama/blob/master/server/runtime_lua_nakama.go)  
So basically in the main you would set a variable within localcache to be false, and once you call your method for creating all of the matches, i assume a function call is performed, once that function finishes set value of that variable to be true.

Maybe not the best case for using localcache but could work in your case.

---

<div class="post-metadata">

**Author:** ![mofirouz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mofirouz/32/1523_2.png) [@mofirouz](https://forum.heroiclabs.com/u/mofirouz)\
**Post date:** [February 21, 2024, 4:33pm UTC](https://forum.heroiclabs.com/t/server-wide-variable/4982/7 "2024-02-21T16:33:39Z")

</div>

> [@jaggred](#):
>
> Migrating all matches to one node will cause an OutOfMemmory exception with all matches disappearing (been there, done that 🙂 )

Match migration automatically loadbalances all the matches across the all the nodes in the cluster, so not sure about this comment.

---

<div class="post-metadata">

**Author:** ![dela](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/dela/32/1489_2.png) [@dela](https://forum.heroiclabs.com/u/dela)\
**Post date:** [February 27, 2024, 9:17am UTC](https://forum.heroiclabs.com/t/server-wide-variable/4982/8 "2024-02-27T09:17:48Z")

</div>

i use a backup/restore mechanism with the TypeScript Runtime. I store the whole matchState in nakamas storageengine (about every 10 seconds) and on bootup i restore all matches for that instance. In my case, matchState is about 500kb (json) and restoring hundreds of those matches gets done in less then a second.
