Delay when a match starts caused by a large dictionary file

In my game I use a dictionary to validate words sent to the client. The dictionary is ~2MB and loaded at runtime in the match init. This leads to a delay of a few seconds when a match is started. I use Lua.

To reduce this delay I’ve tried:

  • Using a Lua module. This doesn’t work because Nakama doesn’t support global variables, which means the dictionary has to be loaded each time the module is required, leading to the delay (and CPU load).
  • Passing the dictionary as a parameter to the module. This works, but the delay is about the same.

I’m considering:

  • Using a server to server HTTP request from the match to the main module which checks the word. This way the dictionary would only have to be loaded once, possibly with nk.run_once().
  • Saving the dictionary to a Storage object and reading it on match init. Still likely to cause a delay.
  • Using a custom database and access it when a word needs checking. I mention this last, because I’d rather stick to the services Nakama offers, to keep the code base simple.

What is a good approach to reduce this delay as a match starts?

@totebo It sounds to me that your data might not be properly optimized, but anyway, have you tried to use
nk.localcache ?
While server is starting up, you could preload/cache all of those json-s into RAM memory, which is visible from anywhere “acts” like a global. Then in match you would read it when you need it.
I am handling like this data in our game and servers and haven’t noticed hick-ups and delays although our json files the largest is 250kb but highly compressed.

1 Like

Thanks for the reply!

I had not heard of nk.localcache before, this sounds very useful! I can’t seem to find it in the documentation for some reason, do you know where I could read up on this?

Atm, it is not exposed in documentation as far as i know, as that api is still under well work, it is not stable, but at least on xy version of nakama if you stay, it is stable for you :smiley:

as for commands which you got you have:

“localcache_get”: n.localcacheGet,
“localcache_put”: n.localcachePut,
“localcache_delete”: n.localcacheDelete,
“localcache_clear”: n.localcacheClear,

And Lua example:

1 Like

I got around to test localcache, and it works great. The entire dictionary is now cached and accessed near instantly, whereas before it took several seconds for each match start.

Thanks again for this!