Improve PHP Session Performance by Utilizing RAM


RAM is faster than disk. If you have enough RAM, it is a good idea to keep server session files in RAM to improve performance. All session contents will be removed when you restart Apache with php_mod.

Save to RAM Disk

Use RAM disk to replace hard drive is the easiest way is to improve session management performance.

You may create RAM disk in Linux with following command:

mkdir /tmp/ramdisk
chmod 777 /tmp/ramdisk
mount -t tmpfs -o size=512M tmpfs /tmp/ramdisk/

If you are uncomfortable with 777, please read PHP doesn’t have permissions of session file [duplicate] on StackOverflow and change according to how you run PHP programs.

Then Modify session.save_path in php.ini to your RAM disk as below and restart your apache.

session.save_path="/tmp/ramdisk"

PHP Shared Memory

Before you enable this feature, you need to make sure you PHP is compiler with the –with-mm option. Check the Installation and Configuration in PHP manual to compile for your operating system.

If succeed, modify session.save_handler in php.ini from file to mm as below and restart your apache. You may see more in How Sessions Are Support in PHP? by Dr. Hrgong Yang.

session.save_handler = mm

You need to use wincache in Windows according to WinCache Session Handler. mm is not available on PHP for Windows.

Reference

  1. StackOverflow: How to store PHP sessions in APC Cache?
  2. BitrixSoft: Configuring Web Systems For Best Performance: PHP Performance
  3. 海豹雜記:在 Linux 上使用 RAM Disk 提高資料讀寫速度
  4. nixCraft: Linux RAM Disk: Creating A Filesystem In RAM
  5. StackOverflow: PHP doesn’t have permissions of session file [duplicate]
  6. PHP: PHP Manual: Appendices: php.ini directives
  7. PHP: PHP Manual: Runtime Configuration: session.save_path
  8. PHP: PHP Manual: Installation and Configuration
  9. PHP: PHP Manual: Runtime Configuration: session.save_handler
  10. PHP Tutorials – Herong’s Tutorial Examples: How Sessions Are Support in PHP?
  11. PHP: PHP Manual: WinCache: WinCache Session Handler
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s