21

Out of an opportunity and boredom, a friend and I decided to make a web based game. This is the first 'game' I will be making, since usually I program web apps in django.

I've opted to use the same framework for the game, but I am not totally sure about the data storage, since I cannot foresee future requirements and DB related issues. Lately, the noSQL movement is gaining force and I would like to explore that area.

Therefore I have the following questions:

  • Would a noSQL datastorage (document-based, such as MongoDB) would be suitable for a web-based game?
  • What are the issues that might arise using a conventional RDBMS, such as MySQL?
  • How to insurance data consistency amongst users with MongoDB, during gameplay or at timed events, such as cron jobs?

Game overview: Typical adventure game, with the player fighting monsters and gaining items and such.

  • Some data is static across all players: items, weapons, potion, maps, etc.
  • Some data tied to the player: inventory, metrics (stats), progress, etc.
  • A player might need to know the stats and state of another player
  • Scheduled tasks will be run at certain time interval
Jesse Dorsey
  • 2,920
  • 4
  • 33
  • 59
Pran
  • 381
  • 1
  • 2
  • 7
  • Related: http://gamedev.stackexchange.com/questions/2282/what-kind-of-databases-are-usually-used-in-an-mmorpg – Ricket Nov 08 '10 at 04:53
  • 1
    An issue with some noSQL databases is that they cannot do queries unpredicted at "design" time in a fast way on huge datasets like event logs: http://gamedev.stackexchange.com/q/4465/450 Please keep in mind that noSQL databases require the same techniques against query injection normal than normal databases do. – Hendrik Brummermann Nov 08 '10 at 07:18
  • 1
    @nhnb: One nice way of restating your point is "use a relational database for relation data, and use an object/document database for object/document data". Unfortunately I don't think most people have experience with or spend a lot of time thinking about modelling, which leads to bad designs no matter which they choice. –  Nov 08 '10 at 19:17
  • 2
    In response to your question "Is NoSQL a valid option for web based games?" I believe the answer is yes. NoSQL databases have been used for web-based games before (such as FarmVille) and offer a lot of flexibility. The main point of contention for this question seems to be "which is better (NoSQL or SQL)?". Each system has pros and cons, but both are perfectly legitimate options. If you're looking for a detailed list of the advantages of one system over the other, you may want to pay the Programmers StackExchange a vist. :) – Ari Patrick Nov 09 '10 at 00:41

5 Answers5

21

Would a noSQL database be suitable for a web-based game?

Absolutely! Generally speaking, non-relational databases (such as MongoDB) are much better for games, as they are more flexible in how they model data, while being more performant than relational databases (such as SQL) - making them a "win-win" choice.

What are the issues that might arise using a conventional RDBMS?

There are two major drawbacks to using relational databases:

  • Lack of flexibility in the way you model data
  • Performance

Lack of flexibility in the way you model data is a major drawback, because using a relational database forces you to model data to fit the needs of the database, rather than the database accommodating the needs of the model. For example, consider you're creating the model for how you'll store items in a game using a relational database and you decide on the following model:

weapon_type_id | weapon_type
1 | sharp

weapon_id | weapon| weapon_type_id | damage
1 | Short Sword | 1 | 5

potion_type_id | potion_type
1 | HP
2 | Mana

potion_id | potion| potion_type_id | modifier
1 | Healing Elixer | 1| 5
2 | Mana Drainer | 2| -5

Now consider how you'd need to modify the model to meet do the following:

  • Add a new item type
  • Create a weapon with multiple weapon types
  • Create a potion item that can also be used as a weapon

Certainly all these actions are doable, but you won't be the happiest person while you're doing them, and you'll probably wonder, "is there a better solution out there for what I want to do?," when you could design a much more flexible model in a non-relational database.

Note: If you are unfamiliar with how document-based databases store information, please check out this link before continuing. The model presented below designed to use similar logic to what is presented in the aforementioned article.

db.itemTypes

name: Weapon
types: Sharp

name: Potion
types: HP, Mana

db.items

name: Short Sword
weapon
    type: Sharp (db.itemTypes reference)
    damage: 5

name: Healing Elixer
potion
    type:  HP (db.itemTypes reference)
    modifier: 5

name: Mana Drainer
potion
    type:  Mana (db.itemTypes reference)
    modifier: -5  

name:  Healing Potion Sword
potion
    type: HP (db.itemTypes reference)
    modifier: 10
weapon
    type: Sharp (db.itemTypes reference)
    damage: 15

