class 9: consistent hashing

29
Class 9: Consisten t Hashing cs1120 Fall 2011 David Evans 12 September 2011

Upload: david-evans

Post on 14-Dec-2014

885 views

Category:

Education


0 download

DESCRIPTION

Danny Lewin, 14 May 1970 - 11 September 2001Internet CachingLookup TablesHash TablesConsistent Hashing

TRANSCRIPT

Page 1: Class 9: Consistent Hashing

Class 9: Consistent Hashing

cs1120 Fall 2011David Evans12 September 2011

Page 2: Class 9: Consistent Hashing

Charlottesville Airport, 23 December 2001

Page 3: Class 9: Consistent Hashing

Daniel Lewin

Page 4: Class 9: Consistent Hashing

Internet Circa 1998

Yahoo.comRouter

Router

Router

Router

Router

Router

Client

Client

Client

Client

Page 5: Class 9: Consistent Hashing

google.com, November 1998 from archive.org

Page 6: Class 9: Consistent Hashing
Page 7: Class 9: Consistent Hashing

Caching the Internet

Yahoo.comRouter

Router

Router

Router

Router

Router

Client

Client

Client

Client

Cache

Cache

A Cache stores previous results.If the result is already known from the same request recently, it returns that result.

Problem: how do Clients and Routers know which cache to try?

Page 8: Class 9: Consistent Hashing

Lookup Table

Key Value

“University of Virginia” “Wahoos”

“Virginia Tech” “Turkeys”

“University of Michigan” “Wolverines”

“MIT” “Beavers”

… …

Each entry is a (Key . Value) pair. To look up the Value associated with a given Key, search the Keys in order until you find one that matches, and then the value is the associated Value.

Page 9: Class 9: Consistent Hashing

Examples

(define schools (list (cons "University of Virginia" "Wahoos") (cons "Virginia Tech" "Turkeys") (cons "MIT" "Beavers") (cons "UC Santa Cruz" "Banana Slugs")))> (table-lookup schools "University of Virginia")"Wahoos"> (table-lookup schools "MIT")"Beavers"> (table-lookup schools "UC Irvine")#f

Page 10: Class 9: Consistent Hashing

Simple Lookup TableDefine a procedure, table-lookup, that takes as inputs a table and a key. If the table contains an entry (key . value), the output is value. If there is no entry whose first part matches key, the output is false.

1. Be very optimistic! Since lists are defined recursively, most problems involving lists can be solved with recursive procedures.

2. Think of the simplest input, something you can already solve. This is the base case. For lists, it is often when the list is null.

3. Consider how you would solve the problem using the result for a slightly smaller version of the problem. For lists, the smaller version of the problem is usually the cdr of the list.

Page 11: Class 9: Consistent Hashing

Simple Lookup TableDefine a procedure, table-lookup, that takes as inputs a table and a key. If the table contains an entry (key . value), the output is value. If there is no entry whose first part matches key, the output is false.

Page 12: Class 9: Consistent Hashing

Simple Lookup TableDefine a procedure, table-lookup, that takes as inputs a table and a key. If the table contains an entry (key . value), the output is value. If there is no entry whose first part matches key, the output is false.

(define (table-lookup table key) (if (null? table) false (if (equal? (car (car table)) key) (cdr (car table)) (table-lookup (cdr table) key))))

This works…but if the table is big (or there are many caches) it is very slow!

Page 13: Class 9: Consistent Hashing

Regular Hash Tablecompute-hash: Key, Size Number

Maps a key (which could be any value) and a table size to a number between 0 and Size – 1. This is the position in the table where the Key should be stored.

> (compute-hash “University of Virginia” 12)8> (compute-hash “Virginia Tech” 12)9….

Index Key Value

0

1

2

3

4

56

7

8

9

10

11

“University of Virginia” “Wahoos”

“Virginia Tech” “Turkeys”

Page 14: Class 9: Consistent Hashing

Typical compute-hash Function

Convert input to a string(format “~a” x) converts any value x to a string

