107. AWS ElastiCache 02 - qyjohn/AWS_Tutorials GitHub Wiki
(1) More on ElastiCache Memcached
If you install Memcached on your EC2 instance, there is a set of utilities in /usr/share/memcached/scripts that you can use to work with Memcached. In particular, the memcached-tool is useful when working with Memcached. We recommend that you refer to the following documentation on memcached-tool:
When you create an ElastiCache Memcached cluster using the ElastiCache Console, you might have noticed that you have the option to create a cluster with multiple nodes. It should be noted that, strictly speaking, this is not really a cluster, but a set of Memcached servers working independently in parallel. You should at least remember the following:
- Memcached servers do not communicate with each other. This simple architecture enables Memcached to be very fast and effective, but comes with poor reliability.
- There is no master node. All nodes are equal. There is no replication.
- Node selection is done by the client hashing algorithm.
So, when you write an item to your ElastiCache Memcached cluster, you are writing to one of the nodes in the cluster. The node selection is done by the client hashing algorithm. When you read the item from your ElastiCache Memcached cluster, your client knows from which nodes to perform the read operation, using the same hashing algorithm.
(2) Redis Basics
If you want to install Redis on an EC2 instance running Ubuntu 16.04, you can do so using the following commands:
$ sudo apt-get update
$ sudo apt-get install redis-server
$ sudo service redis-server restart
By default Redis runs on port 6379, so you can telnet to port 6379 for a quick test. Similar to the default configuration of MySQL server and Memcached, by default Redis listens on localhost only, not the private IP address of the EC2 instance.
ubuntu@ip-172-31-5-49:~$ telnet localhost 6379
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
^]
telnet> quit
Connection closed.
ubuntu@ip-172-31-5-49:~$ telnet 172.31.5.49 6379
Trying 172.31.5.49...
telnet: Unable to connect to remote host: Connection refused
If you want your self-managed Redis server to provide service for your own VPC, you will need to find the following line in /etc/redis/redis.conf, then replace 127.0.0.1 to the private IP address of your VPC:
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
bind 127.0.0.1
In the same configuration file, uncomment the following line to enable password authentication. Also change "foobared" to your own password.
#requirepass foobared
You will need to restart Redis for the new configuration to take effect. Then you telnet to port 6379 using the private IP address to verify that the new configuration is working. For other EC2 instance in your VPC to connect to your self-managed Redis server, you will need to have the appropriate security groups for both the Redis server and the Redis client.
$ sudo service redis-server restart
$ telnet 172.31.5.49 6379
Trying 172.31.5.49...
Connected to 172.31.5.49.
Escape character is '^]'.
^]
telnet> quit
Connection closed.
On the EC2 instances that will be your Redis client, you might want to install the redis-tools package to use the Redis CLI:
$ sudo apt-get update
$ sudo apt-get install redis-tools
Now we test the Redis server using the Redis CLI:
$ redis-cli -h 172.31.5.49 -a password
172.31.5.49:6379> ping
PONG
172.31.5.49:6379> quit
For more information on Redis CLI and Redis commands, please refer to the following Redis documentation:
(3) ElastiCache Redis
Create an ElastiCache for Redis cluster in your ElastiCache console. When you create a cluster, you have the option to specify the number of replicas. In short, you can think of the Redis server runs on a single master node only, while the replicas contains exactly the same data as the master. When you perform a write, you write to the master. When you perform a read, you can read from either the master or the replica.
Amazon ElastiCache for Redis does not support Redis passwords. This is because of the inherent limitations of passwords stored in a configuration file. Instead of relying on Redis passwords, ElastiCache for Redis clusters use security group for access control.
ElastiCache for Redis allows you to create snapshots and restore from snapshot. ElastiCache for Memcached does not allow you to create snapshots and restore from snapshot.
At this point, you should try to use the AWS CLI to operate your ElastiCache clusters, including Memcached and Redis.
(4) PHP Session Sharing with Redis
On your EC2 instance, edit /etc/php5/apache2/php.ini (Ubuntu 14.04) or /etc/php/7.0/apache2/php.ini (Ubuntu 16.04), make the following modifications. Here we assume that you are using ElastiCache Redis with not password authentication:
session.save_handler = redis
session.save_path = dns-endpoint-of-elasticache-redis-cluster:6379
If you use a self-managed Redis server with password authentication, then the configuration should be:
session.save_handler = redis
session.save_path = "tcp://ip-address:6379?auth=password"
After that you need to restart Apache for the new configuration to take effect. Also, you need to install php-redis on the EC2 instance otherwise things will not work:
$ sudo apt-get update
$ sudo apt-get install php-redis
$ sudo service apache2 restart
Use the browser to browse your website. Then use the Redis CLI to connect to your Redis server to see what is going on there:
~$ redis-cli -h redis-001.rmzfbh.0001.apse2.cache.amazonaws.com
redis-001.rmzfbh.0001.apse2.cache.amazonaws.com:6379> keys *
1) "PHPREDIS_SESSION:eo2v1j4ahou2c244n48p0rf5q2"
redis-001.rmzfbh.0001.apse2.cache.amazonaws.com:6379> get PHPREDIS_SESSION:eo2v1j4ahou2c244n48p0rf5q2
"marker|s:24:\"172.31.5.49 - 1490659769\";"
redis-001.rmzfbh.0001.apse2.cache.amazonaws.com:6379> quit
(6) Using Redis with PHP
The following sample code shows how to use Redis in PHP:
<?php
//Connecting to Redis server
$server = "redis.rmzfbh.ng.0001.apse2.cache.amazonaws.com";
$redis = new Redis();
$redis->connect($server, 6379);
// Set
$redis->set("key-name", "Redis test value");
// Get
echo "Value: ".$redis->get("key-name");
echo "<br>";
// Delete
$redis->delete("key-name");
// Get again
echo "Value: ".$redis->get("key-name");
?>
(7) Homework
Revisit your homework in 106. AWS ElastiCache 01, use ElastiCache for Redis for session sharing and database caching.