This is the document about what I was stuck with when implementing Redis into an existing service.
Background
Last week, I needed to set up Redis for an existing service. I’ve worked with it many times and know what it is and how it is. However, setting up Redis is different from just using it. Also, setting up in production is totally different from setting it up locally. I had set it up in production twice before but I didn’t remember how to do that well. Then I got stuck with a few things. So I decided to document how to do it.
What I did
Just creating a new Redis in AWS is pretty straightforward. All you need to do is just go to the page for ElastiCache and create a new Redis node. But I got stuck with or made mistakes about three things.
The first one is quite a basic thing. I needed to put the Redis node in the same VPC as my existing server. When I set it up with the default/recommended settings, it created a new VPC and couldn’t connect with the server.
So I needed to choose to customize the settings and select my VPC ID which is the same as the server’s one
Secondly, I needed to use rediss:
not redis:
. After I created a cache node, I got an endpoint but it didn’t include the protocol. So I needed to add the protocol by myself when I set the REDIS_URL
in my server application. At first, I didn’t notice the encryption (SSL/TLS) was automatically turned on and used the redis:
for the protocol. Then the production connection with the Redis kept failing forever. Then I looked twice at the settings and I found it enabled.
After I changed redis:
to rediss:
, the connection succeded.
Lastly, I needed to set up a security group for Redis's request. I could make a connection between the server and the Redis when the production server started. However, it raised 500 errors in the server when it sent a request to the Redis. After researching, I found the tutorial provided by AWS.
I seemed to need a security group to allow my EC2 to send/receive requests to port 6379, which is the default port for Redis. I just followed the steps described in the tutorial.
1. Sign in to the AWS Management Console and open the Amazon VPC console at https://console.aws.amazon.com/vpc.
2. In the navigation pane, choose Security Groups.
3. Select or create a security group that you will use for your Cluster instances. Under Inbound Rules, select Edit Inbound Rules and then select Add Rule. This security group will allow access to members of another security group.
4. From Type choose Custom TCP Rule.
1. For Port Range, specify the port you used when you created your cluster.The default port for Redis clusters and replication groups is 6379.
2. In the Source box, start typing the ID of the security group. From the list select the security group you will use for your Amazon EC2 instances.
5. Choose Save when you finish.
Then I added the security group to my EC2 and the Redis.
Finally, my server made requests to Redis and got data.
That’s it!