mardi 28 mai 2019

How to design an efficient architecture for a simple news feed in php/mysql?

To make it short : I have come to 2 solutions while thinking about the best way to make a simple news feed for a big audience website. I'll expose the 2 solutions but most of all, I'm posting them here to have the most opinions about them and mostly to know if you would have a better way to set this up. So I'm very open to all feedbacks/advices.

The details :

In the database, there is 3 tables : User, Author and Books. The users can suscribe to authors and authors are writing the books. So this is what it looks like.

db_architecture_1

What is needed is a simple news feed : users would be displayed a list of the last books published by the authors they suscribed to. Like this: *

  • Author45 wrote a new book : Book756Title
  • Author12 wrote a new book : Book45Title
  • Author 76 wrote a new book : Book28Title
  • ...

*

The solutions :

  • The first solution i that came in my mind : When loading the news, just make a simple SELECT that joins all the concerned tables and order by the publishing date.

    The problem is that, though it's the simplest and quickest solution, i couldn't stop thinking that it is a pretty heavy query which would affect a lot the application performance when executed by a large audience.

  • Then comes the second solution, which needs a little more explanations :

For the website system, there are 2 servers : the public server which hosts the public website with the main database, and a second server which has a backup/update database. The second server is not accessible publicly but both server are synchronized everyday.

So all new author and books insertions are made on this one whereas user suscribtions and all user activities are done on the public server.

Sooo the second solution i found, and the best one in terms of performance is to add a new table Notification, and set up a mysql trigger on the Publication table. The trigger, after every new publishing is inserted, would select all users who suscribed to the author and insert form them new entries on the notification table.

This way, the only remaining task on the public server would be to select the new notifications of the user and display them.

I have made a test for that trigger for 10 000 users suscribtion and i think mysql reacted extremely fast.


So now, even though the second solution is the winner, I came to expose both to you, in order to have the maximum of opinions about this optimization question.

Thank you all for reading and waiting for your answers.




Aucun commentaire:

Enregistrer un commentaire