Explore – AWS Elastic Cache, Redis and Memcache

Definition of Redis, Memcache and Amazon ElasticCache

 

 

Redis  is  not an AWS-specific term; it is a general term that refers to an open-source, in-memory data store and caching system. Redis is a standalone technology that can be used on various platforms and cloud providers, as well as on-premises infrastructure.  AWS provides a managed caching service called Amazon ElastiCache that supports Redis as one of its caching engines, It is a well-established and widely recognized in-memory data store and caching system used across the IT industry.

It is a versatile choice for applications with advanced data storage and caching needs.

 

 

Memcached  or “Memcache” is not an AWS-specific term; it is a general term that refers to an open-source, high-performance, in-memory caching system. It is suitable for simpler, lightweight caching requirements.

Memcached is a standalone technology that can be used on various platforms and cloud providers, as well as on-premises infrastructure.  AWS provides a managed caching service called Amazon ElastiCache that supports Memcached as one of its caching engines.

 

 

Amazon Elastic Cache is a managed caching service of AWS, that supports both Memcached and Redis.

“ElastiCache” is a term that is commonly used in the context of AWS and its services, it is not a general industry term. It is specific to AWS’s offerings for caching solutions. Other cloud providers may have similar services, but they might use different names for their managed caching services.

Benefits of Amazon Elastic: A white background with black text Description automatically generated

A screen shot of a computer Description automatically generated

 

 

 

Connect redis cluster with an ec2 instance.

 

Create aws redis cluster and then an ec2 instance and connect redis cluster with an ec2 instance. 

Create a Redis Cluster in AWS and connect to it using EC2 instance

 

Redis – usage in 3 tier web application. 

To use AWS ElastiCache (Redis) in a 3-tier web application built using Node.js, Angular, and AWS RDS MySQL, follow these steps:

  1. Set Up AWS Resources:
    • Ensure you have the necessary AWS resources in place, including an RDS MySQL instance, an ElastiCache Redis cluster, and EC2 instances for your Node.js backend and Angular frontend. You’ll also need appropriate security groups and IAM roles.
  2. Configure AWS SDK for Node.js:
    • In your Node.js backend, use the AWS SDK to interact with AWS services. Make sure you have the AWS SDK for Node.js installed and configured with appropriate credentials. You can set up AWS access and secret keys or use IAM roles for EC2 instances.
  3. Connect to AWS RDS MySQL:
    • Use the AWS RDS MySQL connection credentials and the MySQL library (such as mysql2) to establish a connection to your RDS instance from your Node.js application. You can configure a connection pool for better performance.

const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'your-rds-endpoint',
user: 'your-username',
password: 'your-password',
database: 'your-database',
});

 

     4. Connect to AWS ElastiCache (Redis):

    • Use the AWS SDK to connect to your ElastiCache Redis cluster in your Node.js application. You can use the ioredis library or the built-in redis library that comes with AWS SDK for Node.js.

const Redis = require('ioredis');
const redis = new Redis({
host: 'your-elasticache-endpoint',
port: 6379, // Default Redis port
});

 

5. Implement Caching:

    • In your Node.js backend, use Redis for caching frequently accessed data. Whenever a request is made to fetch data, first check if the data exists in the Redis cache. If found, return it; otherwise, fetch it from the RDS MySQL database, cache it in Redis, and then return it to the client.

Here’s a simplified example using ioredis:

const cachedData = await redis.get(‘cacheKey’);
if (cachedData) {

// Data exists in Redis, return it

res.json(JSON.parse(cachedData));
} else {

// Data not in Redis, fetch from MySQL

const data = await fetchDataFromMySQL();

// Cache the data in Redis with an expiration time

await redis.set(‘cacheKey’, JSON.stringify(data), ‘EX’, 3600);
res.json(data);
}

 

6. Invalidation and Updates:

When data in your MySQL database changes, make sure to invalidate or update the corresponding Redis cache to reflect the changes. This may involve deleting the cache for a specific key or updating it with new data.

 

7. Build Your Angular Frontend:

In your Angular frontend, consume data from your Node.js backend as usual. The frontend doesn’t need to be aware of the Redis caching; it’s a backend implementation detail.

 

By following these steps, you can effectively use AWS ElastiCache (Redis) as a cache in your 3-tier web application to enhance performance and reduce the load on your AWS RDS MySQL database. Caching helps serve frequently requested data more efficiently, improving the user experience.

 

 

Data caching And data storage in Redis 

In a typical 3-tier web application with Redis as a caching layer, data caching and data storage in Redis do not happen automatically. These processes need to be implemented in application code. Redis is a tool that provides fast in-memory caching and data storage, but you must program your application to interact with Redis to use it effectively. Here’s how it works:

  1. Data Caching in Redis:
    • When you want to cache data in Redis, you need to write code in your Node.js backend to check whether the data is already in the Redis cache before querying your AWS RDS MySQL database. If the data is found in Redis, you return it to the client directly from the cache. If it’s not in Redis, you fetch the data from MySQL, cache it in Redis, and then return it to the client.

 

2. Data Storage in Redis:

    • Storing data in Redis, for caching or other purposes, requires explicit code to set, update, or delete data in Redis. For example, when you have new data to store or existing data to update, you need to write code to put that data into Redis using the appropriate keys. Similarly, if you want to remove data from Redis, you need to write code to delete the data using the appropriate keys.

 

 

