Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP AWS ElastiCache Connection Failure

Recently I just started using AWS ElastiCache for a Laravel application. The application is running on 2 instances behind a ELB and handles about 6-10 request/second. Everything was going fine when I launched the application but then I started to receiving connection errors to the application with high latency and timeouts. The error messages was as follows:

[2016-05-17 07:28:25] production.ERROR: exception 'RuntimeException' with message 'Could not establish Memcached connection.' in /srv/ensemble/laravel/vendor/laravel/framework/src/Illuminate/Cache/MemcachedConnector.php:38 Stack trace: 
#0/srv/ensemble/laravel/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php(164): Illuminate\Cache\MemcachedConnector->connect(Array)
#1 /srv/ensemble/laravel/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php(102): Illuminate\Cache\CacheManager->createMemcachedDriver(Array) #2 /srv/ensemble/laravel/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php(77): Illuminate\Cache\CacheManager->resolve('memcached')...

For my setup I'am using:

  • Laravel 5.2
  • AWS ElastiCache t2.small
  • php5-memcached module libmemcached 1.0.18

To solve my issue for the time being I have installed memcached on a separate EC2 instance and have had no issues.

My question is, do I need to use AWS ElastiCache PHP Client instead of the php5-memcached to use Elasticache? I was under the impression that Elasticache was a drop in replacement for Memcached and could be used with out a problem.

Thank you for the help!

like image 620
whobutsb Avatar asked May 18 '16 17:05

whobutsb


1 Answers

There are few important consideration when using manage services in AWS such as Elasticache.

  1. By default AWS Elasticache is not publicly accessible. Accesible only through Internal IP. (But there are work arounds for public access options like VPN connection, Direct Connect, basion host etc.)
  2. Elasticache offers no security e.g. password(that is why it's important to restrict it using network Nacl or application later Security Group)

Regarding your issue, the application cannot established connection to the cache instance, this error shows that the problem occurs somewhere in the network. This issue is very common.

And most likely caused by

  1. The EC2 instance doesn't have routes that can be used to access the elasticache endpoint. This issue occurs if you provisioned the Elasticache from a different VPC (different from VPC used by your application EC2). To solve this you either move the cache to the same VPC where the EC2 reside or You need to Peer the 2 VPC and create local route to establish connection.

  2. The EC2 instance has the valid route or they are in the same VPC however it's blocked by Elasticache SecurityGroup(SG). To solve this you need to check whether your EC2 Private IP is listed to Elasticache SG inbound rules with the memcache port. allow IP 172.0.1.1/32 to port 11211

enter image description here

like image 59
Edcel Cabrera Vista Avatar answered Oct 21 '22 07:10

Edcel Cabrera Vista