8

I am working on a jQuery Mobile Web App where a company will have the ability to message certain groups of users (based on their profile preferences).

I am debating on what is the most efficient way to mark when each user has read the latest messages. I have considered using a session to try and keep track of the last time they opened the messages page, and comparing that to the post times of the messages. I have also considered a table with the message_id and the user_id, marking each one as read when they open the page.

I think both would work, but I am trying to balance the pros and cons. Keeping in a database would allow me to keep a history (especially if i added a timestamp column to know when they read the message), but if it is going to hurt the app performance due to the table size, then it may not be worth it. The app will potentially have 10's of thousands of users.

One thing I should probably mention is that the users may use the app on multiple devices and the app will have very long session times, potentially allowing the user to stay logged in for months. I like the idea that if they read it on one device then it would mark it read on all devices, which may make sessions difficult to work with, right?

8
  • for each message, add a read column. Shouldn't be much (if any) of a performance hit. MySQL is a highly powerful piece of database software, and provided you have decent servers and optimization, you'll be able to do millions of records no problem
    – Kevin Pei
    Mar 27, 2014 at 0:11
  • 1
    I can't just add a read column to the messages table because one message may go from anywhere from one to 1000 users. I was planning on just having one row in the messages table. Would it hurt efficiency to have 1 row in the messages table and a separate table with message_id, uid to mark them as read?
    – mtindall89
    Mar 27, 2014 at 0:13
  • Any performance change with that extra table would be negligible (think a couple of milliseconds).
    – Kevin Pei
    Mar 27, 2014 at 0:14
  • Also, on a separate note you should not be keeping sessions for months. It is highly insecure as it opens up a much larger window for session hijacking. Rather you should be generating a new session each time the app is accessed, and using a different "remember me" cookie or something each time to authenticate them.
    – Kevin Pei
    Mar 27, 2014 at 0:16
  • I believe that's actually what is happening (I didn't write that code personally), but I will double check into it. Thanks
    – mtindall89
    Mar 27, 2014 at 0:20

2 Answers 2

16

Ok, I'm gonna put everything I said in the comments into one solid answer.

Short Answer: You should be using the database to store 'read' notifications

Logic behind it:

  1. It should be a negligable performance hit with decent servers and optimized code (couple of ms max) even with hundreds of thousands of users
  2. It is highly maintainable
  3. You can track it and sync it across devices

Specifically why you shouldn't use sessions

  1. Sessions were designed to store temporary user data (think ram), they're not supposed to log stuff.

  2. You should not be keeping sessions for months. It is highly insecure as it opens up a much larger window for session hijacking. Rather you should be generating a new session each time the app is accessed, and using a different "remember me" cookie or something each time to authenticate them.

  3. Even if you do make your session persist for months, after those months won't the user all of a sudden get a bajillion "unread" notifications?

How to store it in the database
This is called a many-to-many relationship (from the message perspective) OR a one-to-many relationship (from the user perspective)

Table 1: messages

ID, message, timestamp

Table 2: messages_users

ID, user_id, message_id, read

Table 3: users

(Do user business as usual)
4
  • so with this method, when I would add a message, i'd add one row to messages, and then one row to messages_users for each user getting the message, and then mark each row as read as they open them to read them, correct?
    – mtindall89
    Mar 27, 2014 at 0:34
  • 8
    @TroubleZero Oxford begs to differ :P oxforddictionaries.com/definition/english/bajillion
    – Kevin Pei
    Jan 30, 2016 at 19:11
  • 2
    @livewire1407 or you can just insert the message in table 1 and then create the table 2 record only when user sees the message.
    – Sal Celli
    Sep 21, 2018 at 11:00
  • @KevinPei can you please check this one stackoverflow.com/questions/75220565/…
    – user19534405
    Jan 24, 2023 at 11:06
-3

I can do one thing, if no problem with one user or 100 of user, you create one column named readUnread with more than 63,999 Characters in which you use put every user your message with 0 and 1 assign like {jeff:0,kevin:1,Sal:0} when read update from 0 to 1 and when you open this on the screen, split it with current user and ";", this will help you (this is the logic which inhance your performance).

1
  • 1
    There are way more efficient ways to solve this. Storing all in one column actually will have a huge performance drain as for every lookup, some parsing and/or plaintext searching is needed. For every record. It would be a way better idea to add for example a pivot table to store just the ids of users who read the message. This allows to profit from mechanisms like indexing, reliable query caching, and so on. See stackoverflow.com/a/22675865/1248282 for better idea.
    – mvmoay
    Nov 23, 2022 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.