Goodbye AWS ElastiCache and hello Amazon MemoryDB — configuration and basic usage

Amazon MemoryDB - An alternative of AWS Elasticache
Dec 20 2021 · 4 min read

Introduction 

Programmers continuously remain interacted with caching, willingly or unwillingly, during their life.

Caching is what makes our lives easy as a developer. We are not going much deeper in this though, if you want to know more about caching, please have a look at what is cache? how does it work?.

At all the time, we want our end-users to have an uninterrupted experience with our website/application. If our product takes too long to load its page/screen then it becomes, in many of the cases the API or database is the reason behind this to let our end-users served lately. It doesn’t affect how much your APIs/services are scalable at all if they are highly latent.

To reduce API latency we normally use Redis for caching the response, which will be served to end-users till cache expires.

But what if we have something ready to use as a replacement for Redis? Yes, your assumption is correct, we are talking about one AWS Module which finely serves the role of Redis, named — MemoryDB.

Elasticache is also there to serve us as the Redis does. But in this post, we will go through MemoryDB — its basic usage and configuration.

We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!

What is AWS MemoryDB?

A MemoryDB is a Redis-compatible, durable, in-memory database that delivers ultra-fast performance. Also, the key benefit of using it is to pay only for what you use.

How does AWS MemoryDB differ from AWS Elasticache?

Consider using MemoryDB if our workload requires a durable database that provides ultra-fast performance (microsecond read and single-digit millisecond write latency). If you’re building an application using Redis data structures and APIs and need a primary, durable database, MemoryDB may be a good fit for your use case. Finally, you should consider using MemoryDB to simplify your application architecture and lower costs by replacing the usage of a database with a cache for durability and performance.

ElastiCache for Redis is a caching service that is commonly used to cache data from other databases and data stores. Consider ElastiCache for Redis for caching workloads where we want to accelerate data access with our existing primary database or data store (microsecond read and write performance). Also, consider ElastiCache for Redis for use cases where we want to use the Redis data structures and APIs, but don’t require a durable database.

Let’s see how to set it up step by step.
Here I’m assuming that you’re already familiar with AWS EC2 and AWS IAM and their usage.

  1. Create an IAM user if you haven’t, already. Keep in mind to note down the Access key and secret key in case if you’re newly creating it.
  2. Add AmazonMemoryDBFullAccess permission to the IAM user you’ve created in the above step. It comes pre-provisioned with permission that the service requires to create a service-linked role on your behalf.
    You might decide not to use the default policy and instead to use a custom-managed policy. In this case, make sure that you have either permission to call iam:createServiceLinkedRole or that you have created the MemoryDB service-linked role.

Create amazonMemoryDBFullAccess

3. Visit here to create the MemoryDB cluster.

i. Create a cluster with basic details.

Create a cluster with basic details

ii. Add/select subnet groups and choose cluster settings according to your storage requirement.

Add, select subnet groups

choose cluster settings

iii. Configure/choose a security group that takes account of connection ports.

The default port for MemoryDB clusters is 6379.
Let’s create a VPC security group for our EC2 instances, to allow access to the EC2 instance from the Internet via the VPC’s routing table. For example, you can set rules on this security group to allow TCP access to the EC2 instance over port 22 and for Redis port too.

VPC security group for our EC2 instances

4. Finally, ssh on the EC2 instance and try to connect with the cluster.

To use redis-cli to connect to a MemoryDB cluster on Amazon Linux 2 or Amazon Linux,

Run the following commands according to EC2 instance type, If you’re using AWS Linux 2,

$ sudo yum -y install openssl-devel gcc 
$ wget http://download.redis.io/redis-stable.tar.gz 
$ tar xvzf redis-stable.tar.gz 
$ cd redis-stable 
$ make distclean 
$ make redis-cli BUILD_TLS=yes 
$ sudo install -m 755 src/redis-cli /usr/local/bin/

OR if you’re using Amazon Linux,

$ sudo yum install gcc jemalloc-devel openssl-devel tcl tcl-devel clang wget 
$ wget http://download.redis.io/redis-stable.tar.gz 
$ tar xvzf redis-stable.tar.gz 
$ cd redis-stable 
$ make redis-cli CC=clang BUILD_TLS=yes 
$ sudo install -m 755 src/redis-cli /usr/local/bin/

Finally, Connect to the cluster using the following command,

redis-cli -h cluster endpoint --tls -p 6379

Connect to the cluster

That’s it. You can check by running a command ping in the terminal, and if it returns “pong”, that means the memoryDB connection is successful !🎉🍹

Feel free to leave a response to the article if you would like to have some help or want to suggest improvements.

Similar Useful Articles


Code, Build, Repeat.
Stay updated with the latest trends and tutorials in Android, iOS, and web development.
nidhi-d image
Nidhi Davra
Web developer@canopas | Gravitated towards Web | Eager to assist
nidhi-d image
Nidhi Davra
Web developer@canopas | Gravitated towards Web | Eager to assist
canopas-logo
We build products that customers can't help but love!
Get in touch
contact-footer
Say Hello!
footer
Follow us on
2024 Canopas Software LLP. All rights reserved.