Storing data in Redis from 3-tier web application

Storing data in AWS ElastiCache (Redis) for use in your 3-tier web application involves a few steps:

  1. Set Up AWS ElastiCache:
    • Ensure you have an AWS ElastiCache Redis cluster created and running in your AWS environment. You can configure the cluster to be publicly accessible or within a VPC, depending on your architecture and security requirements.
  2. Connect to ElastiCache from Node.js:
    • In your Node.js backend, use a Redis client library (such as ioredis or redis) to connect to your ElastiCache cluster. You will need the endpoint and port information for your Redis cluster.

const Redis = require('ioredis');
const redis = new Redis({
host: 'your-elasticache-endpoint',
port: 6379, // Default Redis port
});

3. Store Data in Redis:

    • You can store data in Redis using various data structures like strings, lists, sets, hashes, and more. The choice of data structure depends on your specific use case.

Example of storing a simple key-value pair in Redis:

const cacheKey = 'your-key';
const cacheValue = 'your-value';
redis.set(cacheKey, cacheValue);

 

4. Retrieve Data from Redis:

To retrieve data from Redis, use the key to access the stored data. Example of retrieving data:

const cacheKey = 'your-key';
redis.get(cacheKey).then((value) => {
if (value) {
console.log('Data from Redis:', value);
} else {
console.log('Data not found in Redis; fetch from another source.');
}
});

 

5. Set Expiration for Cached Data (Optional):

You can set expiration times for cached data to ensure that it doesn’t stay in Redis indefinitely.

This is useful for implementing cache refresh mechanisms.

Example of setting a cache expiration time:

const cacheKey = 'your-key';
const cacheValue = 'your-value';
// Set a cache expiration time (e.g., 1 hour)
redis.set(cacheKey, cacheValue, 'EX', 3600);

 

6. Manage Data Consistency:

Ensure that your application code handles data consistency between your RDS MySQL database and Redis. When you update data in your MySQL database, consider updating or invalidating the corresponding data in Redis to reflect those changes. This keeps the cache up-to-date with the latest information.

  1. Use the Cached Data in Your Application:
    • In your Node.js backend, you can check Redis for cached data before making requests to the RDS MySQL database. If the data is found in Redis, you can return it from there, thus reducing the load on your RDS instance and improving response times.
  2. Your Angular frontend can access the cached data in the same way it would for any other data source, as the Redis caching is managed by your Node.js backend.

By following these steps, you can effectively store and retrieve data from AWS ElastiCache (Redis) within your 3-tier web application, enhancing performance and reducing the load on your AWS RDS MySQL database.

 

 

 

 

Difference between cookies and cache

Cookies and cache are both mechanisms used in web development to store and manage data, but they serve different purposes and have distinct characteristics. Here are the key differences between cookies and cache:

  1. Purpose:
    • Cookies: Cookies are primarily used for storing small amounts of data on the client-side, typically to track user information or session state, such as user authentication, user preferences, or shopping cart contents. Cookies are sent with every HTTP request, making them suitable for maintaining user-specific data.
    • Cache: Caching is used to store and retrieve data, often on the server or proxy server, to improve the performance and reduce the load on the server and network. Caching is used to store and serve frequently accessed resources like HTML pages, images, stylesheets, or API responses, reducing the need to fetch the data from the original source repeatedly.

 

2. Data Storage Location:

    • Cookies: Data is stored on the client-side, in the user’s browser. Cookies are typically limited in size, with a maximum storage capacity of a few kilobytes per cookie.
    • Cache: Data can be stored on the server, proxy server, or a content delivery network (CDN). Cached data can be significantly larger than cookies and is stored on the server side.

 

3. Data Persistence:

    • Cookies: Cookies can be either session cookies (deleted when the browser is closed) or persistent cookies (stored on the client’s device until they expire or are manually deleted). They are accessible across sessions but have limited storage capacity.
    • Cache: Cached data is typically stored with a specific expiration time. Cached resources can be purged and refreshed according to cache policies and can persist longer than cookies.

 

4. Accessibility:

    • Cookies: Cookies are accessible and modifiable by JavaScript on the client-side. This allows for dynamic interaction with cookie data.
    • Cache: Caches are typically not directly accessible or modifiable by client-side JavaScript. They are controlled by server-side configuration and policies.

 

5. Scope:

    • Cookies: Cookies are associated with a specific domain or subdomain, and they are typically sent with every HTTP request to that domain or subdomain. They are suitable for maintaining user-specific information.
    • Cache: Caching can be applied at various levels, such as the browser cache, proxy server cache, or CDN cache. Caches can store and serve resources globally to multiple users.

 

6. Use Cases:

Cookies are used for maintaining user sessions, user preferences, tracking user behavior, and storing small amounts of data on the client-side.

    • Caching is used for improving website performance by reducing latency and server load, particularly for static or less frequently changing resources. It is also used for storing API responses and database query results to reduce database load.

In summary, cookies and cache serve different purposes and are used at different levels of the web stack. Cookies are typically used to store small amounts of user-specific data on the client-side, while caching is used to store and serve resources efficiently, often on the server side, to improve the overall performance of web applications.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *