Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Memcached notes

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

When you have time to stay at home on weekends, make a memo to memcached, summarize it, and provide you with a reference. If there are any misunderstandings, please correct me.

1. Memcached: a set of software that uses system memory to cache data, which is often used in the back end of dynamic web cluster system and the front end of database. Temporarily cache the database data queried by the web system. When users access the query for the first time, the results are returned to the client through the database query and a copy is also cached to memcached in the form of key/value. When the same data is queried the second time, the service is provided directly by memcached through memcached query, thus reducing the number of visits to the back-end database and reducing the pressure.

2. Memcached: the meaning of existence, improve the access speed (query is much faster than mysql), enhance user experience, and reduce the pressure of access to the back-end database.

3. Memcached nature: data is cached in the form of key/value. The data will be lost after restarting the server, so it cannot be stored persistently.

4. The operation principle of Memcached: by temporarily caching all kinds of data in the database in the planned system memory space, the direct and high concurrency access to the database by the front-end business service can be reduced, thus the concurrency ability of the dynamic service of large-scale website cluster can be improved.

5. When updating the data, how to ensure the consistency of the data?

When the data is updated or deleted, the data in the back-end database will be processed first. At the same time, the old data corresponding to the memcached will be notified of its invalidation and the cached data will be updated. There are two ways: 1, there is a web program to notify updates (recommended) 2. If it is highly concurrent, to prevent the first visit from causing great pressure on the back-end database, you can use relevant mechanisms (such as setting UDFs in the database) to update a copy of the database in advance when there is a data update in the database, so that the web program can hit the new data from memcached when it requests for the first time. Reduce the pressure of access to the back-end database.

Here is the first: directly use the website program to update the memcached cache.

6. Data warm-up: that is, before the business is launched, the data of some commodity classification features that do not need to be changed frequently are updated to the memcached in advance, and then the request of the front-end page is opened, and the first visit can be hit.

7. It can be used as a cluster and session session shared storage.

8. Support distributed cluster caching. Because the data cached by each memcached system is different, you need to implement distributed memcached settings through web programming or by supporting hash algorithm and consistent hashing algorithm, so that users from the same source can access the IP and point to the same memcached to improve the hit rate.

9. Memory management mechanism:

Using the slab memory allocation mechanism: that is, the memory block chunks which allocates the specified size of memory space in advance, when the data is received, it is stored directly, and the speed is very fast. Pros: no memory fragmentation. Disadvantages: it will lead to a waste of memory space, if the pre-divided memory can hold 112 bytes, storing a 100-byte data. 12 bytes of memory is wasted. In this way, developers need to plan and divide memory blocks suitable for size according to their own business data, which can solve the memory space to a certain extent. There is another mechanism, malloc, which dynamically allocates memory space, but needs to calculate the size, divide memory blocks and finally store data, which is much slower and produces memory fragments.

The LRU object clearing mechanism is adopted.

The hash mechanism is used to quickly retrieve item.

10. Memcached laziness detection object and lazy delete object mechanism.

Memcached program: there are two programs: server side and client side. The server is installed in the memcached independent cache server system, and the client is installed in the web program (java, php and other applications are in the same server). The client is actually decompressed into a software library file. Php compiles and loads the memcached module, then specifies the memcached software library file through the php.ini configuration file, and communicates with the server through the client's memcached software library file.

11. Memcached server software installation

Wget http://www.memcached.org/files/memcached-1.4.20.tar.gz

Libevent-2.0.20-stable.tar.gz

Memcached-1.4.20.tar.gz

Tar zxvf memcached-1.4.20.tar.gz

Cd memcached-1.4.20

. / configure-prefix=/usr/local/memcached-1.4.20-with-libevent=/usr/local/libevent-2.0.20/

Make

Make install

Ls-al/ usr/local/memcached-1.4.20/bin | grep memcached

Start the command:

/ usr/local/memcached-1.4.20/bin/memcached-m 128m-p 11211-d-u root

Write the startup entry in rc.local

Vi / etc/rc.local

/ usr/local/memcached-1.4.20/bin/memcached-m 128m-p 11211-d-u root

Multiple instances can be started at the same time through unused ports.

12. Memcached action: write data through the set command, obtain data through the get command, and delete the data through the delete command.

You can use the telnet ip port and the nc command to set memcached

13. Memcached client installation (take php as an example)

# wget http://pecl.php.net/get/memcache-2.2.7.tgz

# tar-zxvf memcache-2.2.7.tgz

# cd memcache-2.2.7

# / usr/local/php/bin/phpize (installation directory of php)

#. / configure-enable-memcache-with-php-config=/usr/local/php/bin/php-config

# make & & make install

Finally, I remind you to generate the module library file memcache.so under the Installing shared extensions: / usr/local/php/lib/php/extensions/no-debug-zts-20100525/ directory.

14. Configure the memcached client to take effect: configure vim php.ini to add the following two lines

Extension_dir = "/ usr/local/php/lib/php/extensions/no-debug-zts-20100525/"

Extension = memcache.so

Or

Extension = / usr/local/php/lib/php/extensions/no-debug-zts-20100525/memcache.so

Check to see if the module works through / usr/local/php/bin/php-m.

15. Memcached management: log in to memcached directly through telnet ip port or monitor and manage through nc.

16. Client page management memcached: manage memcached on the interface by installing the memadmin php tool

17. Scheduling algorithm of distributed memcached cache cluster: 1. Module calculation hash 2, Consistent hash consistent hashing algorithm (recommended)

18. Because memcached temporarily caches data in memory, it will be lost after restart. You can use an enhanced version of the MemcachedDB-compatible persistence tool to cache data on disk at the same time. Or use a redis database for persistent storage.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report