admin管理员组

文章数量:1025218

I'm writing a game in javascript, and to prevent cheating, i'm having the game be played on the server (it's a board game like a more plicated checkers). Since the game is fairly plex, I need to store the gamestate in order to validate client actions.

Is it possible to store the gamestate in memory? Is that smart? Should I do that? If so, how? I don't know how that would work.

I can also store in redis. And that sort of thing is pretty familiar to me and requires no explanation. But if I do store in redis, the problem is that on every single move, the game would need to get the data from redis and interpret and parse that data in order to recreate the gamestate from scratch. But since moves happen very frequently this seems very stupid to me.

What should I do?

I'm writing a game in javascript, and to prevent cheating, i'm having the game be played on the server (it's a board game like a more plicated checkers). Since the game is fairly plex, I need to store the gamestate in order to validate client actions.

Is it possible to store the gamestate in memory? Is that smart? Should I do that? If so, how? I don't know how that would work.

I can also store in redis. And that sort of thing is pretty familiar to me and requires no explanation. But if I do store in redis, the problem is that on every single move, the game would need to get the data from redis and interpret and parse that data in order to recreate the gamestate from scratch. But since moves happen very frequently this seems very stupid to me.

What should I do?

Share Improve this question asked Jan 15, 2011 at 20:04 expressnoobexpressnoob 2,3473 gold badges16 silver badges7 bronze badges 7
  • 1 This doesn't really have anything to do with JavaScript. You essentially just need a server which can deal with all the different connected clients, and since a program executes in memory it will be fast! Only save the game state to a database if you need to "save" the game. Represent your game state in code! – Marcus Whybrow Commented Jan 15, 2011 at 20:07
  • @Marcus thanks, I'm asking for advice on implementation since it's not as obvious to me. How do I represent the game state in code? – expressnoob Commented Jan 15, 2011 at 20:13
  • @expressnoob variables and classes. These are the tools, use them to represent objects in your game, such as Game, Player, Piece etc. The design is down to you and specific to your game. – Marcus Whybrow Commented Jan 15, 2011 at 20:28
  • @Marcus how do I keep them persistent on the server for each individual game? – expressnoob Commented Jan 15, 2011 at 20:39
  • @expressnoob Create a separate Thread for each game the server might be orchestrating and give each an instance of a Game, then if by persistent you meant after the server shuts down, you could serialise all of your objects and save them to file (or database) or translate it into database tables in a more bespoke way. – Marcus Whybrow Commented Jan 15, 2011 at 20:43
 |  Show 2 more ments

1 Answer 1

Reset to default 5

If you really, really don't want the overhead of I/O then just store the game state in a global object keyed by the game id:

var global_gamesate = {}

Then on each connection check what the game id is to retrieve he game state:

var gamestate = global_gamestate[game_id];

Presumably you already have a mechanism to map client sessions to game id.

Usually, game state is small and would hardly take up much RAM. Let's be pessimistic and assume each game state takes up 500K. Then you can serve two million thousand games (four million thousand users if we assume two users per game) for each gigabyte of RAM on your server.


However, I would like to point out that databases like MySQL already implement caching (which is configurable) so loading the most frequently used data basically loads from RAM with a minor socket I/O overhead. The advantages of databases is that you can have much more data than you have RAM because they store the rest on disk.

If your program ever reaches the load where you start thinking of writing your own disk-serialization algorithm to implement a swap file then you're basically re-inventing the wheel. In which case I'd say go with databases.

I'm writing a game in javascript, and to prevent cheating, i'm having the game be played on the server (it's a board game like a more plicated checkers). Since the game is fairly plex, I need to store the gamestate in order to validate client actions.

Is it possible to store the gamestate in memory? Is that smart? Should I do that? If so, how? I don't know how that would work.

I can also store in redis. And that sort of thing is pretty familiar to me and requires no explanation. But if I do store in redis, the problem is that on every single move, the game would need to get the data from redis and interpret and parse that data in order to recreate the gamestate from scratch. But since moves happen very frequently this seems very stupid to me.

What should I do?

I'm writing a game in javascript, and to prevent cheating, i'm having the game be played on the server (it's a board game like a more plicated checkers). Since the game is fairly plex, I need to store the gamestate in order to validate client actions.

Is it possible to store the gamestate in memory? Is that smart? Should I do that? If so, how? I don't know how that would work.

I can also store in redis. And that sort of thing is pretty familiar to me and requires no explanation. But if I do store in redis, the problem is that on every single move, the game would need to get the data from redis and interpret and parse that data in order to recreate the gamestate from scratch. But since moves happen very frequently this seems very stupid to me.

What should I do?

Share Improve this question asked Jan 15, 2011 at 20:04 expressnoobexpressnoob 2,3473 gold badges16 silver badges7 bronze badges 7
  • 1 This doesn't really have anything to do with JavaScript. You essentially just need a server which can deal with all the different connected clients, and since a program executes in memory it will be fast! Only save the game state to a database if you need to "save" the game. Represent your game state in code! – Marcus Whybrow Commented Jan 15, 2011 at 20:07
  • @Marcus thanks, I'm asking for advice on implementation since it's not as obvious to me. How do I represent the game state in code? – expressnoob Commented Jan 15, 2011 at 20:13
  • @expressnoob variables and classes. These are the tools, use them to represent objects in your game, such as Game, Player, Piece etc. The design is down to you and specific to your game. – Marcus Whybrow Commented Jan 15, 2011 at 20:28
  • @Marcus how do I keep them persistent on the server for each individual game? – expressnoob Commented Jan 15, 2011 at 20:39
  • @expressnoob Create a separate Thread for each game the server might be orchestrating and give each an instance of a Game, then if by persistent you meant after the server shuts down, you could serialise all of your objects and save them to file (or database) or translate it into database tables in a more bespoke way. – Marcus Whybrow Commented Jan 15, 2011 at 20:43
 |  Show 2 more ments

1 Answer 1

Reset to default 5

If you really, really don't want the overhead of I/O then just store the game state in a global object keyed by the game id:

var global_gamesate = {}

Then on each connection check what the game id is to retrieve he game state:

var gamestate = global_gamestate[game_id];

Presumably you already have a mechanism to map client sessions to game id.

Usually, game state is small and would hardly take up much RAM. Let's be pessimistic and assume each game state takes up 500K. Then you can serve two million thousand games (four million thousand users if we assume two users per game) for each gigabyte of RAM on your server.


However, I would like to point out that databases like MySQL already implement caching (which is configurable) so loading the most frequently used data basically loads from RAM with a minor socket I/O overhead. The advantages of databases is that you can have much more data than you have RAM because they store the rest on disk.

If your program ever reaches the load where you start thinking of writing your own disk-serialization algorithm to implement a swap file then you're basically re-inventing the wheel. In which case I'd say go with databases.

本文标签: javascriptnodejs storing gamestatehowStack Overflow