Why can't I use Memcached to store sessions?

Source: Internet
Author: User

Why can't I use Memcached to store sessions?

Memcached creator dormano wrote two articles [1] [2] very early and warned developers not to use memcached to store sessions. The reason he gave in the first article is that if memcached is used to store sessions, the memcached cluster will fail (such as memory overflow) or maintenance (such as upgrading, adding, or removing servers), the user cannot log on or be disconnected. In the second article, he pointed out that the recycling mechanism of memcached may cause users to lose line for no reason.

Titas Nork sans nas is the co-founder of Bear Mountain, A DevOps consulting service provider. Since the Ruby/Rails community ignores the issues pointed out in dormano's two articles, he recently wrote an article to further elaborate on this. He believes that the root cause of the problem is that memcached is a system designed to cache data rather than store data, so it should not be used to store sessions.

He thinks that the reason given in the first article is easy to understand, and people often have insufficient understanding of the reasons given in the second article. Therefore, he elaborated on the cause in detail:

Memcached uses the least recently used (LRU) algorithm to reclaim the cache. However, the memcached LRU algorithm is executed for each slab class, rather than for the whole.

This means that if the size of all sessions is roughly the same, they will be divided into two or three slab classes. All other data of roughly the same size will also be placed in the same slab to compete with the Session for storage space. Once the slab is full, even if there is still space in the larger slab, the data will be recycled, rather than put in the larger slab ...... In a specific slab, the oldest Session user will be disconnected. The user will start to drop the line randomly, and the worst thing is that you may not even notice it until the user starts to complain ......

In addition, Nork mongonas mentioned that if new data is added to the Session, a larger Session may cause a disconnection problem.

Someone suggested using the separate memcached cache for Session and other data. However, because memcached's LRU algorithm is local, this method not only causes low memory usage, but also does not eliminate the risk of random disconnection due to Session collection.

If you really want to use memcached to speed up Session reading, you can refer to the memcached + RDBMS (in some cases, NoSQL can also be used) mode proposed by Nork mongonas:

  • When a user logs on, the Session "set" is sent to memcached and written to the database;
  • Add a field to the Session to identify the time when the Session was last written to the database;
  • When each page is loaded, sessions are read from memcached first and sessions are read from the database;
  • After loading N pages or Y minutes, the Session is written to the database again;
  • Get expired sessions from the database and get the latest data from memcached first.

Memcached installation and startup script

Performance problems of using Memcached in PHP

Install Memcached in Ubuntu and its command explanation

Install and apply Memcached

Use Nginx + Memcached's small image storage solution

Getting started with Memcached

For details about Memcached, click here
Memcached: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.