Connect AWS Elasticache Memcached via EC2 or Server

By default, Memcached does not provide own client testing utility like ‘redis-cli’ but we can use ‘telnet’ or ‘nc’ command to connect to memcached node to execute all memcached supported commands.

Note: Please keep in mind that, Elasticache service is accessible within the same VPC by default and please make sure your client side EC2 instance is white listed by the security group of your Memcached cluster.

  • You can refer to this following instruction to use “telnet” to connect .

Example: ``` [ec2-user@ip-172-31-46-228 ~]$ telnet mem15.xxxx.0001.euw1.cache.amazonaws.com 11211 Trying 172.31.8.234... Connected to mem15.xxxx.0001.euw1.cache.amazonaws.com . Escape character is '^]'. set key1 0 0 4 test STORED get key1 VALUE key1 0 4 test END stats items STAT items:1:number 1 STAT items:1:age 19 STAT items:1:evicted 0 STAT items:1:evicted_time 0 STAT items:1:outofmemory 0 STAT items:1:tailrepairs 0 END flush_all OK get Test END version VERSION 1.2.8 quit Connection closed by foreign host. [ec2-user@ip-172-31-46-228 ~]$ ```

  • As we discussed you can also use “nc” command to send command to the cluster directly.

Example: ``` [ec2-user@ip-172-31-46-228 ~]$ /bin/echo -e 'get key1' | nc mem15.xxxx.0001.euw1.cache.amazonaws.com 11211 VALUE key1 0 4 test END ```

Some Useful Memcached Commands Syntax:

  • To store data in Memcached server with telnet:
1set KEY META_DATA EXPIRY_TIME LENGTH_IN_BYTES
  • To retrieve data from Memcached through telnet:
1get KEY
  • To overwrite the existing key:
1replace KEY META_DATA EXPIRY_TIME LENGTH_IN_BYTES
  • To delete the key:
1delete KEY
  • To get the server statistics:
1stats
2stats items
3stats slabs
  • To clear the cache data:
1flush_all
  • To quit the telnet session:
1quit

I :heart: AWS! :smile: Enjoy