I want to do private chat service. Now I have a relation data base (PostgreSQL) for storing my messages and threads (threads - private rooms between users).
I have following tables:
1) Message: id, text, datetime, sender_id, thread_id, is_read
2) Thread: id
3) Thread_Participants: thread_id, user_id
Message table is connected with Thread by Many_to_Many relations through Thread_Participants table.
By my architecture, users exchange messages through WebSocets and Pub/Sub of Redis database.
But in my opinion I need to store messages in relation database, it is better for safety. Maybe I wrong.
But I have a problem with getting the history of user's threads. I would like to request (relation DB) following fields:
thread_id,
last_message_in_this_thread,
count_of_messages_in_this_thread,
datetime_of_last_message_in_this_thread,
username_of_last_message_in_this_thread
But such request means quite hard and slow request... How can I get it with the fast way? Or maybe there is another way or architecture of message?