Feasibility of Building a Chat App using Couchbase Server, Sync Gateway, and Couchbase Mobile Lite

I am currently exploring the feasibility of using Couchbase Server, Sync Gateway, and Couchbase Mobile Lite to develop a chat application for both iOS and Android platforms. The app will feature chat rooms where one or more users can participate. Each chat room will have a name, description, and an associated image. Users will be able to send messages within the chat rooms. Additionally, users can be members of multiple chat rooms, with each user having a display name and profile image. It is crucial that documents only sync to the devices of users who are part of the respective chat rooms.

Based on my research in the Couchbase documentation, it seems that Channels would be the appropriate mechanism to achieve this functionality. However, I have a few questions regarding the limitations and scalability of Channels.

Could it scale to have millions of chat rooms/channels?

According to the documentation, each document can only be assigned to a maximum of 50 channels. In the context of my chat app, this raises concerns about the user profile document, as a user could potentially be part of more than 50 chat rooms. Since the user profile data needs to be displayed alongside the messages they send in each chat room, how can this limitation be addressed?

Furthermore, the documentation states that a user can only be assigned to a maximum of 1,000 channels. Considering the scenario of a user potentially being part of more than 1,000 chat rooms, is there a workaround or solution to allow users to participate in an unlimited number of chat rooms?

I also wanted to inquire about the use of scope and collations in this scenario. Would creating a new collection for each chat room help overcome the limitations of the maximum number of channels per user and document? Or are there alternative approaches that can address this challenge more effectively?

Additionally, I’m curious if implementing a role per chat room, which would have access to the chat room channel, is worthwhile? This way, users could be added to the role instead of directly to the channel. I’m wondering if this approach introduces unnecessary processing overhead or if it would be a recommended practice to handle user access to chat rooms.

Example data model

{
  "user_id": "User123ABC",
  "type": "UserProfile",
  "display_name": "John Doe",
  "profile_image_url": "https://example.com/profile.jpg",
  "channels": [
    'chatroom_01', chatroom_02
  ]
  // Other user profile fields...
}

{
  "chatroom_id": "chatroom01",
  "type": "ChatRoon",
  "name": "Chat Room 01",
  "description": "This is a sample chat room.",
  "image_url": "https://example.com/chatroom.jpg",
  "channels": [
    "chatroom_01"
  ]
  // Other chat room fields...
}

{
  "message_id": "message123",
  "type": "Message",
  "chatroom_id": "chatroom01",
  "sender_id": "User123ABC",
  "message_text": "Hello, everyone!",
  "timestamp": 1620920123,
  "channels": [
    "chatroom_01"
  ]
  // Other message fields...
}

The Guidance Limits of 50 channels per document and 1000 channels are not hard limits. The documentation explains what the limits are and how to mitigate reaching those limits. In the event that your architecture would exceed those limits, you would need to adjust your architecture accordingly - likely with horizontal scaling (i.e. employing multiple couchbase clusters). There are limits on other things as well, such as the number of scopes and collections in a cluster.
Once you have architected your system, the next step for you is to build a prototype. Good luck.

1 Like

Here is an old Couchbase Mobile Chat App you can use as reference:

1 Like

Thanks for your reply @mreiche I will build a POC, first I am just trying to understand a bit more how to architect it so it can scale as you said.

I understand the channel limits given in the documents are just a guidance but I am trying to understand what that actually means and I am not sure I am understanding the documentation.

Channel Limit documentation
Channels per document - "The amount of memory consumed by the combined number of channels and access grants must fit within the maximum 1Mb xattr size limit " with guidance limit of 50 channels.
Channels per user - “The amount of memory consumed by channels must fit within the 20 MB available on Couchbase Server docs for storing metadata” with guidance limit of 1,000 channels

When looking at the Couchbase Server document size limits
It says for a document
Value and user extendable attributes max size is 20mb
System extended attributes max size is 1mb.

