简体   繁体   中英

How much data can be stored in single JS object on a server that shares state between clients?

I have a socket.io server that powers a multiplayer browser game. The separate ongoing matches are stored inside a single object in-memory on the server-side:

const matches = {};

When a player creates a match, I add a new unique key to this object for tracking information about that particular game. So when players make a move, I do something like this (simplified for example code):

matches[uniqueKeyForMatch] = newValidatedMatchState;

--where newValidatedMatchState is a large tree of data describing this particular match. "Large" in this case meaning in total its various sub-dictionaries and sub-arrays contain several hundred individual pieces of numerical, boolean, and string data. Around 200-800 individual pieces of data like 5 or "some text" , per match.

What I'm wondering is, if I have 100 concurrent matches, can the matches object store that much data efficiently? From this question I see there is some limitation, but I'm not sure what else I could really do other than literally have two variables like matches1 and matches2 and split the data between them. Are there more efficient ways of doing this?

As mentioned in that other question, it's not really an issue. Realistically you'll run out of memory before you hit any other kind of limit. Storing a few hundred/thousand matches doesn't sound like an issue though.

Should your matches be more a kind of persistent data you want to store for a longer period, you'd start storing your matches in files or databases.

Should storing it in databases not fit your needs, or your application just gets too much traffic that the databases can't keep up, split up your application. Have multiple game servers. Your "main server" would just tell the users "your game is available at <server1> ".

Also, you might find Map interesting, a better key-value storage object than regular JS objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM