It Probably Works Tyler McMullen CTO of Fastly @tbmcmullen Fastly - - PowerPoint PPT Presentation

it probably works tyler mcmullen
SMART_READER_LITE
LIVE PREVIEW

It Probably Works Tyler McMullen CTO of Fastly @tbmcmullen Fastly - - PowerPoint PPT Presentation

It Probably Works Tyler McMullen CTO of Fastly @tbmcmullen Fastly Were an awesome CDN. What is a probabilistic algorithm? Why bother? In testing primality of very large numbers chosen at random, the chance of stumbling upon a value


slide-1
SLIDE 1

It Probably Works

slide-2
SLIDE 2

Tyler McMullen

CTO of Fastly @tbmcmullen

slide-3
SLIDE 3

Fastly

We’re an awesome CDN.

slide-4
SLIDE 4

What is a probabilistic algorithm?

slide-5
SLIDE 5

Why bother?

slide-6
SLIDE 6

–Hal Abelson and Gerald J. Sussman, SICP

“In testing primality of very large numbers chosen at random, the chance of stumbling upon a value that fools the Fermat test is less than the chance that cosmic radiation will cause the computer to make an error in carrying out a 'correct' algorithm. Considering an algorithm to be inadequate for the first reason but not for the second illustrates the difference between mathematics and engineering.”

slide-7
SLIDE 7

Everything is probabilistic

slide-8
SLIDE 8

Probabilistic algorithms are not “guessing”

slide-9
SLIDE 9

Provably bounded error rates

slide-10
SLIDE 10

Don’t use them if someone’s life depends

  • n it.
slide-11
SLIDE 11

Don’t use them if someone’s life depends

  • n it.
slide-12
SLIDE 12

When should I use these?

slide-13
SLIDE 13

Ok, now what?

slide-14
SLIDE 14

The Count-distinct Problem

slide-15
SLIDE 15

The Problem

How many unique words are in a large corpus of text?

slide-16
SLIDE 16

The Problem

How many different users visited a popular website in a day?

slide-17
SLIDE 17

The Problem

How many unique IPs have connected to a server over the last hour?

slide-18
SLIDE 18

The Problem

How many unique URLs have been requested through an HTTP proxy?

slide-19
SLIDE 19

The Problem

How many unique URLs have been requested through an entire network of HTTP proxies?

slide-20
SLIDE 20

166.208.249.236 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/product/982" ¡200 ¡20246 ¡ 103.138.203.165 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/article/490" ¡200 ¡29870 ¡ 191.141.247.227 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"HEAD ¡/page/1" ¡200 ¡20409 ¡ 150.255.232.154 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/page/1" ¡200 ¡42999 ¡ 191.141.247.227 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/article/490" ¡200 ¡25080 ¡ 150.255.232.154 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/listing/567" ¡200 ¡33617 ¡ 103.138.203.165 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"HEAD ¡/listing/567" ¡200 ¡29618 ¡ 191.141.247.227 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"HEAD ¡/page/1" ¡200 ¡30265 ¡ 166.208.249.236 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/page/1" ¡200 ¡5683 ¡ 244.210.202.222 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"HEAD ¡/article/490" ¡200 ¡47124 ¡ 103.138.203.165 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"HEAD ¡/listing/567" ¡200 ¡48734 ¡ 103.138.203.165 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/listing/567" ¡200 ¡27392 ¡ 191.141.247.227 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/listing/567" ¡200 ¡15705 ¡ 150.255.232.154 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/page/1" ¡200 ¡22587 ¡ 244.210.202.222 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"HEAD ¡/product/982" ¡200 ¡30063 ¡ 244.210.202.222 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/page/1" ¡200 ¡6041 ¡ 166.208.249.236 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/product/982" ¡200 ¡25783 ¡ 191.141.247.227 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/article/490" ¡200 ¡1099 ¡ 244.210.202.222 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/product/982" ¡200 ¡31494 ¡ 191.141.247.227 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/listing/567" ¡200 ¡30389 ¡ 150.255.232.154 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/article/490" ¡200 ¡10251 ¡ 191.141.247.227 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/product/982" ¡200 ¡19384 ¡ 150.255.232.154 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"HEAD ¡/product/982" ¡200 ¡24062 ¡ 244.210.202.222 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/article/490" ¡200 ¡19070 ¡ 191.141.247.227 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/page/648" ¡200 ¡45159 ¡ 191.141.247.227 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"HEAD ¡/page/648" ¡200 ¡5576 ¡ 166.208.249.236 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/page/648" ¡200 ¡41869 ¡ 166.208.249.236 ¡-­‑ ¡-­‑ ¡[25/Feb/2015:07:20:13 ¡+0000] ¡"GET ¡/listing/567" ¡200 ¡42414 ¡

