问题
We have a web app which requires to send lots of notifications to users (over 1,000,000
notifications per day). We use Laravel and MySQL for the database.
I loop through a group of users send notifications and save it to the database. Let's say if I want to send a group of 1000
users. The data will be written to the DB 1000
time. As I said, we have over 1,000,000
notifications per day, which take so many resources.
What's the appropriate way to approach this problem?
I should change to a new database system like MongoDB or I should redesign the notification table schema and the way I save to DB?
The following is my notification table schema.
PS:: I need to save every notification to DB and need to show to users. Can't skip this part.
回答1:
If you're sending the same content for all users, I would suggest the following approach.
DATABASE SCHEMA
notification_contents (id, content, created_by, created_at, updated_at)
user_notifications (id, user_id, notification_id, status, created_at, updated_at)
When there is a new notification is to be sent, Insert it into the notification_contents table. You can use placeholders in your content if you want to customize the content. For example Username or email
Then loop through the users and insert them into the user_notifications
table with the user_id
and notification_id
. Initially set a status
flag as zero if you want to implement a queuing mechanism for this.
Then write a cron job to take x
number of users from the user_notifications
table and send the content to them. You can join user_notifications with notification_contents
to get the content for email.
Set the status
flag to one once the notification is sent.
回答2:
Shrink the dataset.
- Use ascii for UUIDs. Do you really need a UUID?
- Compress UUIDs into
BINARY(16)
. (This won't completely solve the scalability nightmare that UUIDs lead to, but it will help some.) - Use ENUM for types and status, not long VARCHARs.
- What is the difference between the two "types"?
- Do you need 8-byte BIGINT?
- There are 3 TIMESTAMPs, do you need more than one?
I don't see anything like user_id
??
来源:https://stackoverflow.com/questions/54303977/reduce-database-write-on-notification-system-or-change-approbate-database