hen I was thinking about implementing a system, enabling users to send messages to eachother, I thought about the ammount of data you could save if, instead of saving a copy of the message for each of the receivers, I would save one message with a list of receivers.
There would actually be 3 lists, one list of receivers, one list of booleans, where if b[i], then receiver[i] has read the message, and a third list containinging all the users that have not deleted the message. Every day, I would run a cron job, looking for messages with an empty list of such users, and remove them.
Could there be any problems with this model?
The first schema, it's like trying to replicate the email architecture, which is outdated and does not work quite well.
Definitely, the second approach it's better.
Problems? No one while your code does not have bugs. But consider replies, if you have to support them. Maybe a fourth list could be enough if the instance does not exceeds the 1M size limit.
But actually, a separated model for answers is more consistent and intuitive. This new model will also have lists like: readed_by
, deleted_by
, etc.
The cron job may be unnecessary. You could just delete the message after a user mark it as "deteleted" if message.deleted_by == message.receivers + message.from
.