- design-seckill.md: 秒杀系统设计 - design-shorturl.md: 短链接系统设计 - design-lbs.md: LBS附近的人系统设计 - design-im.md: 即时通讯系统设计 - design-feed.md: 社交信息流系统设计 Each document includes: - Requirements analysis and data volume assessment - Technical challenges - System architecture design - Database design - Caching strategies - Scalability considerations - Practical project experience - Alibaba P7 level additional points Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
17 KiB
17 KiB
社交信息流系统设计
需求分析和数据量评估
需求分析
- 核心功能:信息流展示、个性化推荐、社交互动、内容管理
- 业务场景:微博、朋友圈、抖音等社交应用
- QPS评估:日请求100亿+,峰值QPS 20万+
- 数据规模:用户5亿+,内容100亿+,关系数据1000亿+
数据量评估
- 用户表:5亿条,日均查询1亿次
- 内容表:100亿条,日增1亿+
- 关系表:1000亿+条,日均更新10亿次
- 互动表:500亿+条,日增5亿+
- 推荐系统:日处理1000亿次推荐请求
核心技术难点
1. 海量内容处理
- 亿级内容的存储和检索
- 内容的实时分发和推送
- 内容的审核和过滤
2. 个性化推荐
- 用户兴趣建模
- 实时推荐算法
- 推荐效果评估
3. 高并发读取
- 信息流的实时性要求
- 用户关系计算
- 数据缓存优化
4. 社交图谱构建
- 用户关系网络
- 关系强度计算
- 图算法优化
系统架构设计
总体架构
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 移动端APP │ │ Web网页 │ │ API接口 │
│ (iOS/Android)│ │ (PC/移动) │ │ (第三方) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
└───────────────────────┼───────────────────────┘
│
┌─────────────────────┼───────────────────────┐
│ │ │
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 负载均衡 │ │ API网关 │ │ CDN加速 │
│ (Nginx) │ │ (Gateway) │ │ (Edge) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
┌─────────────────────┼───────────────────────┐
│ │ │
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Feed服务 │ │ 互动服务 │ │ 推荐服务 │
│ (微服务) │ │ (微服务) │ │ (微服务) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
└─────────────────────┼───────────────────────┘
│
┌─────────────────────┼───────────────────────┐
│ │ │
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 缓存集群 │ │ 数据库集群 │ │ 消息队列 │
│ (Redis) │ │ (MySQL分库分表)│ │ (Kafka) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
┌─────────────────────┼───────────────────────┐
│ │ │
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 搜索引擎 │ │ 图数据库 │ │ 数据仓库 │
│ (Elasticsearch)│ │ (Neo4j) │ │ (ClickHouse) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
关键组件
1. 流量层
- 负载均衡:Nginx L7负载均衡
- API网关:请求路由、限流、认证
- CDN加速:静态资源缓存
2. 服务层
- Feed服务:信息流生成和分发
- 互动服务:点赞、评论、分享处理
- 推荐服务:个性化推荐算法
- 社交服务:关系管理
3. 存储层
- Redis集群:缓存、计数器
- MySQL集群:用户数据、内容数据
- MongoDB集群:Feed流数据
- 图数据库:社交关系数据
4. 基础设施
- 搜索引擎:内容全文检索
- 消息队列:异步处理、削峰填谷
- 数据仓库:离线数据分析
- 推荐引擎:机器学习平台
数据库设计
用户表
CREATE TABLE `feed_user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint NOT NULL COMMENT '用户ID',
`username` varchar(50) NOT NULL COMMENT '用户名',
`nickname` varchar(50) NOT NULL COMMENT '昵称',
`avatar` varchar(255) DEFAULT NULL COMMENT '头像',
`gender` tinyint DEFAULT 0 COMMENT '性别',
`birthday` date DEFAULT NULL COMMENT '生日',
`location` varchar(100) DEFAULT NULL COMMENT '位置',
`bio` varchar(255) DEFAULT NULL COMMENT '个人简介',
`interests` json DEFAULT NULL COMMENT '兴趣标签',
`follow_count` int NOT NULL DEFAULT 0 COMMENT '关注数',
`follower_count` int NOT NULL DEFAULT 0 COMMENT '粉丝数',
`post_count` int NOT NULL DEFAULT 0 COMMENT '发布数',
`status` tinyint NOT NULL DEFAULT 1 COMMENT '状态',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
内容表
CREATE TABLE `feed_content` (
`id` bigint NOT NULL AUTO_INCREMENT,
`content_id` varchar(64) NOT NULL COMMENT '内容ID',
`user_id` bigint NOT NULL COMMENT '用户ID',
`content_type` tinyint NOT NULL COMMENT '内容类型',
`title` varchar(255) DEFAULT NULL COMMENT '标题',
`content` text NOT NULL COMMENT '内容',
`media_urls` json DEFAULT NULL COMMENT '媒体URLs',
`tags` json DEFAULT NULL COMMENT '标签',
`like_count` int NOT NULL DEFAULT 0 COMMENT '点赞数',
`comment_count` int NOT NULL DEFAULT 0 COMMENT '评论数',
`share_count` int NOT NULL DEFAULT 0 COMMENT '分享数',
`view_count` int NOT NULL DEFAULT 0 COMMENT '浏览数',
`status` tinyint NOT NULL DEFAULT 1 COMMENT '状态',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_content_id` (`content_id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_created_at` (`created_at`),
FULLTEXT KEY `idx_content` (`title`, `content`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
用户关系表
CREATE TABLE `feed_relationship` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint NOT NULL COMMENT '用户ID',
`follow_user_id` bigint NOT NULL COMMENT '关注用户ID',
`relation_type` tinyint NOT NULL DEFAULT 1 COMMENT '关系类型',
`strength` decimal(5,2) DEFAULT '0.00' COMMENT '关系强度',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_follow` (`user_id`, `follow_user_id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_follow_user_id` (`follow_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Feed流表
CREATE TABLE `feed_stream` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint NOT NULL COMMENT '用户ID',
`content_id` varchar(64) NOT NULL COMMENT '内容ID',
`feed_type` tinyint NOT NULL COMMENT 'Feed类型',
`priority` decimal(10,4) NOT NULL DEFAULT '0.0000' COMMENT '优先级',
`is_read` tinyint NOT NULL DEFAULT 0 COMMENT '是否已读',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_priority` (`priority`),
KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
互动记录表
CREATE TABLE `feed_interaction` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_id` bigint NOT NULL COMMENT '用户ID',
`target_user_id` bigint DEFAULT NULL COMMENT '目标用户ID',
`content_id` varchar(64) DEFAULT NULL COMMENT '内容ID',
`interaction_type` tinyint NOT NULL COMMENT '互动类型',
`content` text DEFAULT NULL COMMENT '互动内容',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_target_user_id` (`target_user_id`),
KEY `idx_content_id` (`content_id`),
KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
缓存策略
Redis缓存设计
// 用户信息缓存
const USER_INFO_PREFIX = 'user:';
const USER_INFO_TTL = 3600; // 1小时
// 内容信息缓存
const CONTENT_INFO_PREFIX = 'content:';
const CONTENT_INFO_TTL = 86400; // 24小时
// Feed流缓存
const FEED_STREAM_PREFIX = 'feed:';
const FEED_STREAM_TTL = 1800; // 30分钟
// 互动计数器
const INTERACTION_PREFIX = 'interaction:';
const INTERACTION_TTL = 300; // 5分钟
// 推荐结果缓存
const RECOMMEND_PREFIX = 'recommend:';
const RECOMMEND_TTL = 300; // 5分钟
// 社交关系缓存
const RELATION_PREFIX = 'relation:';
const RELATION_TTL = 3600; // 1小时
缓存策略
-
多级缓存:
- 本地缓存:Caffeine
- 分布式缓存:Redis Cluster
- CDN缓存:静态资源
-
缓存更新策略:
- Write Behind:异步更新
- Refresh Ahead:预加载热点数据
- Cache Invalidation:定时失效
-
Feed流缓存:
- 分页缓存
- 用户个性化缓存
- 实时更新策略
Feed流生成算法
public class FeedGenerator {
// 基于时间线的基础Feed
public List<FeedItem> generateTimelineFeed(String userId, int offset, int limit) {
// 从Redis获取用户关注列表
List<String> followUsers = getFollowUsers(userId);
// 获取关注用户的最新内容
List<FeedItem> feeds = new ArrayList<>();
for (String followUser : followUsers) {
List<FeedItem> userFeeds = getUserRecentFeeds(followUser, offset, limit / followUsers.size());
feeds.addAll(userFeeds);
}
// 按时间排序
return feeds.stream()
.sorted(Comparator.comparing(FeedItem::getCreatedAt).reversed())
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
}
// 个性化推荐Feed
public List<FeedItem> generateRecommendFeed(String userId, int offset, int limit) {
// 获取用户兴趣标签
List<String> interests = getUserInterests(userId);
// 基于协同过滤推荐内容
List<String> recommendContentIds = collaborativeFiltering(userId, interests);
// 基于内容的推荐
List<String> contentBasedRecommend = contentBasedRecommend(userId);
// 合并推荐结果
Set<String> allRecommend = new HashSet<>();
allRecommend.addAll(recommendContentIds);
allRecommend.addAll(contentBasedRecommend);
// 获取推荐内容详情
List<FeedItem> feeds = getContentDetails(new ArrayList<>(allRecommend));
// 排序并返回
return feeds.stream()
.sorted(Comparator.comparing(FeedItem::getScore).reversed())
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
}
// 混合Feed流
public List<FeedItem> generateHybridFeed(String userId, int offset, int limit) {
List<FeedItem> timelineFeeds = generateTimelineFeed(userId, 0, limit / 2);
List<FeedItem> recommendFeeds = generateRecommendFeed(userId, 0, limit / 2);
// 合并并去重
Set<String> seenContentIds = new HashSet<>();
List<FeedItem> result = new ArrayList<>();
for (FeedItem feed : timelineFeeds) {
if (!seenContentIds.contains(feed.getContentId())) {
result.add(feed);
seenContentIds.add(feed.getContentId());
}
}
for (FeedItem feed : recommendFeeds) {
if (!seenContentIds.contains(feed.getContentId())) {
result.add(feed);
seenContentIds.add(feed.getContentId());
}
}
// 按权重排序
return result.stream()
.sorted(Comparator.comparing(FeedItem::getScore).reversed())
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
}
}
扩展性考虑
1. 水平扩展
- 无状态服务:Feed服务、推荐服务无状态化
- 数据分片:按用户ID分片
- 读写分离:主库写入,从库读取
2. 垂直扩展
- 服务拆分:Feed服务、推荐服务、互动服务
- 数据分层:热数据、温数据、冷数据
- 多级缓存:本地、Redis、CDN
3. 推荐算法扩展
- 实时推荐:基于实时行为更新
- 离线推荐:批量处理推荐结果
- 冷启动:新用户推荐策略
4. 容灾备份
- 多活架构:多机房部署
- 故障转移:自动故障检测和转移
- 数据备份:定时备份和实时同步
实际项目经验
1. 技术栈选择
- 前端:React + TypeScript + Mobile
- 后端:Spring Boot + Node.js + Python
- 数据库:MySQL + MongoDB + Redis
- 消息队列:Kafka + Pulsar
- 搜索引擎:Elasticsearch + ClickHouse
2. 性能优化
- Feed缓存:多级缓存策略
- 数据库优化:分库分表、索引优化
- 算法优化:推荐算法优化
- 网络优化:HTTP/2、Keep-Alive
3. 运维部署
- 容器化:Docker + Kubernetes
- CI/CD:Jenkins + GitLab
- 监控告警:ELK Stack + AlertManager
- 压测:JMeter + Locust
4. 安全设计
- 内容安全:内容审核、过滤
- 用户隐私:数据脱敏、权限控制
- 防刷机制:频率限制、行为分析
阿里P7加分项
1. 架构设计能力
- 高可用架构:99.99%可用性
- 高性能架构:支持亿级QPS
- 扩展性架构:弹性扩缩容
2. 技术深度
- 推荐系统:协同过滤、深度学习
- 实时计算:Flink、Kafka Streams
- 图算法:社交图谱、关系计算
3. 业务理解
- 社交业务:理解社交网络特性
- 用户行为:分析用户互动模式
- 内容生态:掌握内容分发逻辑
4. 团队管理
- 技术团队:带领50人+团队
- 项目管控:管理亿级用户项目
- 技术方案:主导架构设计
5. 前沿技术
- AI应用:智能推荐、内容生成
- 边缘计算:边缘节点处理
- Serverless:函数化服务
面试常见问题
1. 如何生成个性化信息流?
- 用户画像:用户兴趣建模
- 推荐算法:协同过滤、基于内容
- 实时更新:实时行为追踪
- 多目标优化:点击率、停留时间
2. 如何处理高并发Feed请求?
- 缓存策略:多级缓存
- 数据预加载:Feed预生成
- 异步处理:异步更新
- 分页优化:游标分页
3. 如何保证Feed流实时性?
- 实时推送:WebSocket推送
- 增量更新:实时增量计算
- 事件驱动:事件总线
- 缓存预热:热点数据预热
4. 如何实现推荐算法?
- 协同过滤:用户行为相似度
- 内容分析:内容特征提取
- 深度学习:神经网络模型
- 多臂老虎机:探索与利用
5. 如何处理海量数据?
- 分库分表:按用户ID分片
- 数据归档:冷热数据分离
- 缓存优化:热点数据缓存
- 计算优化:批量处理、并行计算