Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database Structure for Web Messaging System

I want to make an web messaging system like facebook have. I already think of many alternative for the database structure, but not sure which is the best practice for it. I have two alternative here, the first is using two table, the second is using three table but make a cycle in ERD.

First: Two Table, where the message table refer to itself

user
----------
id
name

message
--------------
id
from_id
to_id
message_id --> refer to this table itself, to make me know which message is the topic
subject
content
time
status --> inbox, outbox, archive
read --> read, unread

Second: Three Table, but make a cycle in erd

user
----------
id
name

message_header
--------------
id
from_id
to_id
subject
status --> inbox, outbox, archive
time

message
--------
id
message_header_id
content
time
read --> read, unread
author_id

Personally, I like this structure, because it's only use one message header and many message (content). The author_id itself cannot be removed because I need it to know whether the message is at the left side (as a sender) or right side (as a receiver). This system is just for two person messaging system.

Basically this two table is the same, but which is the best practice to implement this messaging system? Thank you before.

like image 699
deka Avatar asked Jun 27 '13 11:06

deka


People also ask

What database is used for messaging?

MongoDB. Cassandra (Discord, Instagram Direct, Netflix, Spotify, old Facebook Messenger, old Twitter) HBase (Facebook Messenger)

Which database is best for storing messages?

Something like AWS Dynamo DB might be a good fit. Relational databases really shine for complex joins and ad-hoc queries. You won't have either of those in a chat feature. Hence something scalable and NoSQL like Dynamo might be just the ticket.

How is the WhatsApp database structured?

To store data and temporary messages, WhatsApp uses an Erlang-based, distributed DBMS (Database Management System) called Mnesia. This DBMS provides benefits that many traditional databases don't such as: Real-time key/value lookup.


1 Answers

After learning the hard way (times ago, during a final project...), I can advise you to separate and organize the things whenever possible. Self-relationship is a nice thing not to be near, when possible (there are rare exceptions). Design your classes at first instance; build then a database in which things fit well, but keeping things simple as they should be.

My preference is... better drawn than said:

Diagram


Here is the MySQL Script exported via MySQL Workbench.

A possible query to list messages from a certain header would be

SELECT
  h.id AS `header_id`, h.`subject`, h.`status`,
  m.id AS `message_id`, m.content, m.`time`,
  IF(m.is_from_sender, x.`name`, y.`name`) AS `written_by`
FROM (SELECT * FROM header WHERE id = @VAR) h
  INNER JOIN message m ON (h.id = m.header_id)
  INNER JOIN user x    ON (h.from_id = x.id)
  INNER JOIN user y    ON (h.to_id = y.id);
  • You'll see a personal preference of mine to bit fields. For instance, you don't really have to remember a certain from_id more than one time, once your purpose is a two person messaging system.
like image 162
leonardo_assumpcao Avatar answered Oct 02 '22 09:10

leonardo_assumpcao