Outline
●Interview questions
●Bloom Filter
–Data structure
–Probability of false positives
–Set properties
●Application
–Cache sharing : squid
–Speed up data access : Hbase
–ID Mapping : zoie
●Materials
Interview questions
●Crawler
–Billions web pages
–How to keep track crawled urls
●Straggler Detection
–You are manning the security desk of a large building
–Everyone checks in or checks out with their id
–At the end of day, identify the few stragglers left in the
building
Data structure
●Data structure
–Init : a bit array of m bits, all set to 0
–Add an element
●K hash function to get K array positions
●Set the bits at all these positions to 1
●Query an element (test whether it's in the set)
–K hash function to get K array positions
–If any positionare 0, not in the set
–If all are 1, probabilistic in the set
Probability of false positives
●1 hash function
000000010000010000000000
p(A[i]=1∣hash[x
1,…,x
n])=1−(1−
1
m
)
n
p(hash[x
1]=i)=
1
m
p(A[i]=0∣hash[x
1])=1−
1
m
p(A[i]=0∣hash[x
1,…,x
n])=(1−
1
m
)
n
p(A[i]=1)=1−(1−
1
m
)
n
≃1−e
−n/m
lim
x→∞
(1−
1
x
)
−x
=e
Probability of false positives
●1 hash function
000000010000010000000000
p(A[H(y)]=1∣y∉S)=
(numberof1)
m
2/23
p(A[i]=1)=1−e
−n/m
Given
E(numberof1)=m⋅(1−e
−n/m
)
p(A[H(y)]=1∣y∉S)=(1−e
−n/m
)
Probability of false positives
●K hash function : repeat for k times
000000010000010000000000
p(A[H(y)]=1∣y∉S)=
(numberof1)
m
p(A[i]=1)=1−e
−n/m
Given
E(numberof1)=m⋅(1−e
−n/m
)
p(A[H(y)]=1∣y∉S)=(1−e
−n/m
)
p(A[H(y)]=1∣y∉S)=(
numberof1
m
)
k
p(A[i]=1)=1−(1−
1
m
)
kn
≃1−e
−kn/m
E(numberof1)=m⋅(1−e
−kn/m
)
p(A[H(y)]=1∣y∉S)=(1−e
−kn/m
)
k
Probability of false positives
●Minimal Probability of false positives
p(A[H(y)]=1∣y∉S)=(1−e
−kn/m
)
k
f=(1−e
−kn/m
)
k
f=e
k∗ln(1−e
−kn/m
)
Minimal f, then minimal g
g=k∗ln(1−e
−kn/m
)
p=e
−kn/m
Given
g=−
m
n
ln(p)∗ln(1−p)
Minimal(f)=(
1
2
)
k
p=
1
2
e
−kn/m
=
1
2
is the probability than any specific bit is still 0
half-full Bloom filter array
Set properties
●Union (bitwise OR)
– same as the Bloom filter created from scratch using
the union of the two sets.
●Intersection (AND operations)
– the false positive probability in the resulting Bloom
filter is at most the false-positive probability in one of
the constituent Bloom filters, but may be larger than
the false positive probability in the Bloom filter
created from scratch using the intersection of the two
sets
Squid : Cache Digests
Squid : Cache Digests
Squid : Cache Digests
Squid : Cache Digests
Squid : Cache Digests
●False positive:
–Proxy A thinks Proxy B has URL U cached. A
asks for cached U, B responds back with “no”,
A goes to actual website.
HBase :architecture
Hbase :HFile format
●(Not including Bloom Filter)
HBase : Query optimization
●Bloom Filter
–As meta store of HFile
–used to determine if a given key is in that store file
●Characteristics
–Know n total KV count (N), but actual count can
often be much lower
–HFile.insert (and hence, BloomFilter.add)
commands are done in lexicographically increasing
order
4000
10000
5000
9001
Application : Zoie
●Long[] uidArray
–Add element
–Query element
int h = (int) ((uid >>> 32) ^ uid) * MIXER;
long bits = _filter[h & _mask];
bits |= ((1L << (h >>> 26)));
bits |= ((1L << ((h >> 20) & 0x3F)));
_filter[h & _mask] = bits;
final int h = (int) ((uid >>> 32) ^ uid) * MIXER;
final int p = h & _mask;
// check the filter
final long bits = _filter[p];
if ((bits & (1L << (h >>> 26))) == 0
|| (bits & (1L << ((h >> 20) & 0x3F))) == 0)
return -1;