There are a lot of good articles on the performance of NoSQL vs SQL databases, but here is one which provides application examples so you can perform your own tests: MongoDB vs. SQL Server 2008 Performance Showdown

How do I insure data consistency amongst users with MongoDB?

MongoDB supports read/write atomic operations on single data entities (documents), so query results from MongoDB should always be accurate with what is stored within the database.

Edit: Provided NoSQL example of item database

Ari Patrick
  • 2,365
  • 17
  • 18
  • In the example that you wrote using sql, how would you, at a high level, model it using nosql? – Pran Nov 08 '10 at 17:17
  • Updated my answer to provide one possible model in NoSQL – Ari Patrick Nov 08 '10 at 18:06
  • 4
    -1, your answer shows only static data. The whole debate over SQL vs. NoSQL for game databases involves highly dynamic data. The best thing to do would be to show a NoSQL transaction that trades something between two players - a very common occurrence, and one most games get wrong regardless of which type they choose. –  Nov 08 '10 at 19:13
  • (And in particular, I'm not familiar with MongoDB, but if as you say atomic operations occur only on single entities - it's really unsuitable for transactional logic in multiplayer games.) –  Nov 08 '10 at 19:14
  • I strongly disagree that "the whole debate over SQL vs. NoSQL for game databases involves highly dynamic data" as there is a lot (if not more) static to moderately dynamic data that goes into games online/multiplayer games. Atomic operations happen at the (highest) document level, and since documents can be embedded within each other, there is less of a need for multi-document operations. – Ari Patrick Nov 08 '10 at 20:25
  • 3
    @Ari: There's a lot more static data, but no one cares about the throughput for writes to it, because no one writes to it - no writes, no transactions, no ACID, no real database question, even if you happen to store it in one. That question is then easy to answer - just keep it in memory. Certainly "How can we load static data faster?" is an interesting question, but I don't think it's at all related to questions of SQL vs. NoSQL. -- As for multi-document operations, does this mean your database would have one document with e.g. all players in it? –  Nov 08 '10 at 21:08
  • More than likely, you would have a database of players, with individual player documents. In terms of atomicity, different flavors of NoSQL have slightly different setups. Here is some of the atomicity documentation for MongoDB, which is the NoSQL implementation mentioned by the OP: http://www.mongodb.org/display/DOCS/Atomic+Operations – Ari Patrick Nov 08 '10 at 21:29
  • So either I can put all my players in the same document and have crazy contention, or I can put them in different documents and have "pseudo-atomic" (barf) operations at best? NoSQL can be a step up from SQL, but this ain't it. –  Nov 08 '10 at 21:48
  • @Joe Wreschnig "By default a multi-update will allow some other operations (which could be writes) to interleave. Thus, this will only be pseudo-atomic (pseudo-isolated). To make it fully isolated you can use the $atomic motifier" Unless I'm reading that wrong, MongoDB is not pseudo-isolated at best, it's isolated at best. – Ari Patrick Nov 08 '10 at 22:04
  • Atomicity and isolation are not the same. See the comment by "Matt" near the bottom of the page. (It also sounds like it enforces isolation by a db-wide lock, again defeating the point.) –  Nov 08 '10 at 22:07
  • And incidentally, this failure of atomicity is exactly what leads to item duping if you have a one document = one player model. –  Nov 08 '10 at 22:08
  • @Joe Wreschnig Before I comment further on atomicity, I'd like to perform some tests on my end, to verify Matt's results and do some further research. I know NoSQL has been used in a number of production environments, so I would like to see how others have overcome this issue, if in fact it still exists. – Ari Patrick Nov 09 '10 at 00:29
  • 2
    @Ari: Please don't confuse NoSQL, which is an idea, and MongoDB, which is a particular database. At my last job we shipped two games on a NoSQL database and got excellent concurrency with low-latency. But our NoSQL database also supported multi-document transactions with field-level locking. NoSQL is not a single "thing" like SQL, it just refers to any database not using SQL (and usually not using relational schemes). –  Nov 09 '10 at 09:47
  • 5
    Just my $.02, but "not performant" is not a drawback of RDBMS. It's a drawback of storing non-relational data in an RDBMS, not tuning your RDBMS, not understanding what your SQL is doing, not indexing, etc. etc. etc.

    If you have relational data, you'll find RDBMSes are incredibly optimized.

    – Robbie Jul 15 '11 at 20:57
20

Couple of words do defend SQL databases.

1 - If you have SQL database you can work with your data not only by primary key. Most of queries in MMO goes by PK but when you need to find all users with level > 30 what will you do in NoSQL world?

