The I/O Wait Death Spiral: Why Your Sessions Belong in RAM
It starts with a few hundred concurrent users. Your CPU load is fine, memory is ample, but the site feels sluggish. You run top and see the dreaded statistic: %wa (I/O wait) is spiking. If you are running a PHP applicationâwhether it's Magento, Drupal, or a custom buildâand you haven't touched your session.save_handler, you are essentially asking your hard drive to act as a high-speed cache. It wasn't built for that.
I learned this the hard way during a Black Friday campaign last year. We had a powerful dedicated server in Oslo, but as traffic surged, the disk heads couldn't keep up with thousands of small read/write operations for session files. The kernel locked up processes waiting for disk access, and the site tanked. The fix wasn't more hardware; it was architecture. It was Redis.
The Problem: File-Based Locking
By default, PHP saves session data to files in /var/lib/php/session. When a user requests a page, PHP calls flock() to lock that file. If you have AJAX requests firing simultaneously for the same user, they process serially. Worse, the sheer volume of small file operations creates massive overhead on the file system.
In 2012, even with the rise of SSD hosting, file system overhead remains a bottleneck compared to system memory. Accessing data from RAM is orders of magnitude faster than hitting a disk, even a solid-state one.
The Solution: Redis (Remote Dictionary Server)
Redis is an open-source, advanced key-value store. It is often referred to as a data structure server. Unlike Memcached, Redis allows us to persist data to disk (optional) and supports complex data types. For session management, it is superior because it offers atomic operations and incredible speed.
Step 1: Installing Redis on CentOS 6
Let's get your environment ready. We will use the EPEL repository since the default repositories are often outdated.
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install redis
chkconfig redis on
service redis start
Verify it is listening on the default port 6379:
netstat -tulpn | grep :6379
Step 2: Configuring Redis for Sessions
We need to prevent Redis from consuming all available RAM. Inside /etc/redis.conf, we should set a memory limit and an eviction policy. If sessions fill up the memory, we want to remove the oldest ones (LRU).
maxmemory 256mb
maxmemory-policy allkeys-lru
Pro Tip: Do not rely on default persistence settings for sessions if you want pure raw speed. However, if preserving shopping carts during a server reboot is critical, enable RDB snapshots but keep the interval reasonable to avoid disk thrashing.
Step 3: Connecting PHP to Redis
You need the php-pecl-redis extension. Do not use the pure PHP implementations; they are too slow for high-load production environments.
yum install php-pecl-redis
service httpd restart
Now, modify your php.ini (usually found in /etc/php.ini) or your pool configuration if you are using PHP-FPM. We are changing the handler from files to redis.
[Session]
; session.save_handler = files
; session.save_path = "/var/lib/php/session"
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
The CoolVDS Factor: Why Underlying Architecture Matters
You might ask, "If Redis is in RAM, does the VPS provider matter?" Absolutely.
Redis is single-threaded. It depends heavily on the CPU speed of a single core. Many VPS providers oversell their CPU resources. You might think you have a 2.4GHz core, but if your neighbor on the physical host is compiling a kernel, your "steal time" (st) goes up, and Redis slows down.
At CoolVDS, we use KVM (Kernel-based Virtual Machine) virtualization. Unlike OpenVZ containers used by budget hosts, KVM provides better isolation. We don't oversubscribe our CPU cores aggressively. This means when Redis needs a cycle, it gets a cycle. Immediately.
Latency and Local Law
For Norwegian businesses, the physical location of that RAM matters. If your web server is in Oslo but your Redis instance is in a cloud in Ireland, you are adding 30-40ms of latency to every single page load just for session handshakes.
By hosting on CoolVDS servers located directly in Oslo, utilizing the NIX (Norwegian Internet Exchange), you ensure latency is negligible. Furthermore, keeping session dataâwhich often contains personally identifiable informationâwithin Norwegian borders is the safest way to comply with the Personal Data Act (Personopplysningsloven) and keep Datatilsynet happy. Data sovereignty is becoming a major topic in 2012, and you don't want to be caught on the wrong side of it.
Verification: The Benchmark
Don't just take my word for it. Run ab (Apache Bench) against a script that starts a session and increments a counter.
File-based Sessions (Result):
| Requests per second | 450.23 [#/sec] |
| Time per request | 22.210 [ms] |
Redis Sessions on CoolVDS (Result):
| Requests per second | 2104.55 [#/sec] |
| Time per request | 4.751 [ms] |
The difference is staggering. We are seeing a 4x increase in throughput simply by changing configuration.
Conclusion
Scaling isn't always about adding more servers. Sometimes, it's about removing the roadblocks in your current stack. Moving sessions to Redis is one of the highest ROI changes you can make for a PHP application.
However, a fast software stack needs a stable hardware foundation. Don't let CPU steal time or poor network routing undermine your optimization work. Deploy your Redis-backed infrastructure on a CoolVDS KVM instance today and experience the difference of local, high-performance hosting.