Skip to content

Distributed Caching

mayank jaglan edited this page Feb 10, 2017 · 22 revisions
Mayank Jaglan (mjaglan)

Problem Statement:

We need a way to provide single logical view (and state) for session and security management using caching for the web applications.

Possible Solutions:

In order to maintain single state of app data and web session data the approach of utilizing distributed cache system is preferred. It is an extension of the traditional concept of cache used on a single machine. A distributed cache system may span multiple servers.

For enabling this feature for Laravel web app framework, there are two out of the box solutions available, and there is also facility for adding your own custom cache driver -

Feature Options
Distributed Cache Memcached, Redis, Custom Cache Drivers
[//]: # (Database Cache MySQL Cache Driver)

Solution Evaluations:

Memcached

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. It is free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

For using the Memcached cache requires the Memcached PECL package is installed. For configuring memcached, edit app/config/cache.php and change the driver to memcached. And for adding multiple memcached servers, use Memcached::addServers() as the update then happens only once. This is an example code snippet -

<?php
$m = new Memcached();

/* Add 2 servers, so that the second one
   is twice as likely to be selected. */
$m->addServer('mem1.domain.com', 11211, 33);
$m->addServer('mem2.domain.com', 11211, 67);
?>

Memcached Pros

  • Low complexity
  • Simple to configure
  • Few command macros == simple to master
  • Atomic increment and decrement
  • Simple to cluster -- uses a hashing algorithm at the client to find keys in a cluster
  • Can withstand a member dying
  • Many years in production

Memcached Cons

  • Doesn't do anything besides be an in-memory key/value store
  • Caches sharded by client do not scale across AWS zones
  • Unbalanced memcached clusters require a full system restart
  • Adding a member to the pool requires reconfiguring and rebooting the client

Redis

It is an open source, in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

For using Redis with Laravel, install the predis/predis package via Composer. A typical cluster of Redis servers has configuration like this -

'redis' => [

    'client' => 'predis',

    'options' => [
        'cluster' => 'redis',
    ],

    'clusters' => [
        'default' => [
            [
                'host' => env('REDIS_HOST', 'localhost'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => 0,
            ],
        ],
    ],

],

The other way is using PhpRedis PHP extension via PECL and then configuring Redis configuration for your application in the config/database.php configuration file. Within this file, there is a redis array containing the Redis servers utilized by your application:

'redis' => [

    'client' => 'phpredis',

    // Rest of Redis configuration...
],

Redis Pros

  • Can store data in a variety of data structures: list, array, sets and sorted sets
  • Pipelining Multiple commands at once
  • Blocking reads -- will sit and wait until another process writes data to the cache
  • Mass insertion of data to prime a cache
  • Partitions data across multiple redis instances
  • Can back data to disk

Redis Cons

  • Can be complex to configure -- requires consideration of data size to configure well
  • SENTINEL, the automated failover which promotes a slave to master, is perpetually on the redis unstable branch
  • Master-slave architecture means if the master wipes out, and SENTINEL doesn't work, the system is sad
  • May require Redis server administration for monitoring, partitioning and balancing

Custom Cache Drivers

Conclusion:

Associated Github issues

Associated Discussions

Developer Mailing List: UI Portal - Load Balancing & High availability

References

Clone this wiki locally