Slow reading (4s) from ElasticCache/Memcached with Elastic Beanstalk

201 Views Asked by At

I have a problem understanding the interaction between ElasticCache and ElasticBeanstalk. I have created a Memcached cluster in ElasticCache (cache.t3.medium 1 node). ElasticBeanstalk is based on PHP 8.0 running on 64bit Amazon Linux 2/3.3.12.

To get access to memcached on EC2 i have a file .ebextensions with:

packages:
  yum:
    libmemcached-devel: []

commands:
  01_install_memcached:
    command:  /usr/bin/yes 'no'| /usr/bin/pecl install memcached
    test: '! /usr/bin/pecl info memcached'
  02_rmfromphpini:
    command: /bin/sed -i -e '/extension="memcached.so"/d' /etc/php.ini
  03_createconf:
    command: /bin/echo 'extension="memcached.so"' > /etc/php.d/41-memcached.ini

If i connect to Memcached i get very fast a connection. But if i read some key it takes 4 seconds to get the result!

I have tested with Symfony\Component\Cache\Adapter\MemcachedAdapter and native php

$time = microtime( true );
$m = new Memcached();
$m->addServer('<elasticache node endpoint>', 11211);
var_dump($m->get('foo'));
printf('%.5f', microtime( true ) - $time) ;

or

$time = microtime( true );
$memcachedClient = MemcachedAdapter::createConnection('memcached://<elasticache node endpoint>:11211');
$memcachedAdapter = new MemcachedAdapter($memcachedClient, $_ENV['MEMCACHED_NAMESPACE']);
$keyCache = 'utime';
$cacheItem = $memcachedAdapter->getItem($keyCache);
printf('%.5f', microtime( true ) - $time) ;

Any idea why it takes 4 seconds?

1

There are 1 best solutions below

0
On

The 4.0s is a timeout, probably meaning you cannot reach the memcached service. Check your security groups.