SlideShare a Scribd company logo
1 of 17
Download to read offline
Hash算法及应用
清平 2016.6
大纲
一、hash表知识
二、hash算法
三、hash算法的应用
一、预读回顾
1. hash表为了解决什么?
2. 什么是hash表退化(或者说hash碰撞) ?
3. hash算法要注意那些问题 ?
4. hash表应用场景有那些 ?
推荐观看:TED-Dan Meyer(达恩·迈尔) : 数学课需要改革
百度面试题
搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串
的长度为1-255字节。
假设目前有一千万个记录(这些查询串的重复度比较高,虽然总数是1千万,但如果除
去重复后,不超过3百万个。一个查询串的重复度越高,说明查询它的用户越多,也就是越热
门),请你统计最热门的10个查询串,要求使用的内存不能超过1G。
一千万条记录,每条记录是255Byte,要占据2.375G内存。
问题变种: 要对这一千万条实现排序, 要求使用的内存不能超 过1G。
一、hash表 -- 链表实现
index = hash(key) % D
resize:
哈希表的载荷因子定义为:a = 填入表中的元素个数 / 哈希表的长度
应严格限制在0.7-0.8以下 (然而也有例外: 最小完美hash)
二、hash算法 -- 非加密现代hash列表
BigData is like teenage sex,everybody talks about it,nobody really knows what is it。
2006 lookup3 Bob Jenkins
2008 MurmurHash Austin Appleby
2011 SpookyHash Bob Jenkins
2011 CityHash google
2012 xxhash Y.C.
2014 FarmHash google
Test Link
二、hash算法 -- hash碰撞
2011:哈希碰撞式拒绝服务攻击的漏洞(Hash Collision DoS)
实际测试中,针对8万条级别的数据,原本只需要数毫秒即可完成的插入或者查询操作,
在退化为链表后则需要长达30秒以上的时间才能完成。
具体攻击分析可参考:这里
二、hash算法 -- redis(murmurhash2)
5381 = 1 0101 0000 0101
0x5bd1e995 = 101 1011 1101 0001 1110 1001 1001 0101
redis在2012年的时候, 切到Murmur: Link
二、hash算法 -- farmhash
farmhash hash64
二、hash算法 -- Blizzard hash algorithm
0x7FED7FED = 2146271213
111 1111 1110 1101 0111 1111 1110 1101
0xEEEEEEEE = 4008636142
1110 1110 1110 1110 1110 1110 1110 1110
Link
不同字符串的碰撞概率:
1 : 18889465931478580854784
二、hash算法 -- Bloom Filter
你现在是google的一个程序员了,被安排到网络蜘蛛项目中, 由于网络间的链接错综复杂,蜘蛛在网络间爬行很可能会形成“环”;
为了避免形成“环”,就需要知道蜘蛛已经访问过那些URL, 老大把这个功能模块交给你了: 给一个URL,判断并返回是否已经访问。
你会怎么实现 ?
1. 将访问过的URL保存到数据库。
2. 用HashSet将访问过的URL保存起来。那只需接近O(1)的代价就可以查到一个URL是否被访问过了。
3. URL经过MD5或SHA-1等单向哈希后再保存到HashSet或数据库。
4. Bit-Map方法。建立一个BitSet,将每个URL经过一个哈希函数映射到某一位。
适用场景:允许少量错误判断
出错概率最小: k = ln(2)* m/n
k : 哈希函数个数
m :位数组大小
n : 加入的字符串数量
k=10,m=n*20,概率是 万分之9
三、hash应用 -- 节省磁盘操作
CDN(one-hit-wonders),节省磁盘写操作和磁盘空间
来源: 《CDN算法掘金》 -- ACM SIGCOMM Computer
你在七牛的同学请教你一个问题,他正在使用k/v存储,根
据统计结果发现有很多无效的k请求,导致每次都需要磁盘
IO(因为无效的k也需要读一次磁盘), 怎么有效避免? 你跳槽到UCloud了,通过CDN请求统计发现,有很多仅访问一次的资源
(one-hit-wonders), 把这些资源存到CDN中会带来磁盘操作和存储空
间的浪费。
你必须要找到一个方案来避免!
leveldb 减少磁盘访问
三、hash应用 -- 分布式缓存(一致性hash)
为了缓解海量请求,我们采用了分布式缓存,假设有3个
node,根据uid%3来获取读写node的index。
问题来了:
1. 活跃的uid都分到同一个node上去了,形成了热点
2. node数量减少到2, 或者增加到4
回顾&QA
一、hash表知识
二、hash算法
redis、Blizzard、Bloom filter etc
三、hash算法的应用
分布式缓存、CDN、leveldb、文本压缩
附录A:hash应用 -- 文本压缩(LZ4)
xxhash
lz4:link1,link2
附录B:hash应用 -- 搜索(倒排索引)
最小完美hash( c实现 )
w: 关键词, m: 关键词总数
● f1,f2根据w生成两个不同的数字(n1,n2) 范围在0-2m
● g 是一个根据n1,n2构造的无向图(无环)
● 无向图的n1,n2顶点的边,则是w生成的数字(唯一)
附录C:hash算法应用 -- 列表
1、负载均衡
分布式缓存节点调度、分布式节点调度
2、Hash表
数据字典(压缩、索引)、缓存等
3、安全相关
密码表、数字签名(SHA)、文件校验(MD5)、鉴权协议
4、海量数据处理
bloom filter