Does this mean
“Channels per document” is stored in System extended attributes with a limit of 1mb?
“Channels per user” is stored in the Value and user extendable attributes with a limit of 20mb?

Sync Metadata limits says every time a document is assigned a new channel, the name is append to that docs metadata. Therefore, a document’s set of channels is limited by the allowed sync metadata size described in Table 2

Table 2 Size Limits for Sync Metadata

enable_shared_bucket_access = false - 20mb per document
enable_shared_bucket_access = true - 1mb per document

What exactly is this doing? If false, is it moving the “Channels per document” storage location from System extended attributes to “Value and user extendable attributes” so it is now 20mb? What does it mean for “Channels per user” as that limit was already 20mb?

enable_shared_bucket_access setting to false seems to then not allow you to scale horizontal though? As I understand it, enable_shared_bucket_access set to true allows your to have multiple sync gateway instances connecting to one Couchbase Server bucket?

The documentation gives 3 ways to increase number of channels when channel count exceeds the useable sync meta data storage space.

1.Lower the number of channels - I don’t think this is an option
2.Shorten the channel names - this is an option but how many additional channels does it give 100s, 1000s?
3.Lower revs limit - this is an option but how many additional channels does it give 100s, 1000s?

What I need to know is how a user can potentially be assigned to 1000s of chat rooms channels “channels per user” and how their user profile document could be assigned to 1000s of chat rooms channels “Channels per document”.

Thanks for sharing @househippo I will check it out, any thoughts if this would scale up in terms of number of users and chat rooms?

Your POC should answer your questions.

What I need to know is how a user can potentially be assigned to 1000s of chat rooms channels “channels per user” and how their user profile document could be assigned to 1000s of chat rooms channels “Channels per document”.

That will be solved in your application with your imagination and ingenuity. Look for research papers for products that already do this - like Slack. Look for papers on application scaling.

Surely someone from Couchbase can shed some insights into some of the questions I have?

Is the documentation correct that when you use

enable_shared_bucket_access = true - 1mb per document

will reduces the number of channels a document can be assigned to and how many channels a user can be assigned to?

Yes you are correct can be solved in the application layer but if you are having to do this to design around the limitations of channels, Couchbase might not be the best fit solution.

Are you saying Slack uses Couchbase mobile and sync gateway channels?

Yes, the documentation is correct.

but if you are having to do this to design around the limitations of channels, Couchbase might not be the best fit solution.

Using a product always requires working with the limits of the product.

Are you saying Slack uses Couchbase mobile and sync gateway channels?

No. I’m saying if you want to develop a chat application, look for research on chat applications.

Thanks for confirming the documentation is correct.

It seem a little odd to me the way it has been designed. By “enabling enable_shared_bucket_access = true” which you need to scale to allow multiple sync gateways to sync to Couchbase server, however this then reduces the number of channels you can have per user because it moves the channel storage location into xattr with 1mb of storage. I am not sure why those 2 items are coupled together when they seem to me to be completely different? Or am I missing something?

The documentation guidance says for channels per user is 1,000 at 20mb so at 1mb that is roughly 50 channels per users. This seems to be very low to me.

If each chatroom is a channel and a user is added to 50 chatrooms/channels what happens when one of their friends add them to the 51st chatroom/channel (over channel storage limit)?
Does Sync gateway crash because the storage limit is exceed for that document or does it remove the oldest used channel from the user?
I am not sure how you can handle this programatically? because the friend isn’t going to know how many channels the user is a member of.

My application doesn’t need to be real time, it is more posts with comments than a real time chat app (data model is the same) and Couchbase mobile sync gateway ticks so many boxers, I am just trying to understand how channels and sync gateway can work at a larger scale like more of a B2C type application with lots of users and highly shareable data or is it more suited to B2B/Enterprise applications with less users and less sharable data?

cc @priya.rajagopal @Mark_Gamble