2

I have three servers for single domain

I'm using nginx as loadbalancer.

I want to share php sessions across these servers.

My application is heavily dependent on session. storing sessions in files is bad idea. i'm using memcached for this.

how exactly and efficiently should i configure memcached to read and write sessions and share between servers quickly.

or any other good alternative suggestion.

Maxime
  • 8,645
  • 5
  • 50
  • 53
Ningappa
  • 1,279
  • 4
  • 16
  • 26

1 Answers1

4

As I read your question, it looks like you installed Memcache on every server (and that's why you ask about "sharing between servers" ?).

What I would do is have a server where you ONLY have Memcached on it. Each web server would connect to your instance of Memcache. You can also have a pool of Memcache servers if needed and Memcache will take care of distributing your data and sessions correctly.

First, you may want to change the way sessions are handled in PHP (for each server) in order to read session's data in Memcache. Your php.ini file will need this:

[Session]
; Handler used to store/retrieve data.
session.save_handler = memcache[d] ; memcache or memcached
session.save_path = "127.0.0.1:11211"

See how Session Handlers work. Note that you can use memcache or memcached extension. They are not the same.

Here is the documentation for both extensions:

If you want more details about the right memcache to pick, I suggest you check this: https://serverfault.com/questions/63383/memcache-vs-memcached

Note that storing sessions in Memcache can be problematic. If Memcache is stopped (for whatever reason) you will loose all data you have in it. You may want to consider storing your sessions in a database and also have them in Memcache to speedup the process.

You can build a custom Session Handler to do that and make sure it suits your needs. You can read more about The SessionHandler class.

Finally, if you are open to suggestion, I would also consider using Redis instead of Memcache as it offers more features and will enable you to reload data if shutdown correctly.

Community
  • 1
  • 1
Maxime
  • 8,645
  • 5
  • 50
  • 53
  • Is memcached need RAM or Disk. I can create one GCE instance for memcache. so that writing sessions in to only one server. and reading from it. with muliple servers – Ningappa Apr 22 '15 at 14:36
  • Memcache stands for "Memory Cache". It uses RAM memory. Using the disk would be really slow comparing to RAM. – Maxime Apr 22 '15 at 20:05
  • Memcached port is autoclosing. What would be the issue..? BTW i Created one server for memcache of 104GB ram. My application giving some warning......................................... Failed to write session data (memcached). Please verify that the current setting of session.save_path is correct (XXX.XXX.XXX.XXX:11211?persistent=1&weight=1&timeout=1&retry_interval=15). – Ningappa Apr 24 '15 at 13:32
  • Can you try adding "tcp://" to your session.save_path so that it looks like this: `session.save_path = "tcp://127.0.0.1:11211"`. Make sure your session size is not over 1MB. This is the default memcache object size. – Maxime Apr 26 '15 at 18:58