We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Cache and process synchronisation

Do Phalcon Cache do any synchronisation? Im refering to https://docs.phalcon.io/en/latest/reference/cache.html.

For Example, i have busy site with about 50 requests per second at peek, now i want to cache something which creating taking very long time, lets say: 2 seconds.

In scenarion when item does not exists in the cache and must be created, without any synchronisation it will cause about 100 php-fpm processes during that 2 sec , to create the item in the same time, and then put it 100 times into cache, causing massive performance-down spike.

With proper synchronisation only one process is creating item for particular key at the same time (other processes can create other items for other keys at the same time) and rest of them waiting/sleeping until item is created and cached, checking let's say every 50 ms if the item is already created or not.

So is synchro implemented in Phalcon cache in any way - semaphores or something?

edited Mar '14

I think that there are no one and only way to do it automagicaly.
Phalcon have got multi level cache https://docs.phalcon.io/en/latest/reference/cache.html#multi-level-cache

At your case 50 request per second i advice you to register redis in DI

$di->setShared("redis", function() {
        $redis = new Redis();
        $redis->connect('', 6379);
        return $redis;

and do something like this

class IndexController extends \Phalcon\Mvc\Controller
    public function testAction()

    public function get_cached_data()
            if($this->redis->ttl('key') < 60)
                $this->redis->setTimeout('key',360); //set ttl to max that other requests don't execute set_cached_data()
                return $this->set_cached_data();
            return $this->redis->get('key');
            $this->redis->setex('key',360,'soon'); //some request (2s) will return soon than real data
            return return $this->set_cached_data();

    public function set_cached_data()
            time consuming things to create cache
        $generated_data = "hello";



I'm interested in this question too. I'm in the same situation with a new busy site. I hoped that phalcon had some lock mechanism in the cache backend. Is it this problem solved? Does the developer solve this problem? Thanks

edited Oct '17

Hello, to resolve this issue I have created the PHP No Slam Cache library, give it a try:


If you have any question or need additional cache backend (like Redis) let me know.

Edit: Wow... i just realised the last answer was in Feb... but I've received notification about it few minutes ago, lol...