In our system every user can write any other user a message. The first obvious idea is a data model like this:
User username email ... more properties Message user_from_FK user_to_FK text creation-date ... more properties
So a message stores the User-Key as a FK in a tradition database. Like (for simplicity visualised as a table):
User-"Table"
KEY username ... ----------------- 1 peter 2 paul KEY
Message-"Table":
KEY user_from_FK user_to_FK creation-date text ... ------------------------------------------------------- 11 1 2 2342342342234 Hi Paul. 22 1 2 2342342356455 Hi Paul. You got my message? 33 2 1 2342342377544 Hi Peter. Yes, I did.
To query all messages for a given user is simple then:
SELECT __key__ FROM Message WHERE user_to_KF = :userKey ORDER BY creation-date
Our system should scale to millions of users and millions of messages. Maybe 500 messages will be send per second. Is this simple solution then a good data model? Can we do it better? (Every user is not allowed to have more then 1000 messages and this inbox. Messages should be sorted by date when returned. And we want to do paging.)