Home  >  Article  >  Database  >  How does Memcached work?

How does Memcached work?

藏色散人
藏色散人Original
2018-09-13 17:04:591534browse

This article mainly introduces the working principle of Memcached to you.

The atom processed by Memcached is each (key, value) pair (hereinafter referred to as kv pair). The key will be converted into a hash-key through a hash algorithm to facilitate search, comparison and hashing as much as possible. . At the same time, memcached uses a two-level hash, which is maintained through a large hash table.

Memcached consists of two core components: server and client. In a memcached query, the client first determines the server where the kv pair is located by calculating the hash value of the key. Location. When the server is determined, the client will send a query request to the corresponding server to let it find the exact data. Because there is no interaction or multicast protocol between them, the impact of memcached interaction on the network is minimized.

Example: Consider the following scenario. There are three clients, c1, c2, c3, and three ms, s1, s2, s3:

Set kv pair
c1 I want to set key="com", value="iQiyi"
c1 Get the server list, perform hash conversion on the key, and determine the server location where the kv pair is stored based on the hash value
s2 was selected
c1 Connect to s2, s2 receives the request and saves (key=”com”, value=”iQiyi”)

Get the kv pair
c3 wants to get the value of key=”com”
c3 uses the same hash algorithm to calculate the hash value, and determines that the value of key="aa" exists on s2
c3 connects to s2 and gets value="iQiyi" from s2
Any other data from c1, c2 , requests from c3 that want to get the value of key="com" will be sent to s2

If you want to learn more about Memcached, you can follow the Memcached video tutorial or on the PHP Chinese website Memcached Manual.

The above is the detailed content of How does Memcached work?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn