Redis on AWS Made Easy: Compare, Choose, and Launch with ElastiCache for Free

Aarush LuthraAarush Luthra
6 min read

📘Introduction: Getting Started with Redis on AWS

Most of you have probably heard about Redis, the famous in-memory database that’s used almost everywhere, from small startup projects to big tech giants. Widely know for being fast⚡, reliable🔒and flexible, it helps in caching, session storage, real-time analytics and a lot more.

Running Redis on your own means managing scaling, replication, failovers, and availability. Any failure, especially in production can be catastrophic.

AWS Managed Redis Options: ElastiCache and MemoryDB

To avoid all the hassle of setting up and managing in-memory data stores, AWS offers two managed Redis services.

AWS ElastiCache

ElastiCache is a fully managed in-memory data store and cache service that supports Redis, Memcached and Valkey. It’s ideal for scenarios when we want fast access to frequently-used data like user sessions, page views, and queues. It offers two flexible deployment types namely, Serverless (provides effortless autoscaling) and Cluster-based where you design your own setup.

🛡️MemoryDB

MemoryDB combines low-latency performance of Redis with added benefits of high availability and durability. it comes with Multi-AZ support, automatic failover, and persistent storage via a transactional log, making it suitable for using it as a primary database, not just a cache.

However, if you're just starting out or want to test it for learning purposes, cost can be a barrier. While MemoryDB offers 750 free hours for 2 months, ElastiCache gives you 750 hours per month for the entire Free Tier period (12 months)

📝What you’ll explore and learn in this blog

  • 🆚Compare Redis OSS, Memcached, and the newer Valkey.

  • 🛠️Show you how to spin up a Redis OSS cluster for free using AWS Free Tier 🆓 using CloudShell

  • 📊And demonstrate a real-world use case at the end.

⚖️Redis OSS vs Memcached vs Valkey

Let’s go over the key differences between Redis OSS, Memcached, and Valkey. All three are in-memory data stores, but they for slightly different purposes.

Redis OSS: It’s an open source in-memory key-value store that supports a wide range of data types such as lists, sets, bit arrays. It’s ideal for caching, queues, pub/sub. It offers memory persistence through regular database snapshots (RDB) or via the append-only file (AOF) which is a log, capturing all the performed operations. Redis has built-in replication (Master-Slave Redis Sentinel and Cluster mode).

Memcached: It’s one of the simplest open source key-value stores you can find. It’s super fast, lightweight and a great choice when you need a quick caching layer without any advanced features. This simplicity comes with some tradeoffs such as it handles only strings, doesn’t support persistence and lacks native replication support.

Valkey: Sounds unfamiliar? Launched in 2024 as fork of Redis 7.2 after it was announced that the future versions of Redis would no longer remain open source. It is a community driven key-value store providing features of Redis 7.2 without any licensing fee.

Redis vs Memcached: Which Fits Your Caching Needs?

☁️ Setting Up Redis OSS on AWS Elasticache via CloudShell

