Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
53 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
title: 秒杀系统 | ||
date: 2020-03-16 | ||
draft: false | ||
categories: architecture | ||
--- | ||
|
||
## 问题场景 | ||
|
||
在进行系统设计的过程中,首先问题场景的特点。秒杀系统是十分典型的高并发场景,其特点也十分显著:高并发、低库存、高瞬时流量。再者分析整个系统的输入输出,即大概的 API 网关拥有的功能:查(用户查询商品信息)、改(用户购买商品)。将系统的特点和功能分析完毕后,就可以根据这些信息进行系统设计。一个常规的秒杀系统从前到后,依次有: | ||
|
||
```log | ||
前端页面 -> 代理服务 -> 后端服务 -> 数据库 | ||
``` | ||
|
||
根据这个流程,一般优化设计思路:将 **请求拦截在系统上游,降低下游压力**。在一个并发量大,实际需求小的系统中,应当尽量在前端拦截无效流量,降低下游服务器和数据库的压力,不然很可能造成数据库读写锁冲突,甚至导致死锁,最终请求超时。 | ||
|
||
整体优化手段包含:**缓存**、**限流**、**削峰(MQ)**、**异步处理**、**降级**、**熔断**、**SET化**、**快速扩容** | ||
|
||
|
||
## 前端页面 | ||
|
||
- 资源静态化:将活动页面上的所有可以静态的元素全部静态化,尽量减少动态元素;通过CDN缓存静态资源,来抗峰值。 | ||
- 禁止重复提交:用户提交之后按钮置灰,禁止重复提交 | ||
- URL动态化:防止恶意抓取 | ||
|
||
## 代理服务 | ||
|
||
利用负载均衡(例如 Nginx 等)使用多个服务器并发处理请求,减小服务器压力。 | ||
|
||
## 后端服务 | ||
|
||
- 用户限流:在某一时间段内只允许用户提交一次请求,比如可以采取 IP 限流 | ||
- 业务拆分 | ||
- 利用 MQ 削峰 | ||
- 利用缓存应对大量查询请求 | ||
- 利用缓存应对写请求(注意数据一致性、持久性问题):缓存也是可以应对写请求的,可把数据库中的库存数据转移到 Redis 缓存中,所有减库存操作都在 Redis 中进行,然后再通过后台进程把Redis中的用户秒杀请求同步到数据库中。 | ||
|
||
## 数据库 | ||
|
||
- 多数据库:防止数据热点问题 | ||
- 优化 SQL 防止死锁 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters