Serving PHP session files from a RAM based disk (tmpfs) for AWS Micro Instances

It’s rare to find a web server with slow disk I/O performance, but Amazon’s EC2 micro-instances are one such example. Their EBS disk subsystem access is rated “low”, and this can have a detrimental effect on HTTP throughput. Static resources can be cached in memory or pushed to CDN, and dynamic pages can be cached with APC (setting apc_stat off on production servers also provides in-memory caching for interpreted PHP bytecode). Also access and error logs can be written to the rsyslog daemon, which will perform asynchronous disk writes or forward logs to a central server.
This leaves sessions, which can be written to a redundant and fault tolerant storage system. But if sessions are stateless, only storing small amounts of reconstructable data (a user ID for example) they do not need redundancy, and writing to disk is acceptable. In order to speed up the disk access, a RAM-based disk can be mounted over the session directory. This has the disadvantage of being volatile – the data is lost in case of a server reboot, or the mount point being unmounted. However if tolerable, storing sessions in RAM insulates the application from poor filesystem performance.
There are two filesystem types distributed with newer kernels: ramfs and tmpfs (kernel 2.4+, previously called shmfs).
One downside of ramfs is you can keep writing data into it until you fill up all memory, and the VM can’t free it because the VM thinks that files should get written to backing store (rather than swap space), but ramfs hasn’t got any backing store. Because of this, only root (or a trusted user) should be allowed write access to a ramfs mount.
A ramfs derivative called tmpfs was created to add size limits, and the ability to write the data to swap space. Normal users can be allowed write access to tmpfs mounts.
source kernel.org
What
- Saves PHP session data to RAM instead of disk
Why
- Faster I/O on systems with low filesystem performance (i.e. Amazon EC2 Micro-Instances)
- Insulate the application from slow disk subsystem
Risks
- Session data is lost on reboot or remounting of filesystem (although online resize is possible)
How
- Stop webserver (in this case Apache) or prevent new sessions being created using the application
- Move the existing sessions directory
mv /var/lib/php/session /var/lib/php/session.old
- Create a new mount point
mkdir /var/lib/php/session
- Write the configuration to
/etc/fstab
echo "tmpfs /var/lib/php/session tmpfs defaults 0 0" >> /etc/fstab
- Mount the tmpfs filesystem
mount /var/lib/php/session
- Copy the existing sessions into the new directory, preserving attributes with
-p
cp -p /var/lib/php/session.old/* /var/lib/php/session
- Set the permissions and owner for the new directory
chown root:apache session -R
chmod 770 session -R
- Restart the web server – existing sessions will be maintained
- Faster I/O on systems with low filesystem performance (i.e. Amazon EC2 Micro-Instances)
- Insulate the application from slow disk subsystem
Risks
- Session data is lost on reboot or remounting of filesystem (although online resize is possible)
How
- Stop webserver (in this case Apache) or prevent new sessions being created using the application
- Move the existing sessions directory
mv /var/lib/php/session /var/lib/php/session.old
- Create a new mount point
mkdir /var/lib/php/session
- Write the configuration to
/etc/fstab
echo "tmpfs /var/lib/php/session tmpfs defaults 0 0" >> /etc/fstab
- Mount the tmpfs filesystem
mount /var/lib/php/session
- Copy the existing sessions into the new directory, preserving attributes with
-p
cp -p /var/lib/php/session.old/* /var/lib/php/session
- Set the permissions and owner for the new directory
chown root:apache session -R
chmod 770 session -R
- Restart the web server – existing sessions will be maintained
- Stop webserver (in this case Apache) or prevent new sessions being created using the application
- Move the existing sessions directory
mv /var/lib/php/session /var/lib/php/session.old
- Create a new mount point
mkdir /var/lib/php/session
- Write the configuration to
/etc/fstabecho "tmpfs /var/lib/php/session tmpfs defaults 0 0" >> /etc/fstab
- Mount the tmpfs filesystem
mount /var/lib/php/session
- Copy the existing sessions into the new directory, preserving attributes with
-pcp -p /var/lib/php/session.old/* /var/lib/php/session
- Set the permissions and owner for the new directory
chown root:apache session -R chmod 770 session -R
- Restart the web server – existing sessions will be maintained
-
Tim Webdev
-
smaftoul
-
http://www.binarysludge.com Andy
-
http://www.binarysludge.com Andy
Recent Posts
- Signs that you’re a good programmer
- Signs that you’re a bad programmer
- How to Test Software (or: Teach Yourself to be a QA)
- Know Your Onions (and Antipatterns)
- Clean Code and Clean TDD Cheat Sheets
- The Definitive Guide to Bash Command Line History
- The analogy of print and code reviews
- Recovering from a forced push to a tracked repository
- 10 CoffeeScript One Liners to Impress Your Friends
- UUID API
Archives
- May 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- November 2012
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- October 2010
- April 2010
- March 2010
Pages
Recent Comments





