Home Backend Development PHP Tutorial 微博关注是根据什么来知道你关注小弟我,小弟我关注你了?数据库如何设计

微博关注是根据什么来知道你关注小弟我,小弟我关注你了?数据库如何设计

Jun 13, 2016 am 11:11 AM
mongo mysql nbsp redis

微博关注是根据什么来知道你关注我,我关注你了?数据库怎么设计?
如题,我想做个微博,但是关注这快 该怎么操作 用php写。。


------解决方案--------------------
表结构 估计像双链表
------解决方案--------------------
建一张表,两个字段:
关注者id
被关注者id
------解决方案--------------------
我想微博这么火,应该是赶上智能手机发展的时机了 呵呵
------解决方案--------------------
mysql的话,就不说了。这些都属于热数据,如果真的是具备一定规模的微博,查mysql直接死翘翘了。

用redis的话,用n个list或sets或hash table+list。名字为前缀+被关注者id,内容就是关注者的id列表或集合。类似:

owner:1 = {3,1,5,8,12,64...}
owner:2 = {32,56,22,11,4...}
...


其实最难的设计点在于被关注者发一条微博,而他的所有粉丝需要收到这个消息。楼主想过这个如何实现嘛?
尤其是一个明星,他有上百万上千万粉丝。解决方案有两个思路:

1 由被关注者主动推数据
2 由被关注者向粉丝推送一个通知,然后由粉丝去拉数据

不过这样就意味着他发一条消息需要有千万个人来访问这张消息表或发一条消息需要写向千万个粉丝的消息表写数据。

由于redis的数据结构过于简单,所以完全用其建表虽然可以实现但确实非常麻烦,其实用mongo比较合适。

------解决方案--------------------
引用:
其实最难的设计点在于被关注者发一条微博,而他的所有粉丝需要收到这个消息。楼主想过这个如何实现嘛?
尤其是一个明星,他有上百万上千万粉丝。解决方案有两个思路:

1 由被关注者主动推数据
2 由被关注者向粉丝推送一个通知,然后由粉丝去拉数据

不过这样就意味着他发一条消息需要有千万个人来访问这张消息表或发一条消息需要写向千万个粉丝的消息表写数据。

由于redis的数据结构过于简单,所以完全用其建表虽然可以实现但确实非常麻烦,其实用mongo比较合适。


这个问题我一直想不明白,只有膜拜的份了
------解决方案--------------------
引用:
引用:其实最难的设计点在于被关注者发一条微博,而他的所有粉丝需要收到这个消息。楼主想过这个如何实现嘛?
尤其是一个明星,他有上百万上千万粉丝。解决方案有两个思路:

1 由被关注者主动推数据
2 由关注者去被关注者的消息表拉数据

不过这样就意味着他发一条消息需要有千万个人来访问这张消息表或发一条消息需……


其实这个我也是看新浪微博的架构师说的,他只说了大概思路,我是根据他的思路联想存储结构如何设计,所以不一定完全正确。

第一种方案,应该是每个人都有一张自己的消息表。当被关注者发消息时,会将此消息写入关注者的消息表中,内容大概有被关注者id、消息内容、发送时间。这里最大的问题在于要向千万张表写数据。

第二种方案,每个人的消息只存储在自己的消息表中,当自己发消息后,写入。然后由其所有关注者定时从这张表中取数据。或者当自己发消息后,向所有关注者发一个通知,比如发个1,关注者就来自己的消息表取数据。这种方法当某人粉丝数量很多时,会造成这张表的并发读操作非常高。

简单的看,两种方案都有利弊。但都还有很大优化空间。新浪微博两种方案都用过。并且在这过程中也摸索出了一些经验。

第一种方案他们采取过分批推送的策略,会将用户按活跃度划分几个等级,推送顺序是按照用户活跃度等级来决定的。分批推送一定程度上减轻了负担。

第二种方案可以采用冗余多份数据负载均衡的办法将那一张表的并发读操作均衡开。比如我有一张消息表,但这张消息表存储n份,在n台服务器上,内容完全一致。当我发消息时同时向这几台服务器的表写数据,或者分批写入,然后我不同的粉丝,会根据一定策略来决定去哪台服务器读。这里也可以将用户活跃度作为参数,活跃度高的粉丝去服务器a读(服务器a中的消息表在分批写入时最优先被写入)

想来想去,方案似乎就两种,但可优化的地方还很多,例如在读取数据时,加入cache层,cache层只存储每个用户最近发表的消息,数据定期归档。
------解决方案--------------------
我觉得这个真正难的地方是服务器架构设计,而不是实现方法。再优秀的方法,也无法应对日益增大的数据量,只有调整服务器架构,将压力均衡开,才是长期发展之路。
------解决方案--------------------
你就不能说点让人明白的吗?
引用:
引用:引用:其实最难的设计点在于被关注者发一条微博,而他的所有粉丝需要收到这个消息。楼主想过这个如何实现嘛?
尤其是一个明星,他有上百万上千万粉丝。解决方案有两个思路:

1 由被关注者主动推数据
2 由关注者去被关注者的消息表拉数据

不过这样就意味着他发一条消息需要有千万个人来访……

------解决方案--------------------
引用:
你就不能说点让人明白的吗?引用:引用:引用:其实最难的设计点在于被关注者发一条微博,而他的所有粉丝需要收到这个消息。楼主想过这个如何实现嘛?
尤其是一个明星,他有上百万上千万粉丝。解决方案有两个思路:

1 由被关注者主动推数据
2 由关注者去被……

跑题了
------解决方案--------------------
你们想的太复杂了,确实如此而已:
引用:
建一张表,两个字段:
关注者id
被关注者id

------解决方案--------------------
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Redis's Role: Exploring the Data Storage and Management Capabilities Redis's Role: Exploring the Data Storage and Management Capabilities Apr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Redis: Understanding Its Architecture and Purpose Redis: Understanding Its Architecture and Purpose Apr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

See all articles