5 ms·
This is really interesting. We did something similar for GameClay. I stored game properties as JSON-encoded dicts stored in MogileFS, then had a "regular" MyS
by nostrademons 18y ago
This is really interesting.
We did something similar for GameClay. I stored game properties as JSON-encoded dicts stored in MogileFS, then had a "regular" MySQL table that would point to the MogileFS key for the file, then the Python code would just read it out, use a JSON library to parse it, and manipulate it as a Python object. We had normal MySQL indexes on all the game metadata that appeared in the UI, so if you want all arcade games that nostrademons has posted, it would do a normal index search on (nostrademons, "arcade"), find the Mogile key for that, and then fetch it.
We never got to the point where we'd need to shard, alas. I'd like to think I would've thought of the index-per-table approach independently, but probably not.
Curiously, I did things this way for ease of development, not performance (and there were probably lots of little things that would've killed our performance anyway - it's really hard to diagnose performance bugs until you have real users banging away). We needed a storage system - MogileFS and MySQL work out of the box. We needed a data format - JSON works out of the box. We needed to be able to change game schemas rapidly, since we kept finding our existing design was not really fun to play with (and that's what killed us, ultimately). The rest of it followed pretty obviously.
- sam_in_nyc 18y agoI just recently wrapped up using a similar "store serialized data" set-up myself. In my example, I'm allowing the user insert/remove/reorder items on a list. This type of operation is pain to do with SQL operations. If you have a list, A,B,C,D,E,F and you want to insert G before C, then you either have to: delete all, then insert A,B,G,C,D,E,F. set "sortnumber" on G to "3", and increment sortnumber on all >= 3. set "sortnumber" of G to be the average of B and C. The latter is the cleverest, but eventually you run out of space in floating-point land, unless you have a Cron come in and clean everything up periodically. And of course, reordering (the example was an insert), and removing need to be considered as well. So it's a choice between lots of deletes and inserts (but really easy). Semi-annoying logic of reordering/removing and a few updates. Or clever hack that requires a cron to cover your ass. What I did instead was stored the list as JSON, convert it to an array, and use array splicing functions to reorder things. Then, I convert it back to JSON and store it. It's worked extraordinarily well... It takes a fraction of the amount of time to do native data structure stuff than it does to touch the DB several times.
- bk 18y ago> It takes a fraction of the amount of time to do native data structure stuff than it does to touch the DB several times. And app processes are much easier to scale than a database. Even in "slow" languages (Ruby in my case) array operations are extremely fast, definitely "fast enough".
- jsrn 18y agoWhile your approach to do this in JSON is cool, I think you have overlooked the 'direct' solution to do this in a RDBMS - with a linked list. Here is a quick suggestion (works in Postgres): create table l ( id char primary key references l(prev) deferrable initially deferred, prev char unique not null references l(id) deferrable initially deferred, mydata text not null ); then I populate the table with your example items: insert into l (id, prev, mydata) values ('A', 'F', 'dA'), ('B', 'A', 'dB'), ('C', 'B', 'dC'), ('D', 'C', 'dD'), ('E', 'D', 'dE'), ('F', 'E', 'dF'); let's see how that looks like: test=# select * from l; select * from l; id | prev | mydata ----+------+-------- A | F | dA B | A | dB C | B | dC D | C | dD E | D | dE F | E | dF (6 rows) to insert a new item into the list, you would do: begin; update l set prev='G' where prev='C'; insert into l (id, prev, mydata) values ('G', 'C', 'data for G'); commit; so that's one update, one insert for an insertion into the list. Note that the two commands have to be in one transaction, because inside the transaction the foreign key constraint is violated (as allowed by the deferrable initially deferred modifier). Let's inspect our list again: test=# select * from l; select * from l; id | prev | mydata ----+------+------------ A | F | dA B | A | dB C | B | dC E | D | dE F | E | dF D | G | dD G | C | data for G so the predecessor of G is C, and the predecessor of D is G, like specified. Of course, you loose the ability to sort with 'order by', but that's no big deal: you know the predecessor and successor of each item, so it's easy to traverse the list in either order. This could be done on the client side [probably the best solution in your case], in the application code, or inside the database with a stored procedure or with a recursive query (coming in PostgreSQL 8.4), in Oracle it could probably be done with 'connect by'. In reality, you would of course choose other datatypes for id and prev (probably integer), but I wanted to translate your example as literally as possible. Another problem that's easily solved: how do I get all elements of one list? Solution: Either give me one 'starting element' and the list is traversed and returned. Or introduce a listId attribute and select by that, which is probably faster but without sort order.
- ryanwaggoner 18y agoThis is really interesting...could you do the same thing using S3 instead of MogileFS? The advantage would be cost and simple scalability, especially if you're running on EC2 so you don't have to pay for all the back and forth transfer from EC2 to S3. Concerns might be latency issues and whether this would scale to billions of objects. Other than that, it would seem like you could run a pretty huge site off of just a couple beefy servers.
- nostrademons 18y agoAbsolutely. It'd work with any sort of distributed filestorage. It wouldn't be too hard to wrap the S3 methods so that the API is consistent (the MogileFS API is basically a dict), so app code wouldn't even have to change. We used Mogile over S3 mostly because I didn't realize there was no minimum monthly fee for S3.