> (format "~a" 23)"23"> (format "~a" "University of Virginia")"University of Virginia"> (format "~a" (cons 1 (cons 2 null)))"(1 2)"

Page 15: Class 9: Consistent Hashing

Typical compute-hash Function

Convert input to a string(format “~a” x) converts any value x to a string

Convert each character in the string to a number(char->integer c) converts any character to a number. > (char->integer #\A)

65> (char->integer #\U)85> (char->integer #\space)32

Page 16: Class 9: Consistent Hashing

Typical compute-hash Function

Convert input to a string(format “~a” x) converts any value x to a string

Convert each character in the string to a number(char->integer c) converts any character to a number.

Compute the sum of all the numbers, modulo the size of the table.

Page 17: Class 9: Consistent Hashing

Defining compute-hash

(define (compute-hash key size) (modulo (sum-chars (string->list (format "~a" key))) size))

Page 18: Class 9: Consistent Hashing

Defining sum-charsDefine a procedure, sum-chars, that takes as input a list of characters, and outputs the sum of all the character values (as converted by char->integer) in the list.

1. Be very optimistic! Since lists are defined recursively, most problems involving lists can be solved with recursive procedures.

2. Think of the simplest input, something you can already solve. This is the base case. For lists, it is often when the list is null.

3. Consider how you would solve the problem using the result for a slightly smaller version of the problem. For lists, the smaller version of the problem is usually the cdr of the list.

Page 19: Class 9: Consistent Hashing

Defining sum-charsDefine a procedure, sum-chars, that takes as input a list of characters, and outputs the sum of all the character values (as converted by char->integer) in the list.

(define (sum-chars p) (if (null? p) 0 (+(char->integer (car p)) (sum-chars (cdr p)))))

Page 20: Class 9: Consistent Hashing

(define (sum-chars p) (apply + (map char->integer p)))

Page 21: Class 9: Consistent Hashing

Use of Hash Table in PS2

(define (memoize f) (let ((ht (make-hash))) (lambda (s1 s2) (let ((key (format "~a#~a" s1 s2))) (hash-ref ht key (lambda () (let ((res (f s1 s2))) (hash-set! ht key res) res)))))))

Note: this uses several things you haven’t seen yet. I won’t try to explain them all now.

Look up the value of key in the hash table, ht. If it has a value, that is the value. If it has no value yet, apply this procedure which finds the value by applying (f s1 s2) and stores it in the table at the location associated with this key.

Page 22: Class 9: Consistent Hashing

Index Key Value01234

56789

1011

Problem with Regular Hashing

> (compute-hash “University of Virginia” 12)8> (compute-hash “Virginia Tech” 12)9….> (compute-hash “Virginia Tech” 13)10

“University of Virginia” “Wahoos”

“Virginia Tech” “Turkeys”

To work, everyone needs to have exactly the same table! This is really hard on the Internet, where nodes leave and join all the time.

Page 23: Class 9: Consistent Hashing

Yahoo.comRouter

Router

Router

Router

Router

Router

Client

Client

Client

Client

Cache

Cache

(find-cache caches url) => evaluates to address of the cache likely to have request cached.

Page 24: Class 9: Consistent Hashing

29th ACM Symposium on Theory of Computing, 1997

Page 25: Class 9: Consistent Hashing
Page 26: Class 9: Consistent Hashing

Consistent Hashing0

1

½

Page 27: Class 9: Consistent Hashing

Akamai

Akamai Headquarters, Cambridge, MA

Page 28: Class 9: Consistent Hashing
Page 29: Class 9: Consistent Hashing

ChargeEnjoy Today’s Fast Internet and

remember Danny Lewin’s contribution (see today’s class post for links to articles)

PS2 Due WednesdayUpcoming Help

Today, noon-1:30pm (Kristina, Rice 1st)Today, 1:15-2:00pm (Dave, Rice 507)Tuesday, 11am-noon (Dave, Rice 507)Tuesday, 5-6:30pm (Valerie, Rice 1st)Tuesday, 6:30-8pm (Jonathan, Rice 1st)

Daniel Lewin14 May 1970 –

11 September 2001