More Related Content

Viewers also liked

数据库Sharding专题
数据库Sharding专题数据库Sharding专题
数据库Sharding专题清平 张
 
Companies act 1956
Companies act 1956Companies act 1956
Companies act 1956Rahul Sharma
 
Managing IT security and Business Ethics
Managing IT security and Business EthicsManaging IT security and Business Ethics
Managing IT security and Business EthicsRahul Sharma
 
Latest Mergers and Acquisitions
Latest Mergers and AcquisitionsLatest Mergers and Acquisitions
Latest Mergers and AcquisitionsRahul Sharma
 
Consumer protection Act
Consumer protection ActConsumer protection Act
Consumer protection ActRahul Sharma
 
Coal pricing(Domestic and International)
Coal pricing(Domestic and International)Coal pricing(Domestic and International)
Coal pricing(Domestic and International)Rahul Sharma
 
Unit trust of india
Unit trust of indiaUnit trust of india
Unit trust of indiaRahul Sharma
 
Labord. evaluation-par-les-pairs -MOOC-pedagogie
Labord. evaluation-par-les-pairs -MOOC-pedagogieLabord. evaluation-par-les-pairs -MOOC-pedagogie
Labord. evaluation-par-les-pairs -MOOC-pedagogieExpertWeb
 
Performances php chez M6Web
Performances php chez M6WebPerformances php chez M6Web
Performances php chez M6WebKenny Dits
 
Instants T de Rhône Tourisme : Atelier Relations Presse
Instants T de Rhône Tourisme : Atelier Relations PresseInstants T de Rhône Tourisme : Atelier Relations Presse
Instants T de Rhône Tourisme : Atelier Relations PresseStéphanie De Lyon Tourisme
 

Viewers also liked (13)

数据库Sharding专题
数据库Sharding专题数据库Sharding专题
数据库Sharding专题
 
Senior Year!
Senior Year!Senior Year!
Senior Year!
 
Companies act 1956
Companies act 1956Companies act 1956
Companies act 1956
 
Pay it forward!
Pay it forward!Pay it forward!
Pay it forward!
 
Managing IT security and Business Ethics
Managing IT security and Business EthicsManaging IT security and Business Ethics
Managing IT security and Business Ethics
 
Latest Mergers and Acquisitions
Latest Mergers and AcquisitionsLatest Mergers and Acquisitions
Latest Mergers and Acquisitions
 
Consumer protection Act
Consumer protection ActConsumer protection Act
Consumer protection Act
 
Coal pricing(Domestic and International)
Coal pricing(Domestic and International)Coal pricing(Domestic and International)
Coal pricing(Domestic and International)
 
JSW Steel Ltd.
JSW Steel Ltd.JSW Steel Ltd.
JSW Steel Ltd.
 
Unit trust of india
Unit trust of indiaUnit trust of india
Unit trust of india
 
Labord. evaluation-par-les-pairs -MOOC-pedagogie
Labord. evaluation-par-les-pairs -MOOC-pedagogieLabord. evaluation-par-les-pairs -MOOC-pedagogie
Labord. evaluation-par-les-pairs -MOOC-pedagogie
 
Performances php chez M6Web
Performances php chez M6WebPerformances php chez M6Web
Performances php chez M6Web
 
Instants T de Rhône Tourisme : Atelier Relations Presse
Instants T de Rhône Tourisme : Atelier Relations PresseInstants T de Rhône Tourisme : Atelier Relations Presse
Instants T de Rhône Tourisme : Atelier Relations Presse
 

Hash算法