2 - If you have SQL language you may create "hotfixes" to repair broken data. For example: "update player_items set flag=0 where item_type_id = 100". How can you fix this in NoSQL? I think you will have to make a script which will parse all your data...

3 - SQL databases are not so slow as you can think. My collegues create the web based MMO game, wich make 5000 transaction per second in MySQL. Is it not enough for you? If not, you may use sharding to scale your database.

4 - SQL has a foreign key constraints and such good stuff, which will help you to find bugs in your code.

NoSQL is not a silver bullet. It solves only couple of problems and brings many new problems. So my advice - think twice before choose NoSQL.

Andrey Frolov
  • 378
  • 1
  • 7
  • +1 (hoping this is not FUD :P since I don't know NoSQL at all...) – o0'. Nov 08 '10 at 13:52
  • 1
    These are all great reasons to avoid NoSQL until you actually have a need for it. Especially #3. Unless you have zillions of users already (and refuse to shard anything), you'll probably be much more productive with MySQL. – ojrac Nov 08 '10 at 16:57
  • 5
  • You'd use: "for x in db.user.find({"level": {"$gt" : 30}})"

  • Technically, what you wrote is a script as well, so I'm not quite sure what your point is.

  • It is VERY possible to make an MMO using SQL, and in fact, many MMOs have been developed using the technology. NoSQL technology is fairly new and has already been adopted by services like Amazon, Google, and Facebook for it's performance and flexibility.

  • In NoSQL you would need to write your own constraints, but that's simply the cost of added flexibility.

  • – Ari Patrick Nov 08 '10 at 17:10
  • 2
    I agree with your statement of thinking twice. That's partially what I am doing here with this question :) – Pran Nov 08 '10 at 17:10
  • 10
    SQL is not a sliver bullet. NoSQL is not a silver bullet. Think twice before using any technology. – stonemetal Nov 08 '10 at 17:54
  • 5
    Regarding 3, the problem is not so much that SQL is slow, but that SQL is laggy. Generally speaking, SQL databases get excellent throughput at the expense of latency. For a web-based game, that's probably what you want! For a real-time networked game, it's probably not, and most "WoW-like" MMOs that use SQL use a caching layer in front of it that removes most of the benefits of SQL, which is why NoSQL is a big step up for them. –  Nov 08 '10 at 19:11
  • @Joe Wreschnig +1 for laying that out in a very easy to understand manner. Out of curiosity, is there any particular reason you don't think web-based games could benefit from NoSQL? – Ari Patrick Nov 08 '10 at 20:32
  • I might as well just make this an answer, coming soon... –  Nov 08 '10 at 20:33
  • @Joe Wreschnig in MMO like WOW (one of them I am developing now) you need to model the world in your server memory. We don't have to make a cache, we already have it as a side effect. This world is working as a cache so we have to process only 100 transaction per second. You don't need to use fast database here. So you can use database that can have a lot of features, such as SQL. And we use this features! Belive me :) And I forget to notice that SQL databases is fits for ingame-database. For resource database (like items descriptions) you must use XML or PlanText file in order to versioning. – Andrey Frolov Nov 08 '10 at 20:56
  • 1
    @Andrew: I addressed your concern on the question Ricket linked; in summary, many transactions in MMOs need to be done cross-server, and many single-server changes still need transactional memory and need to be pushed atomically and as quickly as possible to prevent upset and confused players. I don't think SQL's features (or lack of them) really come into play - it's really all about making the interface between your code and your data store as painless as possible. –  Nov 08 '10 at 20:59
  • 1
    @Ari Patrick Amazon, Google, and Facebook use MySQL too. Check this out http://highscalability.com/blog/2010/11/4/facebook-at-13-million-queries-per-second-recommends-minimiz.html AFAIK this firms are using both SQL and NoSQL approaches in different services. – Andrey Frolov Nov 08 '10 at 20:59
  • @Andrew Frolov Re-reading my comment, I see how it seems like I'm saying "Amazon, Google, and Facebook ONLY use NoSQL" when what I was trying to get across was that the services use NoSQL as an essential layer. This brings up an interesting point that SQL and NoSQL do not have to be used exclusively (although I believe this option is probably overkill for the situation the question presents). – Ari Patrick Nov 08 '10 at 21:11
  • @Andrew Frolov You can version control SQL databases, in fact you should ALWAYS version control your development databases, regardless of the type. – Ari Patrick Nov 08 '10 at 21:18
  • I do) (10 characters) – Andrey Frolov Nov 08 '10 at 21:23