How does memcached work?
Memcached’s magic lies in its two-stage hash approach. It behaves as though it were a giant hash table, looking up key = value pairs. Give it a key, and set or get some arbitrary data. When doing a memcached lookup, first the client hashes the key against the whole list of servers. Once it has chosen a server, the client then sends its request, and the server does an internal hash key lookup for the actual item data. For example, if we have clients 1, 2, 3, and servers A, B, C: Client 1 wants to set key “foo” with value “barbaz”. Client 1 takes the full list of servers (A, B, C), hashes the key against them, then lets say ends up picking server B. Client 1 then directly connects to server B, and sets key “foo” with value “barbaz”. Next, client 2 wants to get key “foo”. Client 2 runs the same client library as client 1, and has the same server list (A, B, C). It is able to use the same hashing process to figure out key “foo” is on server B.
Being a distributed memory cache, memcached works by utilizing two hashes; behaving simply as a giant hash table for looking up key/value pairs. When finding an object in memcached, the client first hashes the key against the entire array of servers. Once a server has been chosen, the client then issues its request to that server, and the server performs an internal hash key lookup for the actual item data. If the data is found, it is returned to the client.