We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Redis Frontend/Backend cache very slow

I have been using Redis as a cache and noticed that it was quite slow. I am using the Phalcon\Cache\Frontend\Data and Phalcon\Cache\Backend\Redis as a cache for my models. I've noticed that it is at times 40x slower than using a regular redis connection.

Here is my connections for both (normal frontend/backend cache) and just using Redis:


$di->set('cache', function() use ($config) {
    $redis = new Redis();
    $redis->connect("localhost", "6379");

    $frontend = new Phalcon\Cache\Frontend\Data(array(
        'lifetime' => 3600
    ));

    $cache = new Phalcon\Cache\Backend\Redis($frontend, array(
        'redis' => $redis
    ));

    return $cache;
});

$di->set('cache2', function() use ($config) {
    $redis = new Redis();
    $redis->connect("localhost", "6379");
    return $redis;
});

For a test, in a controller, I will do:


$this->cache->save("test", 123);

I'll load another page once that data is set to not have it be cached in the frontend:


$start = microtime(true);

$this->cache2->get("test");

$end = microtime(true);

echo "Total Time: ".($end - $start)."<br />";

$start = microtime(true);

$this->cache->get("test");

$end = microtime(true);

echo "Total Time: ".($end - $start)."<br />";

I'll run the previous code and get:


Total Time: 0.00025105476379395
Total Time: 0.0061139850616455

Any reason this could be that slow? Is it just overhead with the library?



3.1k

So I changed the backed to Mongo:


$di->set('cache', function() use ($config) {
    $frontend = new Phalcon\Cache\Frontend\Json(array(
        'lifetime' => 3600
    ));

    $cache = new Phalcon\Cache\Backend\Mongo($frontend, array(
        'server'     => "mongodb://localhost",
        'db'         => 'caches',
        'collection' => 'cache'
    ));

    return $cache;
});

And I am getting very quick times:


Total Time: 0.00071096420288086

However that doesn't fix my problem as I'd like to use Redis for this.



3.1k

Looking at some of the other Redis classes for metadata:


$di->set('modelsMetadata', function() use ($config) {
    $redis = new Redis();
    $redis->connect("localhost", 6379);

    return new \Phalcon\Mvc\Model\MetaData\Redis(array(
        "lifetime" => 3600,
        "redis"    => $redis
    ));
});

Even this seems to be quite slow:


$start = microtime(true);

$user = new User();

$metaData = $user->getModelsMetaData();

$attributes = $metaData->getAttributes($user);

$end = microtime(true);

echo "Total Time: ".($end - $start);

Yields the results:


Total Time: 0.010041952133179

Is there any configuration that I'm missing? But it seems to be because of the classes as I am able to connect quickly with my own methods.

edited Oct '15

Same thing happens to me, actually a page with cached data spend 500ms while the non cache only uses 75ms I'm using the standard way with phalcon 2.0.8:

        $di->setShared('cache', function () {
            //Create a Data frontend and set a default lifetime to 1 hour
            $frontend = new \Phalcon\Cache\Frontend\Data(['lifetime' => 172800]);

           $cache = new \Phalcon\Cache\Backend\Redis($frontend, [
                'prefix'     => 'cache_',
                'lifetime'   => 172800,
                'host'       => 'tcp://127.0.0.1',
                'port'       => 6379,
                'persistent' => false,
            ]);

            return $cache;
        });

I looked at the redis server and the latency is perfect for my system so is no redis server problem. Any ideas?

P.D. I was looking if it was the save or the get problem, but it seems that both causes the time to grow as hell.

It seems related to the phalcon debugbar. If I disable it the caché works fine. I didn't look in depth but just disabling it make the page load at full speed with cache.

Interactive debugging always come at a price. It's good to know that you found a root cause... so it's not the framework killing the perfomance, which would be lost point since Phalcon aims at high perfomrance :)



27.0k

Were you sovle this?