tags:

views:

56

answers:

2

If one were to copy twitter's DM feature, how would you go about designing the schema for it? I'm able to send and reply to messages, but my issue is how do I keep track of the sent messages? If Bob sends Amy a message, Amy will see Bob's message. Bob also has a copy of his message located in his "sent folder."

Right now, each message has a "recipient" and "sender." At first thought, I thought I it since I could just query to see who the sender is, thus displaying the message in the user's "Sent" tab. However, Amy might want to delete the message, then what? I don't want to get rid of the message since Bob may still want it in his sent archive for whatever reason.

The only other option I could think of is to save the message twice, which is stupid and could get out of hand.

A: 

The only other option I could think of is to save the message twice, which is stupid and could get out of hand.

In fact, it's quite the opposite of stupid. Saving the message twice is exactly what's expected here. De-normalization is expected in these types of non-relational databases.

And look at your own description.

Bob also has a copy of his message located in his "sent folder."

OK, so Bob has "a copy" of the message, it seems pretty reasonable this "copy" be a different document in the database.

Gates VP
A: 

2 solutions I can think of are:

  1. Save 2 copies of the message. This may seem like a waste, but works well especially if you need to shard across multiple servers - all of the users details are stored together. (Note: This is how email works - a very distributed model)

  2. Save one copy with sender/receiver as you have mentioned, but make your delete function a "soft" delete - ie. flag the message as deleted instead of actually deleting it. This might require 2 flags if you want the sender and receiver to be able to delete the message independently of each other.

Brenton Alker