The problem domain is short and sweet;
- We need to write game data.
- We need to read game data.
The only real snag is, being single threaded, we need these operations to block as little as possible -- as in thousandths of a second. I've even toyed with the idea of reading everything at server start and only performing writes during execution. That would cut our blocking problem in half but removes our ability to modify data externally.
The model in my head calls for very little of the
R in
RDBMS. Heck, MUDs have managed very well for decades using flat files on systems with less processing power than your dishwasher.
Lastly, given that this hobby-coding, I have the luxury of asking, 'is the solution fun?' Let's set a couple goals;
- I don't want a crash to lose the overall state of the game -- which precludes saving writes for an extended period, or even worse, until server shutdown.
- Simple to install. Freely available software that does not require a DBA to manage.
- Simple to operate. No fretting dirty caches, scheduled housekeeping , etc.
- I would prefer back-ups and restores to be file copies. Tar'ing a directory or two is fine.
- Some external method to futz with the data while the game is running.
Flat files are certainly doable (and made crazy easy with Python's
Pickle module). They meet goals #2, #3, and #4 but I cringe at the amount of runtime file-io needed to cover goal #1 and supporting goal #5 means we have to perform constant reads as well (plus some form of file-locking mechanism). Ick.
A lightweight dbms like
SQLite would work except for goal #3. Wrapping Python
CRUD in SQL statements is soul-destroying tedious. Yeah, I could tap a ORM like
SQLAlchemy but let's look at that new sexy, NoSQL...
When I was a kid, one Christmas I got this electronics kit with 150 projects. It was awesome. You could build things like a crystal radio, lie detector, and light activated room alarm.
Python holds that same appeal for me. It's a big toy. Another one I've found is
Redis -- a dead simple NoSQL data store. The distinguishing feature is all data is held in-memory which makes it wicked fast.
I'll admit, when I first read that it holds everything is RAM it struck me as rather pointless. I mean, wasn't I already doing that in my program? And who wants to hold everything in memory whether you need it or not?
Having played with it, the utility and genius starts to shine through. Redis has a clever system of serializing to a file based on the frequency of updates and you can copy this file at any moment without fear of corrupting it. The author also provides a really nice CLI tool with history and auto-complete similar to a Linux terminal.
We're pushing goal #2 a bit since it requires installing three packages,
Redis,
Hiredis, and
Redis-py but they all loaded with minimal fuss. Hiredis needed the Python development libs you can get on Ubuntu using:
$ sudo apt-get install python-dev
...to be continued