Wednesday, April 13, 2022

Codeigniter with redis and Memcache Session Drivers

 Ref: https://codeigniter.com/userguide3/libraries/sessions.html


Session Drivers

As already mentioned, the Session library comes with 4 drivers, or storage engines, that you can use:

  • files
  • database
  • redis
  • memcached

By default, the Files Driver will be used when a session is initialized, because it is the most safe choice and is expected to work everywhere (virtually every environment has a file system).

However, any other driver may be selected via the $config['sess_driver'] line in your application/config/config.php file, if you chose to do so. Have it in mind though, every driver has different caveats, so be sure to get yourself familiar with them (below) before you make that choice.

In addition, you may also create and use Custom Drivers, if the ones provided by default don’t satisfy your use case.

Redis Driver

Note

Since Redis doesn’t have a locking mechanism exposed, locks for this driver are emulated by a separate value that is kept for up to 300 seconds.

Redis is a storage engine typically used for caching and popular because of its high performance, which is also probably your reason to use the ‘redis’ session driver.

The downside is that it is not as ubiquitous as relational databases and requires the phpredis PHP extension to be installed on your system, and that one doesn’t come bundled with PHP. Chances are, you’re only be using the ‘redis’ driver only if you’re already both familiar with Redis and using it for other purposes.

Just as with the ‘files’ and ‘database’ drivers, you must also configure the storage location for your sessions via the $config['sess_save_path'] setting. The format here is a bit different and complicated at the same time. It is best explained by the phpredis extension’s README file, so we’ll simply link you to it:

Warning

CodeIgniter’s Session library does NOT use the actual ‘redis’ session.save_handler. Take note only of the path format in the link above.

For the most common case however, a simple host:port pair should be sufficient:

$config['sess_driver'] = 'redis';
$config['sess_save_path'] = 'tcp://localhost:6379';

Memcached Driver

Note

Since Memcache doesn’t have a locking mechanism exposed, locks for this driver are emulated by a separate value that is kept for up to 300 seconds.

The ‘memcached’ driver is very similar to the ‘redis’ one in all of its properties, except perhaps for availability, because PHP’s Memcached extension is distributed via PECL and some Linux distrubutions make it available as an easy to install package.

Other than that, and without any intentional bias towards Redis, there’s not much different to be said about Memcached - it is also a popular product that is usually used for caching and famed for its speed.

However, it is worth noting that the only guarantee given by Memcached is that setting value X to expire after Y seconds will result in it being deleted after Y seconds have passed (but not necessarily that it won’t expire earlier than that time). This happens very rarely, but should be considered as it may result in loss of sessions.

The $config['sess_save_path'] format is fairly straightforward here, being just a host:port pair:

$config['sess_driver'] = 'memcached';
$config['sess_save_path'] = 'localhost:11211';
Bonus Tip

Multi-server configuration with an optional weight parameter as the third colon-separated (:weight) value is also supported, but we have to note that we haven’t tested if that is reliable.

If you want to experiment with this feature (on your own risk), simply separate the multiple server paths with commas:

// localhost will be given higher priority (5) here,
// compared to 192.0.2.1 with a weight of 1.
$config['sess_save_path'] = 'localhost:11211:5,192.0.2.1:11211:1';



No comments:

Post a Comment

Install and use xorg-server on macOS via Homebrew

  The instructions to install and use xorg-server on macOS via Homebrew: Install Homebrew (if you haven't already): /bin/bash -c ...