Repository navigation
Expand file tree
/
Copy pathMiniRedis.html
More file actions
350 lines (339 loc) · 22.4 KB
/
Copy pathMiniRedis.html
File metadata and controls
350 lines (339 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MiniRedis 技术分享报告</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<style>
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
background-color: #FFFFFF;
}
.card {
background-color: white;
border: 1px solid #E5E7EB;
border-radius: 1.25rem; /* 20px */
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
overflow: hidden;
position: relative;
}
.highlight-gradient-blue { background: linear-gradient(135deg, rgba(59, 130, 246, 0), rgba(59, 130, 246, 0.05)); }
.highlight-gradient-green { background: linear-gradient(135deg, rgba(34, 197, 94, 0), rgba(34, 197, 94, 0.05)); }
.highlight-gradient-amber { background: linear-gradient(135deg, rgba(245, 158, 11, 0), rgba(245, 158, 11, 0.05)); }
.highlight-gradient-purple { background: linear-gradient(135deg, rgba(139, 92, 246, 0), rgba(139, 92, 246, 0.05)); }
.highlight-gradient-red { background: linear-gradient(135deg, rgba(239, 68, 68, 0), rgba(239, 68, 68, 0.05)); }
.highlight-gradient-sky { background: linear-gradient(135deg, rgba(14, 165, 233, 0), rgba(14, 165, 233, 0.05)); }
</style>
</head>
<body class="bg-white">
<div class="max-w-7xl mx-auto p-4 md:p-8">
<div class="grid grid-cols-1 md:grid-cols-4 gap-4 md:gap-6">
<!-- 标题卡片 -->
<div class="card md:col-span-4 p-8 flex flex-col justify-center items-center text-center">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-blue"></div>
<div class="relative w-full">
<h1 class="text-4xl md:text-6xl font-bold text-gray-800 mb-4">MiniRedis</h1>
<h2 class="text-2xl md:text-3xl font-semibold text-gray-600 mb-6">架构设计、核心实现与性能优化</h2>
<div class="text-lg text-gray-500 max-w-4xl mx-auto">
一款基于 Java 实现、完全兼容 RESP 协议的轻量级内存键值存储系统,复刻了 Redis 的核心架构与功能特性。
</div>
</div>
</div>
<!-- 核心特性卡片 -->
<div class="card md:col-span-2 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-green"></div>
<div class="relative flex-grow">
<div class="flex items-center mb-4">
<i class="fa-solid fa-layer-group text-2xl text-green-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">核心特性</h3>
</div>
<ul class="space-y-3 text-gray-700">
<li class="flex items-start"><i class="fa-solid fa-check-circle text-green-500 mt-1 mr-2"></i><span>支持 String, List, Hash, Set, ZSet 五种主流数据类型</span></li>
<li class="flex items-start"><i class="fa-solid fa-check-circle text-green-500 mt-1 mr-2"></i><span>完整实现 RDB 快照与 AOF 日志持久化</span></li>
<li class="flex items-start"><i class="fa-solid fa-check-circle text-green-500 mt-1 mr-2"></i><span>支持主从复制、事务、Lua 脚本执行</span></li>
<li class="flex items-start"><i class="fa-solid fa-check-circle text-green-500 mt-1 mr-2"></i><span>提供发布订阅、键过期管理等企业级特性</span></li>
</ul>
</div>
</div>
<!-- 技术栈卡片 -->
<div class="card md:col-span-2 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-amber"></div>
<div class="relative flex-grow">
<div class="flex items-center mb-4">
<i class="fa-solid fa-cogs text-2xl text-amber-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">技术栈选型</h3>
</div>
<ul class="space-y-3 text-gray-700">
<li class="flex items-start"><i class="fa-brands fa-java text-amber-500 mt-1 mr-2"></i><span>基于 Java 语言,易于理解与二次开发</span></li>
<li class="flex items-start"><i class="fa-solid fa-network-wired text-amber-500 mt-1 mr-2"></i><span>Netty 作为高性能网络通信层</span></li>
<li class="flex items-start"><i class="fa-solid fa-database text-amber-500 mt-1 mr-2"></i><span>定制化 Dict 数据结构实现高效存储</span></li>
<li class="flex items-start"><i class="fa-solid fa-file-code text-amber-500 mt-1 mr-2"></i><span>兼容 Redis 标准 RESP 协议</span></li>
</ul>
</div>
</div>
<!-- 架构分层卡片 -->
<div class="card md:col-span-4 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-purple"></div>
<div class="relative flex-grow">
<div class="flex items-center mb-6">
<i class="fa-solid fa-sitemap text-2xl text-purple-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">系统架构分层</h3>
</div>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div class="bg-purple-50 border border-purple-100 rounded-lg p-4 text-center">
<i class="fa-solid fa-server text-3xl text-purple-500 mb-2"></i>
<h4 class="font-semibold text-gray-800 mb-1">网络传输层</h4>
<p class="text-sm text-gray-600">Netty 处理 TCP 连接与数据传输</p>
</div>
<div class="bg-purple-50 border border-purple-100 rounded-lg p-4 text-center">
<i class="fa-solid fa-code text-3xl text-purple-500 mb-2"></i>
<h4 class="font-semibold text-gray-800 mb-1">协议处理层</h4>
<p class="text-sm text-gray-600">解析与封装 RESP 协议</p>
</div>
<div class="bg-purple-50 border border-purple-100 rounded-lg p-4 text-center">
<i class="fa-solid fa-cogs text-3xl text-purple-500 mb-2"></i>
<h4 class="font-semibold text-gray-800 mb-1">核心服务层</h4>
<p class="text-sm text-gray-600">命令路由、服务器状态管理</p>
</div>
<div class="bg-purple-50 border border-purple-100 rounded-lg p-4 text-center">
<i class="fa-solid fa-database text-3xl text-purple-500 mb-2"></i>
<h4 class="font-semibold text-gray-800 mb-1">数据管理层</h4>
<p class="text-sm text-gray-600">存储引擎、持久化、高可用</p>
</div>
</div>
</div>
</div>
<!-- 核心模块表格卡片 -->
<div class="card md:col-span-4 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-red"></div>
<div class="relative flex-grow overflow-x-auto">
<div class="flex items-center mb-6">
<i class="fa-solid fa-puzzle-piece text-2xl text-red-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">核心模块划分</h3>
</div>
<table class="min-w-full bg-white border border-gray-200 rounded-lg">
<thead>
<tr class="bg-gray-50 text-left">
<th class="px-4 py-3 border-b text-sm font-semibold text-gray-700">模块ID</th>
<th class="px-4 py-3 border-b text-sm font-semibold text-gray-700">核心职责</th>
<th class="px-4 py-3 border-b text-sm font-semibold text-gray-700">关键技术选型</th>
<th class="px-4 py-3 border-b text-sm font-semibold text-gray-700">所属架构分层</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
<tr class="hover:bg-gray-50">
<td class="px-4 py-3 text-sm text-gray-800 font-medium">remoting-netty4</td>
<td class="px-4 py-3 text-sm text-gray-600">高性能异步网络通信</td>
<td class="px-4 py-3 text-sm text-gray-600">Netty 4.1, Reactor 模型</td>
<td class="px-4 py-3 text-sm text-gray-600">网络传输层</td>
</tr>
<tr class="hover:bg-gray-50">
<td class="px-4 py-3 text-sm text-gray-800 font-medium">redis-protocol</td>
<td class="px-4 py-3 text-sm text-gray-600">RESP 协议编解码</td>
<td class="px-4 py-3 text-sm text-gray-600">自定义编解码器</td>
<td class="px-4 py-3 text-sm text-gray-600">协议处理层</td>
</tr>
<tr class="hover:bg-gray-50">
<td class="px-4 py-3 text-sm text-gray-800 font-medium">java-redis30</td>
<td class="px-4 py-3 text-sm text-gray-600">主服务器实现</td>
<td class="px-4 py-3 text-sm text-gray-600">命令路由、上下文管理</td>
<td class="px-4 py-3 text-sm text-gray-600">核心服务层</td>
</tr>
<tr class="hover:bg-gray-50">
<td class="px-4 py-3 text-sm text-gray-800 font-medium">redis-storage</td>
<td class="px-4 py-3 text-sm text-gray-600">内存数据存储与访问</td>
<td class="px-4 py-3 text-sm text-gray-600">Dict 结构, 读写锁</td>
<td class="px-4 py-3 text-sm text-gray-600">数据管理层</td>
</tr>
<tr class="hover:bg-gray-50">
<td class="px-4 py-3 text-sm text-gray-800 font-medium">redis-persistence</td>
<td class="px-4 py-3 text-sm text-gray-600">RDB/AOF 持久化</td>
<td class="px-4 py-3 text-sm text-gray-600">异步文件写入</td>
<td class="px-4 py-3 text-sm text-gray-600">数据管理层</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- 网络层优化卡片 -->
<div class="card md:col-span-2 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-sky"></div>
<div class="relative flex-grow">
<div class="flex items-center mb-4">
<i class="fa-solid fa-bolt text-2xl text-sky-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">高性能网络层优化</h3>
</div>
<ul class="space-y-3 text-gray-700">
<li class="flex items-start"><i class="fa-solid fa-circle-nodes text-sky-500 mt-1 mr-2"></i><span>Reactor 主从线程模型,解耦连接与处理</span></li>
<li class="flex items-start"><i class="fa-solid fa-sliders text-sky-500 mt-1 mr-2"></i><span>启用 TCP_NODELAY,禁用 Nagle 算法</span></li>
<li class="flex items-start"><i class="fa-solid fa-layer-group text-sky-500 mt-1 mr-2"></i><span>ByteBuf 内存池化,降低 GC 开销</span></li>
<li class="flex items-start"><i class="fa-solid fa-copy text-sky-500 mt-1 mr-2"></i><span>CompositeByteBuf 实现零拷贝</span></li>
</ul>
</div>
</div>
<!-- 性能对比图表卡片 -->
<div class="card md:col-span-2 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-amber"></div>
<div class="relative flex-grow">
<div class="flex items-center mb-4">
<i class="fa-solid fa-chart-line text-2xl text-amber-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">性能对比 (QPS)</h3>
</div>
<div class="h-64">
<canvas id="performanceChart"></canvas>
</div>
</div>
</div>
<!-- 持久化卡片 -->
<div class="card md:col-span-4 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-green"></div>
<div class="relative flex-grow">
<div class="flex items-center mb-6">
<i class="fa-solid fa-save text-2xl text-green-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">持久化机制</h3>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div class="bg-green-50 border border-green-100 rounded-lg p-5">
<h4 class="font-semibold text-gray-800 mb-3 flex items-center"><i class="fa-solid fa-camera-retro text-green-500 mr-2"></i>RDB 快照持久化</h4>
<ul class="space-y-2 text-sm text-gray-700">
<li class="flex items-start"><i class="fa-solid fa-check text-green-500 mt-1 mr-2"></i><span>全量数据快照,体积小,恢复快</span></li>
<li class="flex items-start"><i class="fa-solid fa-check text-green-500 mt-1 mr-2"></i><span>支持手动 (SAVE/BGSAVE) 和自动触发</span></li>
<li class="flex items-start"><i class="fa-solid fa-exclamation-triangle text-amber-500 mt-1 mr-2"></i><span>可能丢失最后一次快照后的修改</span></li>
</ul>
</div>
<div class="bg-green-50 border border-green-100 rounded-lg p-5">
<h4 class="font-semibold text-gray-800 mb-3 flex items-center"><i class="fa-solid fa-file-lines text-green-500 mr-2"></i>AOF 日志持久化</h4>
<ul class="space-y-2 text-sm text-gray-700">
<li class="flex items-start"><i class="fa-solid fa-check text-green-500 mt-1 mr-2"></i><span>增量记录写命令,数据安全性高</span></li>
<li class="flex items-start"><i class="fa-solid fa-check text-green-500 mt-1 mr-2"></i><span>支持 always, everysec, no 三种同步策略</span></li>
<li class="flex items-start"><i class="fa-solid fa-check text-green-500 mt-1 mr-2"></i><span>支持 AOF 重写,压缩日志体积</span></li>
</ul>
</div>
</div>
</div>
</div>
<!-- 数据类型卡片 -->
<div class="card md:col-span-4 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-blue"></div>
<div class="relative flex-grow">
<div class="flex items-center mb-6">
<i class="fa-solid fa-layer-group text-2xl text-blue-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">核心数据类型实现</h3>
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-4">
<div class="bg-blue-50 border border-blue-100 rounded-lg p-4 text-center">
<i class="fa-solid fa-font text-3xl text-blue-500 mb-2"></i>
<h4 class="font-semibold text-gray-800 mb-1">String</h4>
<p class="text-xs text-gray-600">SDS 轻量化实现</p>
</div>
<div class="bg-blue-50 border border-blue-100 rounded-lg p-4 text-center">
<i class="fa-solid fa-list text-3xl text-blue-500 mb-2"></i>
<h4 class="font-semibold text-gray-800 mb-1">List</h4>
<p class="text-xs text-gray-600">Quicklist 优化版</p>
</div>
<div class="bg-blue-50 border border-blue-100 rounded-lg p-4 text-center">
<i class="fa-solid fa-table-cells text-3xl text-blue-500 mb-2"></i>
<h4 class="font-semibold text-gray-800 mb-1">Hash</h4>
<p class="text-xs text-gray-600">链地址法哈希表</p>
</div>
<div class="bg-blue-50 border border-blue-100 rounded-lg p-4 text-center">
<i class="fa-solid fa-circle-nodes text-3xl text-blue-500 mb-2"></i>
<h4 class="font-semibold text-gray-800 mb-1">Set</h4>
<p class="text-xs text-gray-600">数组/哈希表切换</p>
</div>
<div class="bg-blue-50 border border-blue-100 rounded-lg p-4 text-center">
<i class="fa-solid fa-sort-numeric-down text-3xl text-blue-500 mb-2"></i>
<h4 class="font-semibold text-gray-800 mb-1">ZSet</h4>
<p class="text-xs text-gray-600">跳表 + 哈希表</p>
</div>
</div>
</div>
</div>
<!-- 高级特性卡片 -->
<div class="card md:col-span-2 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-purple"></div>
<div class="relative flex-grow">
<div class="flex items-center mb-4">
<i class="fa-solid fa-star text-2xl text-purple-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">高级特性</h3>
</div>
<ul class="space-y-3 text-gray-700">
<li class="flex items-start"><i class="fa-solid fa-server text-purple-500 mt-1 mr-2"></i><span>主从复制:全量同步 + 增量复制</span></li>
<li class="flex items-start"><i class="fa-solid fa-file-contract text-purple-500 mt-1 mr-2"></i><span>事务支持:MULTI, EXEC, DISCARD</span></li>
<li class="flex items-start"><i class="fa-solid fa-tower-broadcast text-purple-500 mt-1 mr-2"></i><span>发布订阅:频道消息广播</span></li>
<li class="flex items-start"><i class="fa-solid fa-code text-purple-500 mt-1 mr-2"></i><span>Lua 脚本:原子性执行</span></li>
</ul>
</div>
</div>
<!-- 应用场景卡片 -->
<div class="card md:col-span-2 p-6 flex flex-col">
<div class="absolute top-0 left-0 w-full h-full highlight-gradient-red"></div>
<div class="relative flex-grow">
<div class="flex items-center mb-4">
<i class="fa-solid fa-lightbulb text-2xl text-red-500 mr-3"></i>
<h3 class="text-xl font-bold text-gray-800">典型应用场景</h3>
</div>
<ul class="space-y-3 text-gray-700">
<li class="flex items-start"><i class="fa-solid fa-microchip text-red-500 mt-1 mr-2"></i><span>单元测试与本地开发环境</span></li>
<li class="flex items-start"><i class="fa-solid fa-rocket text-red-500 mt-1 mr-2"></i><span>CI/CD 流水线缓存</span></li>
<li class="flex items-start"><i class="fa-solid fa-book-open text-red-500 mt-1 mr-2"></i><span>Redis 内部原理学习平台</span></li>
<li class="flex items-start"><i class="fa-solid fa-cogs text-red-500 mt-1 mr-2"></i><span>轻量级缓存与会话存储</span></li>
</ul>
</div>
</div>
</div>
</div>
</body>
<script>
const ctx = document.getElementById('performanceChart').getContext('2d');
new Chart(ctx, {
type: 'bar',
data: {
labels: ['GET', 'SET', 'LPUSH', 'LRANGE', 'HSET', 'ZRANGE'],
datasets: [{
label: 'MiniRedis',
data: [85000, 80000, 75000, 60000, 70000, 55000],
backgroundColor: 'rgba(59, 130, 246, 0.6)',
borderColor: 'rgba(59, 130, 246, 1)',
borderWidth: 1
}, {
label: 'Redis (参考)',
data: [100000, 95000, 90000, 75000, 85000, 70000],
backgroundColor: 'rgba(34, 197, 94, 0.6)',
borderColor: 'rgba(34, 197, 94, 1)',
borderWidth: 1
}]
},
options: {
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'QPS'
}
},
x: {
title: {
display: true,
text: '命令类型'
}
}
},
plugins: {
legend: {
position: 'top',
},
title: {
display: false
}
}
}
});
</script>
</html>