Consistent Hashing Demo

A simple demo of consistent hashing.

Features

These features have been implemented:

  • Core consistent-hashing-algorithm
  • Consistent Hashing with Bounded Loads, according to Consistent Hashing with Bounded Loads
  • Customize hash function support
  • Customize replica number on hash ring support
  • A simple load-balance proxy demo
  • A full demo to show how consistent-hashing-algorithm works

How to use

Step 1: Start Server

First, start the proxy server:

$ go run main.go

start proxy server: 18888

This will start the proxy server on port 18888.

The server will listen for incoming registration and will forward key searching to the servers.

Then, start the k-v server:

$ go run server/main.go

start server: 8080

Also, the server will register to the proxy server:

register host: localhost:8080 success

Notice: you can register multiple servers to the proxy server:

go run server/main.go -p 8081
go run server/main.go -p 8082
……

Step 2: Query

Use curl to get the key from proxy:

$ curl localhost:18888/key?key=123

key: 123, val: hello: 123

If you query the key at the first time,the key will be cached on the corresponding server for 10s.

The log for proxy server:

Response from host localhost:8080: hello: 123

The log for k-v server:

cached key: {123: hello: 123}
removed cached key after 3s: {123: hello: 123}

Step 3: Try Query Different Key

You can try to query different key, to check whether they are cached on the different servers:

Response from host localhost:8082: hello: 45363456
Response from host localhost:8080: hello: 4
Response from host localhost:8082: hello: 1
Response from host localhost:8080: hello: 2
Response from host localhost:8082: hello: 3
Response from host localhost:8080: hello: 4
Response from host localhost:8082: hello: 5
Response from host localhost:8080: hello: 6
Response from host localhost:8082: hello: sdkbnfoerwtnbre
Response from host localhost:8082: hello: sd45555254tg423i5gvj4v5
Response from host localhost:8081: hello: 0
Response from host localhost:8082: hello: 032452345

Reference

Reference:

Linked Blog:

GitHub

View Github