Console Login
Home / Blog / Performance Optimization / Stop Killing Your Database: High-Performance Session Caching with Redis in 2011
Performance Optimization ‱ ‱ 9 views

Stop Killing Your Database: High-Performance Session Caching with Redis in 2011

@

Stop Killing Your Database: High-Performance Session Caching with Redis

It starts with a few slow page loads. Then, the dreaded Too many connections error appears in your MySQL logs. You check top, and your I/O wait is through the roof. The culprit? It’s not your complex join queries—it’s your users logging in.

Storing PHP sessions in a database (or heaven forbid, the file system) is the architectural equivalent of using a semi-truck to deliver a pizza. It works, but the overhead is killing your margins. With the release of Redis 2.0 late last year, we finally have a stable, persistent, and blazing-fast alternative to Memcached that is changing how we architect high-load applications in Norway.

The Problem: Disk I/O vs. RAM

Traditional session handling writes a file to /var/lib/php/session or a row in a MySQL table for every single page request. Even with 15k RPM SAS drives, disk I/O is the bottleneck. If you are running a high-traffic site—say, a news portal covering the Holmenkollen Ski Festival—thousands of concurrent writes will lock your MyISAM tables or saturate your InnoDB buffer pool.

Latency is the enemy. When a user connects from Trondheim to your server in Oslo, you are already fighting network distance. Don't add 50ms of disk seek time on top of it.

Enter Redis: The Structure Store

Redis (Remote Dictionary Server) solves this by holding the dataset in RAM but—unlike Memcached—periodically syncing to disk. This gives you the speed of memory (sub-millisecond reads) with the durability of a database. If your VPS reboots, you don't log out every single user.

Implementation: Ditching files for Redis

Assuming you are running a standard LAMP stack (Linux, Apache/Nginx, MySQL, PHP 5.3) on a CentOS 5 or Debian Lenny system, moving sessions to Redis is straightforward. First, you need the phpredis extension.

Here is the configuration change in your php.ini that makes the magic happen:

; old setting
; session.save_handler = files

; new setting for Redis
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379?weight=1&persistent=1&prefix=PHPREDIS_SESSION:"

This tells PHP to talk directly to the Redis daemon via TCP. The persistent=1 flag is crucial; it keeps the connection open between requests, saving the TCP handshake overhead.

Pro Tip: In redis.conf, ensure you set an appropriate max memory limit and eviction policy so your server doesn't swap.
maxmemory 256mb
maxmemory-policy volatile-lru

Data Sovereignty and The "Personopplysningsloven"

As pragmatic systems architects, we cannot ignore the legal side. Under the Norwegian Personal Data Act (Personopplysningsloven), session data often contains personally identifiable information (PII). Storing this data in volatile RAM clouds hosted in the US (like EC2) can be a grey area regarding Safe Harbor principles.

By hosting on a VPS in Norway, you ensure that session data—which often includes IP addresses and user IDs—never leaves Norwegian soil. This satisfies Datatilsynet guidelines and reduces latency for your local users. It is a win-win for compliance and performance.

The Hardware Reality Check

Even with Redis, the underlying hardware matters. In virtualized environments, "noisy neighbors" can steal your CPU cycles, causing Redis to stutter. This is why OpenVZ containers can be risky for high-performance caches.

At CoolVDS, we utilize KVM virtualization to guarantee resource isolation. Furthermore, while many providers are still spinning 7200 RPM SATA drives, we are aggressively rolling out Solid State Drive (SSD) storage tiers. Redis persistence (RDB snapshots) writes to disk in the background; on a standard drive, this fork operation can cause lag. On our SSD-backed instances, it is instantaneous.

Feature File System Sessions MySQL Sessions Redis on CoolVDS
Speed Slow (Disk I/O) Medium (Query Overhead) Instant (RAM)
Locking File locks (high contention) Table/Row locks Atomic operations
Persistence Yes Yes Yes (AOF/RDB)

Summary

In 2011, there is no excuse for slow session handling. Redis 2.0 provides the persistence we missed in Memcached and the speed we crave over MySQL. Whether you are scaling a Magento store or a custom PHP application, moving sessions to memory is the single most effective optimization you can make today.

Don't let legacy hardware bottleneck your shiny new code. Deploy a KVM-based instance with low latency connectivity to NIX today.

Ready to speed up your stack? Spin up a CoolVDS SSD instance in Oslo now.

/// TAGS

/// RELATED POSTS

Taming Latency: Tuning NGINX as an API Gateway on Linux (2015 Edition)

Is your REST API choking under load? We dive deep into Linux kernel tuning, NGINX upstream keepalive...

Read More →

Stop Letting Apache mod_php Eat Your RAM: The PHP-FPM Performance Guide

Is your server swapping during peak hours? We ditch the bloated Apache mod_php model for the lean, m...

Read More →

Stop Wasting RAM: Migrating from Apache mod_php to Nginx & PHP-FPM on CentOS 6

Is your server swapping out under load? The old LAMP stack architecture is dead. Learn how to implem...

Read More →

PHP-FPM vs mod_php: Tuning High-Performance LAMP Stacks in 2011

Is your Apache server thrashing under load? Stop relying on the bloated mod_php handler. We dive dee...

Read More →

Stop Using mod_php: Optimizing PHP Performance with FPM and Nginx

Is your web server struggling under load? Learn why moving from Apache's mod_php to PHP-FPM and Ngin...

Read More →

Stop Watching 'wa' in Top: Why Spinning Disks Are the Bottleneck in 2011

Is your server load spiking despite low CPU usage? The culprit is likely I/O wait. We break down why...

Read More →
← Back to All Posts