Database – System Design: Chat Application and Polling

databasedesignperformance

What would be the best way to add chat feature to an android, ios, and web app? Is polling really as bad as people say?

The requirements are user to user messaging and chat rooms.

Here is my thought process:

  1. Long polling with in memory cache. The long polling endpoint will continually check the in memory cache. Pretty simple to implement.
  2. XMPP based server like OpenFire. Hard to horizontally scale. Some other implementations scale well but setup is more complex.
  3. Websocket with in memory cache. Pretty simple to implement.
  4. Short polling with reasonable delay, say every two seconds to ping the server. Query relational database. Store all chats in database. Have it designed so that if user doesn't do anything after 20 seconds, they get a prompt or we stop polling. This seems the simplest to implement.

I guess what I'm wondering is short polling really that bad. I am making a startup and don't want to make a mistake with investing time with a solution that isn't going to scale well.

At the same time, I am thinking that if I get funding / traction, then I can always invest into the more scalable solution.

If I don't make any money from it though, I don't want to invest time into the app.

Any thoughts?

Best Answer

I have used all the options you have provided in the applications I have developed, so I think I can answer that.

Polling

Polling is something which should be avoided, as it requires a lot of bandwidth usage in both client and server side. We don't want our app to use a lot of data on mobile, do we? On the same side keeping the cost of the server minimal is again a good thing.
The delay in receiving messages is again a negative point here. People want immediate delivery of messages (even if they are using a 2G connection) in this decade. It's good to make our apps future ready.

XMPP

XMPP is one of the best thing you can use for Chat. It has in-built stanzas and structure for most of the things which you can do in a chat application. It's not without any issue though. Horizontal scaling is an issue as you have mentioned. It requires a lot of customization. Another issue is that most of the XMPP servers don't implement all specifications of XMPP which requires further customization. I found it too hard to work on XMPP servers and thus entirely avoid it now.

Websockets

This is perfect, if you are comfortable in creating your own framework for that. There're libraries such as Socket.io to make it simpler, although all the other thing you have to take care of yourself. I tried it for my app few years ago, but the client library for Android was not that mature at that time.

Firebase

This is something which I would recommend you to use. This is what most of the beginners are trying these days. You can use FCM for push notification and for faster and reliable way, you can use Firebase Real-time database or Firestore which syncs to multiple clients simultaneously. FCM is free entirely, although the database has some limitations which you can remove by paying. This is one of the cheapest available option too.

gRPC

This is what I would recommend if you have some advanced usage and require a highly scalable app. This is something new and I have seen many Google developers recommending it to our clients(We are a Google premiere partner and thus have direct access to Google guys whenever any such need arises). I am not entirely sure how can you connect it to Android as I have not worked a lot on this, but this link can help you.

Related Topic