16

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!

Khalid Khan
  • 2,378
  • 1
  • 8
  • 21
whobutsb
  • 1,015
  • 4
  • 14
  • 28
  • did you tried it with the aws lib if it works? – rsz May 18 '16 at 17:12
  • Hi did u find anything related to this issue? Having similar [issue](http://stackoverflow.com/questions/42949733/runtime-execption-could-not-establish-memcached-connection) – Saumini Navaratnam Apr 28 '17 at 08:09
  • 1
    @SauminiNavaratnam - Unfortunately I haven't. But this issue has become almost non-existent in the last couple of months. I've had excellent stability and connection for a while now. I would recommend installing the AWS Elasticache PHP Client on your server too. – whobutsb Apr 28 '17 at 15:22
  • I think your firewall on aws might block the request, try first with a rule allow all request ingress and egress on both server, elb and memcached, if it's working, then you can revert back and check each one to know which firewall rule is causing problem – Hanlin Wang Feb 13 '18 at 05:32
  • Check the security group. If someone set the access to the Elasticache only for a bunch of IPs and the connection is not open to the world (0.0.0.0/32), you can't access the service without a tunnel. – Lukas Oct 23 '18 at 11:05
  • Check security groups that's IP & port in whitelist – erashdan Oct 30 '18 at 05:26

2 Answers2

1

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

-2

Check what your security group settings are and set them to 0.0.0.0/32(not secure - open to everyone but a good way to test) then if that works just set it to your public ip address which you can find here https://www.whatismyip.com/what-is-my-public-ip-address/.

Logan Craft
  • 419
  • 3
  • 8