Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

memcached - Is it recommended to store PHP Sessions in MemCache?

I'm working with a couple of Web Servers behind a Load Balancer and I can enable Sticky Sessions to hold a user to the one specific Web Servers - this will work.

I have been reading about PHP Sessions & MemCache. I must say what I've read is a touch confusing as some pages say its a good idea and others the opposite.

Questions:

  1. is it possible to keep php sessions in memcache?
  2. is it better to use sticky sessions over memcache?
  3. what are the problems with php sessions in memcache - note: I can get enough cache (amazon so its expandable).
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1: YES. And I strongly recommend storing PHP sessions in Memcached. Here's why:

Memcached is great for storing small chunks of data that are frequently accessed by the database and filesystem.

Memcached was designed specifically for sessions. It was originally the brainchild of the lead developer of livejournal.com and later used to also cache the content of users' posts. The benefit was immediate: most of the action was taking place in memory. Page load times greatly improved.

Thankfully, PHP and Apache have an easy implementation to handle sessions with Memcached. Simply install with a few shell commands

example for Debian:

sudo apt-get -t stable install php7.4-memcached

and

change your php.ini settings to something similar to:

(taken from https://www.php.net/manual/en/memcached.sessions.php)

 session.save_handler = memcached
 ; change server:port to fit your needs...
 session.save_path = "localhost:11211"

The key is the session.save_path

It will no longer point to a relative file path on your server. APC was mentioned - APC for the caching of .php files used by the program. APC and Memcached will reduce IO significantly and leave Apache/Nginx free to server resources, such as images, faster.

2: No

3: The fundamental disadvantage of using Memcached is data volatility

Session data is not persistent in Memcached. So if and when the server crashes, all data in memory is lost. Everyone will have to log in again.

And then you have memory consumption...

Remember: the sessions are stored in the memory. If your website handles a large number of concurrent users, you may have to shell out a little extra money for a larger memory allocation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...