Design users chat data


#1

Hi all!

I just started use couchbase, and really liked it.

I try design users chat and find 2 way:

  1. Data design like embedded:

    UserID : {
    profile:[
    {
    name:“Alex”
    },
    {

    }
    ],

},
chat: {
UserID2:{messages : [] },
UserID3:{messages : []}
}
}

  1. Create new document for all chat room.
    like:
UserID 1 - UserID2 : [ { type:text, author:UserID1, value:value, date: new Date() } ] How do you thing, what option better? Also I'm interested in blogs or book with good example nosql design.

#2

Hi @AlexDb, are you using couchDB or couchbase. they are drastically different in their behavior.


#3

Hey cihangirb!,

Sorry, I’m used couchbase*.


#4

thanks, I can help you with Couchbase :smile:,
data model depends very much on how you’d like to optimize the system and what you expect the items counts to be. so a few questions:

  • How many users and how many chat rooms per person do you expect?
  • I see message in there on your first option as well under the user document. In option #2, are you planning on keeping messages in chatroom or under your second proposal, will you break messages into separate doc as well?

#5
  • 10000+ active users, 10-20 personal chat room each user.
  • In option 2, I design create new document for each chat room. For example:
    User1 chat with User2:

doc_id : “User1 User2”,
messages: [
{
“author”:“User1”,
“value”: “Hey!”,
“date” : new Date()
},
{
“author”:“User2”,
“value”:“Hi, I cant chat now…”,
“date” : new Date()
}
]


#6

I’ll assume chatroom are many to many as well. I’ll also assume you will have flows where folks will come in to chatrooms directly and flow into users and visa versa. - start from users and go to chatrooms.
Option #2 would be the balanced model for me. The design would ensure balanced optimization for both flows. I’d recommend putting in all chatroomIDs in user docs that are participating in the chatrooms and chatrooms to contain the userIDs.
It seems like you want to overload the chatroomID with User1+user2+… What do you gain by doing that exactly? I can see a gain for the first user but if you want to use the key to search for the chatrooms a user is in, you are better of using the user document and chatroomID reference. but maybe I am missing something?
thanks
thanks
-cihan


#7

cihangirb, Thank you!

I’m really liked option 2. Yes, I can get name documents like ID connection.
The problem If I have a lot of chat room, in my database -> I have a lot of little document without real content.

Another way, save messages ID in user document:
User : {
connections: {
connectionID:[
messagesID1,messagesID2,messagesID3 …
]
}
}

And save messages in new documents, like:
messageData1 : {
messageID1 : {
type:text,
value:‘Hi!’
},
messageID2 : {
type:text,
value:‘Hey!’
},
messageID3 : {
type:text,
value:‘How are you?’
}
}

What do you thing about this?


#8

it is hard to say without knowing all the compromises you want to make but I’d not worry about documents with no data at all. the overhead is small to nonexistent depending on the settings you choose.
thanks
-cihan


#9

cihangirb, Thank you!