文章目录
-
- 一、 MySQL:稳如磐石的库表设计
-
- 1.1 库表设计原则
- 1.2 索引设计(B+树的艺术)
- 1.3 设计示例与可视化
- 二、 Elasticsearch:海量数据的索引设计
-
- 2.1 索引与 Mapping 设计
- 2.2 数据同步流程
- 三、 Redis:极速缓存的 Key 设计
-
- 3.1 Key 设计规范
- 3.2 数据结构选型思维导图
- 四、 RabbitMQ:异步解耦的 Topic 设计
-
- 4.1 Exchange 与 Topic 设计
- 4.2 消息模型时序图
- 综合架构示意图
在现代高并发、分布式的后端架构中,单一的数据库早已无法满足所有需求。MySQL 负责核心事务与数据持久化,Elasticsearch (ES) 解决海量数据的复杂搜索与分析,Redis 承担极速缓存与计数,而 RabbitMQ (RMQ) 则负责系统解耦与削峰填谷。
本文将深入探讨这四大组件在“库表、索引、Key、Topic”层面的设计规范与最佳实践。
一、 MySQL:稳如磐石的库表设计
MySQL 是数据的“最后一道防线”。设计的目标是:保证数据一致性、扩展性及查询效率。
1.1 库表设计原则
- 命名规范:使用小写字母、下划线分隔(snake_case)。如 db_order、t_order_detail。
- 字符集:无脑选择 utf8mb4,兼容 Emoji 和生僻字。
- 主键策略:
- 尽量避免 UUID(导致聚簇索引碎片)。
- 推荐使用自增BIGINT,既有序又保证全局唯一。
- 字段类型:
- 金额使用 DECIMAL,禁止用 FLOAT/DOUBLE。
- 状态值用 TINYINT,并备注状态含义(如 0:待支付 1:已支付)。
- 时间统一使用 DATETIME 或 BIGINT (时间戳)。
1.2 索引设计(B+树的艺术)
- 最左前缀原则:联合索引 (a, b, c),查询 (a, b) 走索引,查询 (b, c) 不走。
- 覆盖索引:查询字段尽量包含在索引中,避免回表(Select * 是大忌)。
1.3 设计示例与可视化
#mermaid-svg-idvxcZZgGzrTSu7I{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-idvxcZZgGzrTSu7I .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-idvxcZZgGzrTSu7I .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-idvxcZZgGzrTSu7I .error-icon{fill:#552222;}#mermaid-svg-idvxcZZgGzrTSu7I .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-idvxcZZgGzrTSu7I .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-idvxcZZgGzrTSu7I .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-idvxcZZgGzrTSu7I .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-idvxcZZgGzrTSu7I .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-idvxcZZgGzrTSu7I .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-idvxcZZgGzrTSu7I .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-idvxcZZgGzrTSu7I .marker{fill:#333333;stroke:#333333;}#mermaid-svg-idvxcZZgGzrTSu7I .marker.cross{stroke:#333333;}#mermaid-svg-idvxcZZgGzrTSu7I svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-idvxcZZgGzrTSu7I p{margin:0;}#mermaid-svg-idvxcZZgGzrTSu7I g.classGroup text{fill:#9370DB;stroke:none;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-idvxcZZgGzrTSu7I g.classGroup text .title{font-weight:bolder;}#mermaid-svg-idvxcZZgGzrTSu7I .nodeLabel,#mermaid-svg-idvxcZZgGzrTSu7I .edgeLabel{color:#131300;}#mermaid-svg-idvxcZZgGzrTSu7I .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-idvxcZZgGzrTSu7I .label text{fill:#131300;}#mermaid-svg-idvxcZZgGzrTSu7I .labelBkg{background:#ECECFF;}#mermaid-svg-idvxcZZgGzrTSu7I .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-idvxcZZgGzrTSu7I .classTitle{font-weight:bolder;}#mermaid-svg-idvxcZZgGzrTSu7I .node rect,#mermaid-svg-idvxcZZgGzrTSu7I .node circle,#mermaid-svg-idvxcZZgGzrTSu7I .node ellipse,#mermaid-svg-idvxcZZgGzrTSu7I .node polygon,#mermaid-svg-idvxcZZgGzrTSu7I .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-idvxcZZgGzrTSu7I .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I g.clickable{cursor:pointer;}#mermaid-svg-idvxcZZgGzrTSu7I g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-idvxcZZgGzrTSu7I g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-idvxcZZgGzrTSu7I .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-idvxcZZgGzrTSu7I .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-idvxcZZgGzrTSu7I .dashed-line{stroke-dasharray:3;}#mermaid-svg-idvxcZZgGzrTSu7I .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-idvxcZZgGzrTSu7I #compositionStart,#mermaid-svg-idvxcZZgGzrTSu7I .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I #compositionEnd,#mermaid-svg-idvxcZZgGzrTSu7I .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I #dependencyStart,#mermaid-svg-idvxcZZgGzrTSu7I .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I #dependencyStart,#mermaid-svg-idvxcZZgGzrTSu7I .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I #extensionStart,#mermaid-svg-idvxcZZgGzrTSu7I .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I #extensionEnd,#mermaid-svg-idvxcZZgGzrTSu7I .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I #aggregationStart,#mermaid-svg-idvxcZZgGzrTSu7I .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I #aggregationEnd,#mermaid-svg-idvxcZZgGzrTSu7I .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I #lollipopStart,#mermaid-svg-idvxcZZgGzrTSu7I .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I #lollipopEnd,#mermaid-svg-idvxcZZgGzrTSu7I .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-idvxcZZgGzrTSu7I .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-idvxcZZgGzrTSu7I .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-idvxcZZgGzrTSu7I .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-idvxcZZgGzrTSu7I .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-idvxcZZgGzrTSu7I :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
has
1
n
User
+bigint id PK
+varchar username
+varchar mobile UK
+datetime create_time
Order
+bigint id PK
+bigint user_id IDX
+decimal amount
+tinyint status
SQL 源码示例:
CREATE TABLE `t_order_base` (
`id` BIGINT UNSIGNED NOT NULL COMMENT '主键ID(雪花算法)',
`user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
`order_no` VARCHAR(64) NOT NULL COMMENT '业务订单号',
`total_amount` DECIMAL(10, 2) NOT NULL DEFAULT '0.00' COMMENT '订单总金额',
`status` TINYINT UNSIGNED NOT NULL DEFAULT '0' COMMENT '状态:0-待支付, 10-已支付, 20-已发货',
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_order_no` (`order_no`),
INDEX `idx_user_status` (`user_id`, `status`) COMMENT '用户状态联合索引'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单基础表';
二、 Elasticsearch:海量数据的索引设计
ES 是基于 Lucene 的分布式搜索引擎,核心在于 倒排索引。设计重心在于 Mapping 结构 和 分片策略。
2.1 索引与 Mapping 设计
- 索引命名:业务_模块_版本,例如 mall_goods_v1。通常配合 Alias(别名)使用,实现零停机索引切换。
- Keyword vs Text:
- Keyword:不分词,用于精确匹配(ID、状态)、聚合、排序。
- Text:分词,用于全文检索(商品名称、文章内容)。
- 分片 (Shards):
- 单个分片建议控制在 30GB – 50GB。
- 一旦创建,主分片数不可改(除非 Reindex)。
2.2 数据同步流程
MySQL 到 ES 的数据同步是常见场景,通常采用 Canal 监听 Binlog。
#mermaid-svg-2iMEMzq1Vy0PS2zw{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-2iMEMzq1Vy0PS2zw .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-2iMEMzq1Vy0PS2zw .error-icon{fill:#552222;}#mermaid-svg-2iMEMzq1Vy0PS2zw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2iMEMzq1Vy0PS2zw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2iMEMzq1Vy0PS2zw .marker.cross{stroke:#333333;}#mermaid-svg-2iMEMzq1Vy0PS2zw svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2iMEMzq1Vy0PS2zw p{margin:0;}#mermaid-svg-2iMEMzq1Vy0PS2zw .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-2iMEMzq1Vy0PS2zw .cluster-label text{fill:#333;}#mermaid-svg-2iMEMzq1Vy0PS2zw .cluster-label span{color:#333;}#mermaid-svg-2iMEMzq1Vy0PS2zw .cluster-label span p{background-color:transparent;}#mermaid-svg-2iMEMzq1Vy0PS2zw .label text,#mermaid-svg-2iMEMzq1Vy0PS2zw span{fill:#333;color:#333;}#mermaid-svg-2iMEMzq1Vy0PS2zw .node rect,#mermaid-svg-2iMEMzq1Vy0PS2zw .node circle,#mermaid-svg-2iMEMzq1Vy0PS2zw .node ellipse,#mermaid-svg-2iMEMzq1Vy0PS2zw .node polygon,#mermaid-svg-2iMEMzq1Vy0PS2zw .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2iMEMzq1Vy0PS2zw .rough-node .label text,#mermaid-svg-2iMEMzq1Vy0PS2zw .node .label text,#mermaid-svg-2iMEMzq1Vy0PS2zw .image-shape .label,#mermaid-svg-2iMEMzq1Vy0PS2zw .icon-shape .label{text-anchor:middle;}#mermaid-svg-2iMEMzq1Vy0PS2zw .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-2iMEMzq1Vy0PS2zw .rough-node .label,#mermaid-svg-2iMEMzq1Vy0PS2zw .node .label,#mermaid-svg-2iMEMzq1Vy0PS2zw .image-shape .label,#mermaid-svg-2iMEMzq1Vy0PS2zw .icon-shape .label{text-align:center;}#mermaid-svg-2iMEMzq1Vy0PS2zw .node.clickable{cursor:pointer;}#mermaid-svg-2iMEMzq1Vy0PS2zw .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-2iMEMzq1Vy0PS2zw .arrowheadPath{fill:#333333;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-2iMEMzq1Vy0PS2zw .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2iMEMzq1Vy0PS2zw .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-2iMEMzq1Vy0PS2zw .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2iMEMzq1Vy0PS2zw .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-2iMEMzq1Vy0PS2zw .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-2iMEMzq1Vy0PS2zw .cluster text{fill:#333;}#mermaid-svg-2iMEMzq1Vy0PS2zw .cluster span{color:#333;}#mermaid-svg-2iMEMzq1Vy0PS2zw div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-2iMEMzq1Vy0PS2zw .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-2iMEMzq1Vy0PS2zw rect.text{fill:none;stroke-width:0;}#mermaid-svg-2iMEMzq1Vy0PS2zw .icon-shape,#mermaid-svg-2iMEMzq1Vy0PS2zw .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2iMEMzq1Vy0PS2zw .icon-shape p,#mermaid-svg-2iMEMzq1Vy0PS2zw .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-2iMEMzq1Vy0PS2zw .icon-shape rect,#mermaid-svg-2iMEMzq1Vy0PS2zw .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2iMEMzq1Vy0PS2zw .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-2iMEMzq1Vy0PS2zw .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-2iMEMzq1Vy0PS2zw :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Binlog
Parse
Consume
Bulk Insert
MySQL DB
Canal Server
Kafka/RocketMQ
同步服务
Elasticsearch
Mapping JSON 示例:
PUT /mall_goods_v1
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"sku_id": { "type": "keyword" },
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"price": { "type": "double" },
"category_id": { "type": "keyword" },
"tags": { "type": "keyword" }
}
}
}
三、 Redis:极速缓存的 Key 设计
Redis 是基于内存的 KV 数据库。设计核心在于 Key 的命名空间管理 和 Value 的数据结构选择。
3.1 Key 设计规范
- 格式:业务线:模块:对象:ID,使用冒号分隔。
- Bad: count, order123
- Good: mall:order:count:u_8821, sys:session:id_x99
- 长度:不要太长,节省内存;也不要太短,保证可读性。
- 大 Key 问题:避免 Hash/List/Set 中元素超过 5000 个,容易阻塞主线程。
3.2 数据结构选型思维导图
#mermaid-svg-KFsMxJL6DERpbbSd{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-KFsMxJL6DERpbbSd .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-KFsMxJL6DERpbbSd .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-KFsMxJL6DERpbbSd .error-icon{fill:#552222;}#mermaid-svg-KFsMxJL6DERpbbSd .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-KFsMxJL6DERpbbSd .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-KFsMxJL6DERpbbSd .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-KFsMxJL6DERpbbSd .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-KFsMxJL6DERpbbSd .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-KFsMxJL6DERpbbSd .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-KFsMxJL6DERpbbSd .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-KFsMxJL6DERpbbSd .marker{fill:#333333;stroke:#333333;}#mermaid-svg-KFsMxJL6DERpbbSd .marker.cross{stroke:#333333;}#mermaid-svg-KFsMxJL6DERpbbSd svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-KFsMxJL6DERpbbSd p{margin:0;}#mermaid-svg-KFsMxJL6DERpbbSd .edge{stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .section–1 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section–1 path,#mermaid-svg-KFsMxJL6DERpbbSd .section–1 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section–1 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section–1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section–1 text{fill:#ffffff;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon–1{font-size:40px;color:#ffffff;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge–1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth–1{stroke-width:17;}#mermaid-svg-KFsMxJL6DERpbbSd .section–1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-0 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-0 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-0 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-0 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-0 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-0{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-0{stroke-width:14;}#mermaid-svg-KFsMxJL6DERpbbSd .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-1 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-1 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-1 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-1 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-1 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-1{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-1{stroke-width:11;}#mermaid-svg-KFsMxJL6DERpbbSd .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-2 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-2 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-2 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-2 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-2 text{fill:#ffffff;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-2{stroke-width:8;}#mermaid-svg-KFsMxJL6DERpbbSd .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-3 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-3 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-3 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-3 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-3 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-3{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-3{stroke-width:5;}#mermaid-svg-KFsMxJL6DERpbbSd .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-4 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-4 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-4 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-4 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-4 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-4{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-4{stroke-width:2;}#mermaid-svg-KFsMxJL6DERpbbSd .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-5 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-5 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-5 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-5 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-5 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-5{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-5{stroke-width:-1;}#mermaid-svg-KFsMxJL6DERpbbSd .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-6 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-6 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-6 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-6 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-6 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-6{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-6{stroke-width:-4;}#mermaid-svg-KFsMxJL6DERpbbSd .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-7 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-7 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-7 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-7 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-7 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-7{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-7{stroke-width:-7;}#mermaid-svg-KFsMxJL6DERpbbSd .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-8 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-8 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-8 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-8 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-8 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-8{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-8{stroke-width:-10;}#mermaid-svg-KFsMxJL6DERpbbSd .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-9 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-9 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-9 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-9 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-9 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-9{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-9{stroke-width:-13;}#mermaid-svg-KFsMxJL6DERpbbSd .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-10 rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-10 path,#mermaid-svg-KFsMxJL6DERpbbSd .section-10 circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-10 polygon,#mermaid-svg-KFsMxJL6DERpbbSd .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-10 text{fill:black;}#mermaid-svg-KFsMxJL6DERpbbSd .node-icon-10{font-size:40px;color:black;}#mermaid-svg-KFsMxJL6DERpbbSd .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .edge-depth-10{stroke-width:-16;}#mermaid-svg-KFsMxJL6DERpbbSd .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled,#mermaid-svg-KFsMxJL6DERpbbSd .disabled circle,#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:lightgray;}#mermaid-svg-KFsMxJL6DERpbbSd .disabled text{fill:#efefef;}#mermaid-svg-KFsMxJL6DERpbbSd .section-root rect,#mermaid-svg-KFsMxJL6DERpbbSd .section-root path,#mermaid-svg-KFsMxJL6DERpbbSd .section-root circle,#mermaid-svg-KFsMxJL6DERpbbSd .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-KFsMxJL6DERpbbSd .section-root text{fill:#ffffff;}#mermaid-svg-KFsMxJL6DERpbbSd .section-root span{color:#ffffff;}#mermaid-svg-KFsMxJL6DERpbbSd .section-2 span{color:#ffffff;}#mermaid-svg-KFsMxJL6DERpbbSd .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-KFsMxJL6DERpbbSd .edge{fill:none;}#mermaid-svg-KFsMxJL6DERpbbSd .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-KFsMxJL6DERpbbSd :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Redis Key 选型
String
简单的 KV 缓存
SETNX
INCR
Hash
User Profile
SkuID: Count
减少 Key 的数量
List
LPUSH/RPOP
最新 N 条评论
Set
点赞用户
SINTER
ZSet
Score: 分数
Score: 时间戳
Bitmap
用户签到
在线状态
Python (redis-py) 示例:
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
# 场景:缓存商品详情,1小时过期
key = "mall:goods:detail:10086"
value = '{"id": 10086, "name": "iPhone 16"}'
r.set(key, value, ex=3600)
# 场景:商品销量排行榜
rank_key = "mall:goods:rank:daily"
r.zincrby(rank_key, 1, "sku_10086")
四、 RabbitMQ:异步解耦的 Topic 设计
RabbitMQ 是基于 AMQP 协议的消息中间件。设计核心在于 Exchange 类型选择 和 Topic 路由键设计。
4.1 Exchange 与 Topic 设计
- Exchange 类型:
- Direct:精确匹配,简单路由。
- Topic:模式匹配,灵活性最强(推荐)。
- Fanout:广播,不处理路由键。
- Routing Key 命名:业务.来源.动作.状态。
- 示例:order.mobile.create.success、user.web.login.failed。
- 优势:消费者可以使用 order.# (监听所有订单消息) 或 *.mobile.*.* (监听所有移动端消息)。
4.2 消息模型时序图
下图展示了一个基于 Topic Exchange 的订单处理流程:
Queue (通知服务)
Queue (库存服务)
Exchange (Topic: mall.event)
生产者 (OrderService)
Queue (通知服务)
Queue (库存服务)
Exchange (Topic: mall.event)
生产者 (OrderService)
#mermaid-svg-Fta8L03Kp0bgVr2x{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Fta8L03Kp0bgVr2x .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Fta8L03Kp0bgVr2x .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Fta8L03Kp0bgVr2x .error-icon{fill:#552222;}#mermaid-svg-Fta8L03Kp0bgVr2x .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Fta8L03Kp0bgVr2x .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Fta8L03Kp0bgVr2x .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Fta8L03Kp0bgVr2x .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Fta8L03Kp0bgVr2x .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Fta8L03Kp0bgVr2x .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Fta8L03Kp0bgVr2x .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Fta8L03Kp0bgVr2x .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Fta8L03Kp0bgVr2x .marker.cross{stroke:#333333;}#mermaid-svg-Fta8L03Kp0bgVr2x svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Fta8L03Kp0bgVr2x p{margin:0;}#mermaid-svg-Fta8L03Kp0bgVr2x .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-Fta8L03Kp0bgVr2x text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-Fta8L03Kp0bgVr2x .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-Fta8L03Kp0bgVr2x .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-Fta8L03Kp0bgVr2x .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-Fta8L03Kp0bgVr2x .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-Fta8L03Kp0bgVr2x #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-Fta8L03Kp0bgVr2x .sequenceNumber{fill:white;}#mermaid-svg-Fta8L03Kp0bgVr2x #sequencenumber{fill:#333;}#mermaid-svg-Fta8L03Kp0bgVr2x #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-Fta8L03Kp0bgVr2x .messageText{fill:#333;stroke:none;}#mermaid-svg-Fta8L03Kp0bgVr2x .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-Fta8L03Kp0bgVr2x .labelText,#mermaid-svg-Fta8L03Kp0bgVr2x .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-Fta8L03Kp0bgVr2x .loopText,#mermaid-svg-Fta8L03Kp0bgVr2x .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-Fta8L03Kp0bgVr2x .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-Fta8L03Kp0bgVr2x .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-Fta8L03Kp0bgVr2x .noteText,#mermaid-svg-Fta8L03Kp0bgVr2x .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-Fta8L03Kp0bgVr2x .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-Fta8L03Kp0bgVr2x .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-Fta8L03Kp0bgVr2x .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-Fta8L03Kp0bgVr2x .actorPopupMenu{position:absolute;}#mermaid-svg-Fta8L03Kp0bgVr2x .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-Fta8L03Kp0bgVr2x .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-Fta8L03Kp0bgVr2x .actor-man circle,#mermaid-svg-Fta8L03Kp0bgVr2x line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-Fta8L03Kp0bgVr2x :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
RoutingKey: order.created
Binding: order.*
Binding:
发送消息 "Order
路由消息
路由消息
扣减库存
发送短信/邮件
Java (Spring Boot) 配置示例:
@Configuration
public class RabbitConfig {
public static final String TOPIC_EXCHANGE = "mall.event.exchange";
public static final String STOCK_QUEUE = "stock.order.queue";
// 1. 定义 Topic 交换机
@Bean
public TopicExchange exchange() {
return new TopicExchange(TOPIC_EXCHANGE);
}
// 2. 定义队列
@Bean
public Queue stockQueue() {
return new Queue(STOCK_QUEUE);
}
// 3. 绑定:库存队列关心所有订单事件 (order.*)
@Bean
public Binding bindingStock() {
return BindingBuilder.bind(stockQueue())
.to(exchange())
.with("order.*"); // 路由键匹配模式
}
}
一个优秀的后端系统,能够充分利用这四者的长处:
综合架构示意图
#mermaid-svg-IVYFsf7CEd3hrosY{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-IVYFsf7CEd3hrosY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IVYFsf7CEd3hrosY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IVYFsf7CEd3hrosY .error-icon{fill:#552222;}#mermaid-svg-IVYFsf7CEd3hrosY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IVYFsf7CEd3hrosY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IVYFsf7CEd3hrosY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IVYFsf7CEd3hrosY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IVYFsf7CEd3hrosY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IVYFsf7CEd3hrosY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IVYFsf7CEd3hrosY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IVYFsf7CEd3hrosY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IVYFsf7CEd3hrosY .marker.cross{stroke:#333333;}#mermaid-svg-IVYFsf7CEd3hrosY svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IVYFsf7CEd3hrosY p{margin:0;}#mermaid-svg-IVYFsf7CEd3hrosY .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-IVYFsf7CEd3hrosY .cluster-label text{fill:#333;}#mermaid-svg-IVYFsf7CEd3hrosY .cluster-label span{color:#333;}#mermaid-svg-IVYFsf7CEd3hrosY .cluster-label span p{background-color:transparent;}#mermaid-svg-IVYFsf7CEd3hrosY .label text,#mermaid-svg-IVYFsf7CEd3hrosY span{fill:#333;color:#333;}#mermaid-svg-IVYFsf7CEd3hrosY .node rect,#mermaid-svg-IVYFsf7CEd3hrosY .node circle,#mermaid-svg-IVYFsf7CEd3hrosY .node ellipse,#mermaid-svg-IVYFsf7CEd3hrosY .node polygon,#mermaid-svg-IVYFsf7CEd3hrosY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-IVYFsf7CEd3hrosY .rough-node .label text,#mermaid-svg-IVYFsf7CEd3hrosY .node .label text,#mermaid-svg-IVYFsf7CEd3hrosY .image-shape .label,#mermaid-svg-IVYFsf7CEd3hrosY .icon-shape .label{text-anchor:middle;}#mermaid-svg-IVYFsf7CEd3hrosY .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-IVYFsf7CEd3hrosY .rough-node .label,#mermaid-svg-IVYFsf7CEd3hrosY .node .label,#mermaid-svg-IVYFsf7CEd3hrosY .image-shape .label,#mermaid-svg-IVYFsf7CEd3hrosY .icon-shape .label{text-align:center;}#mermaid-svg-IVYFsf7CEd3hrosY .node.clickable{cursor:pointer;}#mermaid-svg-IVYFsf7CEd3hrosY .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-IVYFsf7CEd3hrosY .arrowheadPath{fill:#333333;}#mermaid-svg-IVYFsf7CEd3hrosY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-IVYFsf7CEd3hrosY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-IVYFsf7CEd3hrosY .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IVYFsf7CEd3hrosY .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-IVYFsf7CEd3hrosY .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IVYFsf7CEd3hrosY .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-IVYFsf7CEd3hrosY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-IVYFsf7CEd3hrosY .cluster text{fill:#333;}#mermaid-svg-IVYFsf7CEd3hrosY .cluster span{color:#333;}#mermaid-svg-IVYFsf7CEd3hrosY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-IVYFsf7CEd3hrosY .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-IVYFsf7CEd3hrosY rect.text{fill:none;stroke-width:0;}#mermaid-svg-IVYFsf7CEd3hrosY .icon-shape,#mermaid-svg-IVYFsf7CEd3hrosY .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IVYFsf7CEd3hrosY .icon-shape p,#mermaid-svg-IVYFsf7CEd3hrosY .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-IVYFsf7CEd3hrosY .icon-shape rect,#mermaid-svg-IVYFsf7CEd3hrosY .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IVYFsf7CEd3hrosY .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-IVYFsf7CEd3hrosY .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-IVYFsf7CEd3hrosY :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
读
Miss
写
事务写
异步消息
解耦
写入
复杂搜索
主从复制
用户
API Gateway
Redis
业务服务
MySQL Master
RabbitMQ
索引服务
Elasticsearch
MySQL Slave
通过合理规范库表、索引、Key 和 Topic 的设计,我们不仅能提升系统的性能,更能极大地降低后期的维护成本和沟通成本。





