How to use caching db (redis for example) to optimize performance of my battle server

I am very new for websocket battle server. I just wonder to know the usage of caching db (redis for example) . From documents and examples on colyseus website, I have read that the battle data and battle state may be stored in the server memory by room.state, and patched to the client every fps by colyseus mechanism. Can I store them to caching db and still patch them by the colysues mechanism ? And Is it necessary? Thank you so much for teaching!

Hi @zgz682000,

What would be the advantage of using a database (redis) instead of Node.js's memory? What's your use-case?

If you're looking to persist the room's state upon room's disposal, and re-loading it when it's spawned again, then it's a good idea! You can cache the state on onDispose() method, and re-load it on onInit().

Cheers

Thank you so much for replying. I have no use-case, I am just considering this for curiosity. As I know, first of all, the amount of Node.js usable memory is very limited, so storing room state into database would help one server hosting more rooms; secondly, network service programming prefers maintaining no state data in memory, for the risk of losing when service down. Did I understand them correctly? Thank you so much for teaching!

@zgz682000 I understand. Most tutorials and material out there about scalability will tell you about stateless applications. The thing is that games are stateful, IMO.

What Colyseus does at the moment for scalability is using Redis to map connections to messages going back and forth to the correct room instance. Regarding losing data, you usually would persist relevant data during the lifecycle of the room instances. They're meant to be ephemeral, spawned and destroyed all the time, like a short-lived session.

Hope this helps! Cheers!

games are stateful. It's very useful conception. Thank you again!