Nikola Ninkovic
Nikola Ninkovic

Reputation: 1262

NoSQL database design for email-like web app

I'm developing a web app for private messaging inspired by email services (Gmail) using AngularJS on client side and MongoDB (Mongoose) and NodeJS for RESTful server.

Currently, this is the db structure

{
conversation: {
    user1: {
        type: mongoose.Schema.ObjectId,
        ref: 'User'
    },
    user2: {
        type: mongoose.Schema.ObjectId,
        ref: 'User'
    },
    user1Flags: [String],
    user2Flags: [String],
    messages: [{
        title: String,
        content: String,
        sender: {
            type: mongoose.Schema.ObjectId,
            ref: 'User',
        },
        sentAt: Date,
        createdAt: Date,
        attachments: [String],
        senderMetaData: {
            archived: Boolean,
            deleted: Boolean
        },
        recipientMetaData: {
            archived: Boolean,
            deleted: Boolean
        }
    }]
}
}

Well, I'm satisfied with this, but I don't have too much experience with NoSql.

I want to hear your opinion on this, is this the right way to go ?

Upvotes: 0

Views: 960

Answers (1)

Stennie
Stennie

Reputation: 65443

An obvious performance consideration for your design is that you are storing potentially large embedded arrays of messages in a single conversation document. As the very active conversations grow larger, those documents will require more frequent relocations on disk and ultimately could hit the maximum document size (16MB, as at MongoDB 2.6). If you only care about the recent messages in the array, you will also incur a growing overhead of loading the full document into memory on the MongoDB server even if you only project a subset of recent information via the driver call.

For some ideas on how to design an efficient messaging approach, I would suggest reading the documentation for the Socialite project. This is most definitely overkill for your use case, but you might find some helpful things to consider. For example, the Feed Service describes several different approaches including disadvantages and suggestions on when each is more suitable.

Upvotes: 1

Related Questions