Im creating an e-commerce backend where in each of my product has the following counter attributes
- product views
- product likes
- product comments count
The current database columns which I have for the product database table is
- id
- likes_count
- views_count
- comments_count
- category_id
- category_parent_id
- category_sub_parent_id
- handling_charge
- shipping_charge
- meetup_address
- is_additional_fields
- status
- is_deleted
- created_at
- updated_at
As seen in the following blog Wanelo engineering blog implementing a counter which can frequently get updated on a single row would cause row lock on innodb which if get frequently updated can cause dead lock situations in the application. But the solution for this is pretty much explained in blog which I got an idea about. But what if there are multiple counters associated with a single product which can get updated simultaneously when the app grows. How should I be designing the database table for the counters. Should I have to maintain separate tables viz
likes counter table
- id - product_id - count
views counter table
- id - product_id - count
comments counter table
- id - product_id - count
By maintaining separate table even if a simultaneous update comes for a product (like+comment+view) it will be updated separately and reduces chance of row dead lock situations. If its in a single table and if the updates for all of it comes simultaneously it can cause an issue.
Question: Is there any better way in which I could design the tables for a counter? Any suggestions please?
A counter in the product table for views is fine.
A separate table for likes with columns like (product_id, user_id) so each user can only like a product once. Otherwise they would be able to mash like if it was just a simple counter.
A separate table for comments with columns such as (product_id, comment_text, date.. etc)
Is this what you're asking?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With