The best data model always depends on the structure of data and the access pattern. The natural access pattern for your messages is likely lookup from two sides: sender and receiver. And you might need to support message expiry too. This means that you need on-disk indeces if you want more messages than will fit in memory.

In Riak you can do it quite simply with any of the ordered backends (leveldb, hanoidb, lowkeydb):

One message = one Riak object. Example: {sender='321', recipient='123', timestamp = 1234567, text= 'bla bla'}
Key is timestamp prefixed.
2i indeces for <sender_id> and <recipient_id>.
Consider using a compound index if you often distinguish between read/unread messages. For instance: <read-status>_<recipient_id>

Example query returning keys for all unread messages belonging to recipient with id 123 using compound index:
http://myhost:8098/buckets/messages/index/recipient_bin/unread_123

Example query returning keys for all messages older than a year, so you can delete old messages (not necesary using backends with autoexpiry):
http://myhost:8098/buckets/messages/index/$key/0/2011-09-21


Here is an alternative approach: You can choose to optimize for the recipient side of the access pattern, by bulking all the messages for a given recipient in one Riak object. This is much faster that retrieving a large number of messages individually, the price being more difficult lookup from the sender side:
Key: recipient_id
One Riak Object = all messages for a given recipient. Example: [{sender='321', timestamp = 1234567, text= 'bla bla'},{sender='456', timestamp = 1234569, text= 'bla bla2'}]
2i index for <sender_id>s

Example query returning an object with all messages belonging to recipient with id 123:
http://myhost:8098/buckets/messages/keys/123

If you want text search on for instance message content, you should consider using Riak Search.

- Rune

Trifork


Best regards / Venlig hilsen

*Rune Skou Larsen*
Trifork Public A/S / Team Riak
Margrethepladsen 4, 8000 Ã…rhus C, Denmark
Phone: +45 3160 2497    Skype: runeskoularsen   twitter: @RuneSkouLarsen



On 20-09-2012 01:33, Charlie Bowman wrote:
I'm in the process of designing an in game messaging system that basically replicates the same functionality of email all within the context of the application. I do not have a ton of experience with true key/value db stores so I'm looking to find a good resource for ideas on how to best model this. I have a first version, but I dont want to reinvent the wheel if can help it. I've done the requisite googling, but haven't found anything relevant yet. Are there any guides/tutorials/open source projects that anyone knows of.

Thanks!



_______________________________________________
riak-users mailing list
riak-users@lists.basho.com
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com

Reply via email to