slide-21
SLIDE 21

def ¡count_distinct(stream): ¡ ¡ ¡ ¡ ¡seen ¡= ¡set() ¡ ¡ ¡ ¡ ¡for ¡item ¡in ¡stream: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡seen.add(item) ¡ ¡ ¡ ¡ ¡return ¡len(seen)

slide-22
SLIDE 22

Scale.

slide-23
SLIDE 23

Count-distinct across thousands of servers.

slide-24
SLIDE 24

def ¡combined_cardinality(seen_sets): ¡ ¡ ¡ ¡ ¡combined ¡= ¡set() ¡ ¡ ¡ ¡ ¡for ¡seen ¡in ¡seen_sets: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡combined ¡|= ¡seen ¡ ¡ ¡ ¡ ¡return ¡len(combined)

slide-25
SLIDE 25

The set grows linearly.

slide-26
SLIDE 26

Precision comes at a cost.

slide-27
SLIDE 27
slide-28
SLIDE 28
slide-29
SLIDE 29

“example.com/user/page/1”

slide-30
SLIDE 30

1 ¡0 ¡1 ¡1 ¡0 ¡1 ¡0 ¡1

“example.com/user/page/1”

slide-31
SLIDE 31

1 ¡0 ¡1 ¡1 ¡0 ¡1 ¡0 ¡1

slide-32
SLIDE 32

1 ¡0 ¡1 ¡1 ¡0 ¡1 ¡0 ¡1

P(bit ¡0 ¡is ¡set) ¡= ¡0.5

slide-33
SLIDE 33

1 ¡0 ¡1 ¡1 ¡0 ¡1 ¡0 ¡1

P(bit ¡0 ¡is ¡set ¡ & ¡bit ¡1 ¡is ¡set) ¡= ¡0.25

slide-34
SLIDE 34

1 ¡0 ¡1 ¡1 ¡0 ¡1 ¡0 ¡1

P(bit ¡0 ¡is ¡set ¡ ¡ & ¡bit ¡1 ¡is ¡set ¡ & ¡bit ¡2 ¡is ¡set) ¡= ¡0.125

slide-35
SLIDE 35

1 ¡0 ¡1 ¡1 ¡0 ¡1 ¡0 ¡1

P(bit ¡0 ¡is ¡set) ¡= ¡0.5 ¡ Expected ¡trials ¡= ¡2

slide-36
SLIDE 36

1 ¡0 ¡1 ¡1 ¡0 ¡1 ¡0 ¡1

P(bit ¡0 ¡is ¡set ¡ & ¡bit ¡1 ¡is ¡set) ¡= ¡0.25 ¡ Expected ¡trials ¡= ¡4

slide-37
SLIDE 37

1 ¡0 ¡1 ¡1 ¡0 ¡1 ¡0 ¡1

P(bit ¡0 ¡is ¡set ¡ ¡ & ¡bit ¡1 ¡is ¡set ¡ & ¡bit ¡2 ¡is ¡set) ¡= ¡0.125 ¡ Expected ¡trials ¡= ¡8

slide-38
SLIDE 38

We expect the maximum number of leading zeros we have seen + 1 to approximate log2(unique items).

slide-39
SLIDE 39

Improve the accuracy of the estimate by partitioning the input data.

slide-40
SLIDE 40

