class 9: consistent hashing

Post on 14-Dec-2014

885 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Class 9: Consistent Hashing

cs1120 Fall 2011David Evans12 September 2011

Charlottesville Airport, 23 December 2001

Daniel Lewin

Internet Circa 1998

Yahoo.comRouter

Router

Router

Router

Router

Router

Client

Client

Client

Client

google.com, November 1998 from archive.org

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?

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.

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

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.

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.

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!

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”

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)"

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

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.

Defining compute-hash

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

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.

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)))))

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

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.

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.

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.

29th ACM Symposium on Theory of Computing, 1997

Consistent Hashing0

1

½

Akamai

Akamai Headquarters, Cambridge, MA

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

top related