Databases 7 min read

Designing Like/Comment/Favorite Features: MySQL and Redis Schemas with Consistency Considerations

This article explains the database design for likes, comments, and favorites in apps, comparing MySQL table structures and Redis storage solutions, discussing query patterns, scalability challenges, and data consistency measures for moderate to massive traffic scenarios.

Architecture Digest
Architecture Digest
Architecture Digest
Designing Like/Comment/Favorite Features: MySQL and Redis Schemas with Consistency Considerations

Likes, comments, and favorites are basic features in app development; this article discusses their database design requirements and presents practical solutions.

Requirements: display like count, check if a user has liked (deduplication), show personal like list, and show article like list.

MySQL solution: use separate tables for posts, users, and likes. Example DDL statements:

-- 文章表
create table post {
    post_id int(11) NOT NULL AUTO_INCREMENT,
    ...
    star_num int(11) COMMENT '点赞数量'
}

-- 用户表
create table user {
    user_id int(11) NOT NULL AUTO_INCREMENT,
    ...
    star_num int(11) COMMENT '点赞数量'
}

-- 点赞表
create table star {
    id int(11) NOT NULL AUTO_INCREMENT,
    post_id,
    user_id,
    ...
}

Common queries:

select post_id from star where user_id=? // posts liked by a user

select user_id from star where post_id=? // users who liked a post

This design works for data under tens of millions but faces scaling problems when the table grows, requiring sharding or redundant tables.

Redis solution: for large‑scale data, combine Redis with MySQL. Redis can act as cache or primary storage. Example commands for different scenarios:

Scenario a – store like count as a string:

// set initial count
127.0.0.1:6379[2]> set star:tid:888 898
OK
// increment atomically
127.0.0.1:6379[2]> incr star:tid:888
(integer) 899

Scenario b – deduplication using a set:

// add user id to article's like set
127.0.0.1:6379[2]> sadd star:list:tid:888 123 456 789
(integer) 3
// check if user has liked
127.0.0.1:6379[2]> sismember star:list:tid:888 456
(integer) 1

Other structures such as hash or sorted set can reduce global keys but introduce routing complexity; the choice depends on specific needs.

When using Redis as storage, enable both RDB and AOF for persistence, monitor memory usage, and be aware of consistency issues that arise from data copies.

The article concludes that there is no universal solution; developers must select the appropriate design based on traffic volume, consistency requirements, and resource constraints.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

redismysqlDatabase designlikes
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.