class ¡LogLog(object): ¡ ¡ ¡ ¡ ¡def ¡__init__(self, ¡k): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.k ¡= ¡k ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.m ¡= ¡2 ¡** ¡k ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.M ¡= ¡np.zeros(self.m, ¡dtype=np.int) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.alpha ¡= ¡Alpha[k] ¡ ¡ ¡ ¡ ¡ ¡ ¡def ¡insert(self, ¡token): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡y ¡= ¡hash_fn(token) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡j ¡= ¡y ¡>> ¡(hash_len ¡-­‑ ¡self.k) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡remaining ¡= ¡y ¡& ¡((1 ¡<< ¡(hash_len ¡-­‑ ¡self.k)) ¡-­‑ ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡first_set_bit ¡= ¡(64 ¡-­‑ ¡self.k) ¡-­‑ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int(math.log(remaining, ¡2)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.M[j] ¡= ¡max(self.M[j], ¡first_set_bit) ¡ ¡ ¡ ¡ ¡ ¡ ¡def ¡cardinality(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡self.alpha ¡* ¡2 ¡** ¡np.mean(self.M)

slide-41
SLIDE 41

Unions of HyperLogLogs

slide-42
SLIDE 42

HyperLogLog

  • Adding an item: O(1)
  • Retrieving cardinality: O(1)
  • Space: O(log log n)
  • Error rate: 2%
slide-43
SLIDE 43

HyperLogLog

For 100 million unique items, and an error rate of 2%, the size of the HyperLogLog is...

1,500 bytes

slide-44
SLIDE 44

Reliable Broadcast

slide-45
SLIDE 45

The Problem

Reliably broadcast “purge” messages across the world as quickly as possible.

slide-46
SLIDE 46

Single source of truth

slide-47
SLIDE 47

Single source of failures

slide-48
SLIDE 48

Atomic broadcast

slide-49
SLIDE 49

Reliable broadcast

slide-50
SLIDE 50
slide-51
SLIDE 51
slide-52
SLIDE 52

Gossip Protocols

slide-53
SLIDE 53
slide-54
SLIDE 54
slide-55
SLIDE 55

“Designed for Scale”

slide-56
SLIDE 56

Probabilistic Guarantees

slide-57
SLIDE 57
slide-58
SLIDE 58

Bimodal Multicast

  • Quickly broadcast message to all servers
  • Gossip to recover lost messages
slide-59
SLIDE 59
slide-60
SLIDE 60
slide-61
SLIDE 61
slide-62
SLIDE 62
slide-63
SLIDE 63
slide-64
SLIDE 64
slide-65
SLIDE 65

One Problem

Computers have limited space

slide-66
SLIDE 66

Throw away messages

slide-67
SLIDE 67
slide-68
SLIDE 68
slide-69
SLIDE 69
slide-70
SLIDE 70

“with high probability” is fine

slide-71
SLIDE 71

Real World

slide-72
SLIDE 72

End-to-End Latency

133ms

Tokyo

0.00 0.05 0.10 50 100 150

Latency (ms)

slide-73
SLIDE 73

End-to-End Latency

42ms 74ms 83ms 133ms

New York London San Jose Tokyo

0.00 0.05 0.10 0.00 0.05 0.10 0.00 0.05 0.10 0.00 0.05 0.10 50 100 150

Latency (ms) Density

Density plot and 95th percentile of purge latency by server location

slide-74
SLIDE 74

Packet Loss

slide-75
SLIDE 75

Good systems are boring

slide-76
SLIDE 76

What was the point again?

slide-77
SLIDE 77

We can build things that are otherwise unrealistic

slide-78
SLIDE 78

We can build systems that are more reliable

slide-79
SLIDE 79

You’re already using them.

slide-80
SLIDE 80

We’re hiring!

slide-81
SLIDE 81

Thanks

@tbmcmullen

slide-82
SLIDE 82

What even is this?

slide-83
SLIDE 83

Probabilistic Algorithms

slide-84
SLIDE 84

Randomized Algorithms

slide-85
SLIDE 85

Estimation Algorithms

slide-86
SLIDE 86

Probabilistic Algorithms

  • 1. An iota of theory
  • 2. Where are they useful and where are they not?
  • 3. HyperLogLog
  • 4. Locality-sensitive Hashing
  • 5. Bimodal Multicast
slide-87
SLIDE 87

“An algorithm that uses randomness to improve its efficiency”

slide-88
SLIDE 88

Las Vegas

slide-89
SLIDE 89

Monte Carlo

slide-90
SLIDE 90

Las Vegas

def ¡find_las_vegas(haystack, ¡needle): ¡ ¡ ¡ ¡ ¡length ¡= ¡len(haystack) ¡ ¡ ¡ ¡ ¡while ¡True: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡index ¡= ¡randrange(length) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡haystack[index] ¡== ¡needle: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡index

slide-91
SLIDE 91

Monte Carlo

def ¡find_monte_carlo(haystack, ¡needle, ¡k): ¡ ¡ ¡ ¡ ¡length ¡= ¡len(haystack) ¡ ¡ ¡ ¡ ¡for ¡i ¡in ¡range(k): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡index ¡= ¡randrange(length) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡haystack[index] ¡== ¡needle: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡index

slide-92
SLIDE 92

– Prabhakar Raghavan (author of Randomized Algorithms)

“For many problems a randomized algorithm is the simplest the fastest or both.”

slide-93
SLIDE 93

Naive Solution

For 100 million unique IPv4 addresses, the size of the hash is...

>400mb

slide-94
SLIDE 94

Slightly Less Naive

Add each IP to a bloom filter and keep a counter of the IPs that don’t collide.

slide-95
SLIDE 95

Slightly Less Naive

ips_seen ¡= ¡BloomFilter(capacity=expected_size, ¡error_rate=0.03) ¡ counter ¡= ¡0 ¡ ¡ ¡ for ¡line ¡in ¡log_file: ¡ ¡ ¡ ¡ ¡ip ¡= ¡extract_ip(line) ¡ ¡ ¡ ¡ ¡if ¡items_bloom.add(ip): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡counter ¡+= ¡1 ¡ ¡ ¡ print ¡"Unique ¡IPs:", ¡counter

slide-96
SLIDE 96

Slightly Less Naive

  • Adding an IP: O(1)
  • Retrieving cardinality: O(1)
  • Space: O(n)
  • Error rate: 3%

kind of

slide-97
SLIDE 97

Slightly Less Naive

For 100 million unique IPv4 addresses, and an error rate of 3%, the size of the bloom filter is...

87mb

slide-98
SLIDE 98

def ¡insert(self, ¡token): ¡ ¡ ¡ ¡ ¡# ¡Get ¡hash ¡of ¡token ¡ ¡ ¡ ¡ ¡y ¡= ¡hash_fn(token) ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡Extract ¡`k` ¡most ¡significant ¡bits ¡of ¡`y` ¡ ¡ ¡ ¡ ¡j ¡= ¡y ¡>> ¡(hash_len ¡-­‑ ¡self.k) ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡Extract ¡remaining ¡bits ¡of ¡`y` ¡ ¡ ¡ ¡ ¡remaining ¡= ¡y ¡& ¡((1 ¡<< ¡(hash_len ¡-­‑ ¡self.k)) ¡-­‑ ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡Find ¡"first" ¡set ¡bit ¡of ¡`remaining` ¡ ¡ ¡ ¡ ¡first_set_bit ¡= ¡(64 ¡-­‑ ¡self.k) ¡-­‑ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int(math.log(remaining, ¡2)) ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡Update ¡`M[j]` ¡to ¡max ¡of ¡`first_set_bit` ¡ ¡ ¡ ¡ ¡# ¡and ¡existing ¡value ¡of ¡`M[j]` ¡ ¡ ¡ ¡ ¡self.M[j] ¡= ¡max(self.M[j], ¡first_set_bit)

slide-99
SLIDE 99

def ¡cardinality(self): ¡ ¡ ¡ ¡ ¡# ¡The ¡mean ¡of ¡`M` ¡estimates ¡`log2(n)` ¡with ¡ ¡ ¡ ¡ ¡# ¡an ¡additive ¡bias ¡ ¡ ¡ ¡ ¡return ¡self.alpha ¡* ¡2 ¡** ¡np.mean(self.M)

slide-100
SLIDE 100

The Problem

Find documents that are similar to one specific document.

slide-101
SLIDE 101

The Problem

Find images that are similar to one specific image.

slide-102
SLIDE 102

The Problem

Find graphs that are correlated to one specific graph.

slide-103
SLIDE 103

The Problem

Nearest neighbor search.

slide-104
SLIDE 104

The Problem

“Find the n closest points in a d-dimensional space.”

slide-105
SLIDE 105

The Problem

You have a bunch of things and you want to figure out which ones are similar.

slide-106
SLIDE 106
slide-107
SLIDE 107

“There has been a lot of recent work on streaming algorithms, i.e. algorithms that produce an output by making

  • ne pass (or a few passes) over the data while using a

limited amount of storage space and time. To cite a few examples, ...”

{ ¡"there": ¡1, ¡"has": ¡1, ¡"been": ¡ ¡1, ¡“a":4, ¡ ¡ ¡"lot": ¡ ¡ ¡1, ¡"of": ¡ ¡2, ¡"recent":1, ¡... ¡}

slide-108
SLIDE 108
  • Cosine similarity
  • Jaccard similarity
  • Euclidian distance
  • etc etc etc
slide-109
SLIDE 109

Euclidian Distance

slide-110
SLIDE 110

Metric space

slide-111
SLIDE 111

kd-trees

slide-112
SLIDE 112

Curse of Dimensionality

slide-113
SLIDE 113

Locality-sensitive hashing

slide-114
SLIDE 114
slide-115
SLIDE 115
slide-116
SLIDE 116

Locality-Sensitive Hashing for Finding Nearest Neighbors

  • Slaney and Casey
slide-117
SLIDE 117

Random Hyperplanes

slide-118
SLIDE 118

{ ¡"there": ¡1, ¡"has": ¡1, ¡"been": ¡ ¡1, ¡“a":4, ¡ ¡ ¡"lot": ¡ ¡ ¡1, ¡"of": ¡ ¡2, ¡"recent":1, ¡... ¡}

LSH

0 ¡1 ¡1 ¡1 ¡0 ¡1 ¡0 ¡0 ¡0 ¡0 ¡1 ¡0 ¡1 ¡0 ¡1 ¡0 ¡...

slide-119
SLIDE 119

Cosine Similarity LSH

slide-120
SLIDE 120

Firewall Partition

slide-121
SLIDE 121

DDoS

  • `