如何在数据库中存储实时聊天消息? [英] How to store real-time chat messages in database?

查看:582
本文介绍了如何在数据库中存储实时聊天消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前将mysqldb用于我的数据库,并且需要集成实时的消息传递功能.龙卷风提供的chat demo不会实现数据库(而blog 可以.)

I am using mysqldb for my database currently, and I need to integrate a messaging feature that is in real-time. The chat demo that Tornado provides does not implement a database, (whereas the blog does.)

此消息传递服务将来也将兼作电子邮件(例如Facebook的消息传递服务的工作方式.聊天平台也是电子邮件.)无论如何,我想确保我当前的第一个聊天版本能够使用要进行扩展以用作电子邮件,总体而言,我需要将消息存储在数据库中.

This messaging service also will also double as an email in the future (like how Facebook's message service works. The chat platform is also email.) Regardless, I would like to make sure that my current, first chat version will be able to be expanded to function as email, and overall, I need to store messages in a database.

这样的事情很简单:对于每条发送的聊天消息,查询数据库并在用户屏幕上显示该消息.还是这种方法容易遭受服务器负载高和优化效果不佳的困扰?我应该如何准确地构造基础结构"以使其正常工作?

Is something like this as simple as: for every chat message sent, query the database and display the message on the users' screen. Or, is this method prone to suffer from high server load and poor optimization? How exactly should I structure the "infrastructure" to make this work?

(对于这个问题的某些固有主观性,我深表歉意;但是,我宁愿两次测量,一次编码.")

(I apologize for some of the inherent subjectivity in this question; however, I prefer to "measure twice, code once.")

赞赏输入,示例和资源.
问候.

Input, examples, and resources appreciated.
Regards.

推荐答案

Tornado是单线程无阻塞服务器.

Tornado is a single threaded non blocking server.

这意味着,如果您在主线程上进行任何阻塞调用,最终将导致性能下降.起初您可能没有注意到这一点,因为每个数据库调用可能仅阻塞20毫秒.但是,一旦您每秒执行200次以上的数据库调用,您的应用程序将被有效地锁定.

What this means is that if you make any blocking calls on the main thread you will eventually kill performance. You might not notice this at first because each database call might only block for 20ms. But once you are making more than 200 database calls per seconds your application will effectively be locked up.

但是,有很多数据库调用.在您的情况下,将有200个人在同一秒内发送聊天消息.

However that's quite a few DB calls. In your case that would be 200 people hitting send on their chat message in the same second.

您可能想做的是使用具有非阻塞API的队列.因此,龙卷风收到聊天消息.您将其放在队列中,以通过另一个进程保存到数据库,然后将聊天消息发送回其他聊天成员.

What you probably want to do is use a queue with a non blocking API. So Tornado receives a chat message. You put it on the queue to be saved to the database by another process, then you send the chat message back out to the other chat members.

当某人连接到聊天会话时,您还需要向队列发送所有先前消息的请求,当队列响应时,您会将那些消息发送给新连接的用户.

When someone connects to a chat session you also need to send off a request to the queue for all the previous messages, when the queue responds you send those off to the newly connected user.

无论如何,这就是我要解决的问题.

That's how I would approach the problem anyway.

也请参见以下问答:请记住,龙卷风是单线程的.太奇妙了.并可以处理数千个同时连接.但是,如果其中一个连接中的代码阻塞1秒钟,则在该秒钟内将对其他任何连接执行不执行.

Just remember, Tornado is single threaded. It's amazing. And can handle thousands of simultaneous connections. But if code in one of those connections blocks for 1 second then NOTHING else will be done for any other connection during that second.

这篇关于如何在数据库中存储实时聊天消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