Now it’s time we set our infrastructure up! Using CloudShell, we’ll spin everything up. No need to worry about having CLI and configuring it on your system

  1. Open CloudShell from your console

  2. Once CloudShell is running, we’ll run a series of commands.

  3. Redis OSS Cluster is launched inside a VPC Cluster. To keep things simple, let’s use the default VPC. Firstly. we’ll find the VPC ID and then list the subnets associated with the VPC. To create the cluster, we’ll first need the subnet ID from this VPC

     #Find the default VPC ID
      aws ec2 describe-vpcs   --filters "Name=isDefault,Values=true"   --query "Vpcs[0].VpcId"  
    
     #Finding the Subnet IDs associated with the VPC.
     #Make sure to replace <vpc-id> with your VPC ID
     aws ec2 describe-subnets \
       --filters "Name=vpc-id,Values=<vpc-id>" \
       --query "Subnets[*].SubnetId" \
    
     #Sample Command to get the Subnet ID
     aws ec2 describe-subnets --filters "Name=vpc-id,Values=vpc-06ae0857425ccf225" --query "Subnets[*].SubnetId"
    
  4. Next, we’ll create a cache subnet group. Add the subnets you received after running the above commands.

     aws elasticache create-cache-subnet-group \
       --cache-subnet-group-name pagecount-subnet-gp \
       --cache-subnet-group-description "Subnet group for pagecount" \
       --subnet-ids <subnet1> <subnet2> \
       --region eu-north-1
    

    Let’s see how do things look in the CloudShell

    We can confirm the creation of subnet-group via console as well

  5. Before we begin the cluster creation. We need two more things, i.e the security group and an auth-token.

    We’ll create a security group that allows ingress on port 6379

     #Creating a security group
     aws ec2 create-security-group \
       --group-name pagecount-sg \
       --description "Security group for Redis" \
       --vpc-id <your-default-vpc-id> \  #Make sure to enter your VPC ID
       --region eu-north-1
    
     #After running the above command, you'll get Security group ID
     #Use it in the below command, where we'll add ingress rule for port 6379
     aws ec2 authorize-security-group-ingress \
       --group-id <your-sg-id> \
       --protocol tcp \
       --port 6379 \
       --cidr 0.0.0.0/0    #For Demo Only, it's better to use your IP for better security
    

    As the cluster will have TLS encryption enabled, we also need to set up a secure authentication token. This token will help in ensuring only authorized clients can connect to the Redis.

    Generate the token using

     openssl rand -base64 32
    
  6. Now, it’s time to create the replication cluster. We’ll use cache.t3.micro node

     aws elasticache create-replication-group \
       --replication-group-id pagecount \
       --replication-group-description "Redis cluster for pagecount with TLS" \
       --engine redis \
       --engine-version 7.0 \
       --cache-node-type cache.t3.micro \
       --num-node-groups 1 \
       --replicas-per-node-group 0 \
       --security-group-ids <your-security-group-id> \
       --cache-subnet-group-name pagecount-subnet-group \
       --auth-token <GeneratedRedisTokenBase64> \
       --transit-encryption-enabled \
       --at-rest-encryption-enabled \
       --region eu-north-1
    

    Your CloudShell will look something like

Let’s wait for the cluster to spin up. Run the following command, this displays the current status of your cluster.

#Running this command will pro
aws elasticache describe-replication-groups \
  --replication-group-id pagecount \
  --region eu-north-1 \
  --query "ReplicationGroups[0].Status"

It took me approximately 10mins⌛and to have cluster up and running

👀 See Who’s Visiting — Tracking Page Views with Redis (A sample use-case)

Before we begin, it is important that the EC2 instance be in the same VPC as your ElastiCache cluster

  1. Create an EC2 instance. Choose Amazon Linux as your AMI. Importantly, choose the default VPC and the security group we created earlier.

  2. SSH into your instance. Install the required dependencies such as express, redis

  3. Use the following Nodejs code snippet to connect to Redis

     const redisClient = redis.createClient({
       url: process.env.REDIS_URL,
       socket: {
         tls: true, 
       },
     });
     redisClient.on('error', (err) => console.error('Redis error:', err));
     (async () => {
       try {
         await redisClient.connect();
         console.log('Connected to Redis');
       } catch (err) {
         console.error('Redis connection failed:', err);
       }
     })();
    
     app.get('/views', async (req, res) => {
       try {
         const count = await redis.incr('page:views');
         res.json({ count });
       } catch (err) {
         console.error('Redis error:', err);
         res.status(500).json({ error: 'Failed to connect to Redis' });
       }
     });
    
  4. Ensure that you either export your REDIS_URL or setup a .env file. The best way? Use Parameter Store

    The URL looks something like:

rediss://default:<AUTH_TOKEN>@<ENDPOINT>:6379

🧾 Conclusion

Caching, today, is must for building applications and pages that are scalable and load fast. With AWS ElastiCache, setting up Redis OSS instance is fast and completely free using the AWS Free Tier.

Common real-world use cases include Leaderboard systems, Location-based features like "near me" searches, Persisting user sessions, Page-count and much more!

If you're building something that needs low latency and high throughput, AWS Managed services like ElastiCache, MemoryDB might be the one you need.

Let’s keep building and keep exploring!

Thank you

Aarush Luthra

10
Subscribe to my newsletter

Read articles from Aarush Luthra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aarush Luthra
Aarush Luthra

Driven by curiosity and a passion for innovation, I am a tech enthusiast with a growing expertise in cloud computing, dev-ops and web development. Currently, I’m building projects that range from deploying machine learning models on AWS SageMaker to designing interactive web applications.