Redis Clusters
Overview
Redis cluster allows you to deploy fully managed Redis database cluster. You don't have to worry about capacity scaling, hardware & VM provisioning, cluster setup, patching or backups.
Redis is a fast, NoSQL in-memory data store that can provide sub-millisecond latency.
Clusters are easy to set up, built on open-source Redis and compatible with the Redis APIs - ElastiCache for Redis works with your Redis clients and uses the open Redis data format to store your data.
It is often used as a cache to offload main database systems.
Under the hood
Redis clusters are fully managed (powered by Amazon ElastiCache for Redis).
Multiple topologies are possible for the deployment: from a one node database, to a multi-node sharded cluster. See section Cluster topology for more info.
When to use
Redis is used in cases when the performance is important. Example use-cases include caching, chat apps, messaging, queues, real-time analytics, etc.
Advantages
- Performance - Redis is an in-memory data store and can deliver sub-millisecond response times. In comparison to disk-based databases (where most operations need a roundtrip to a disk), in-memory data stores can be an order of magnitude faster.
- Fully managed - You don't have to worry about capacity scaling, hardware & VM provisioning, cluster setup, sharding process, patching, backups, etc.
- High availability - Redis supports both clustered and non-clustered modes and provides high availability via support for automatic failovers with minimal impact.
Disadvantages
- Data persistence - Redis offers 2 options for persistency: regular snap-shotting and append-only files. None of them is as secure as a real transactional server providing undo/redo logging, point-in-time recovery, etc.
- Hard to store large amounts of data - All your data must fit in memory.
- Requires topology-aware clients - Clients connecting to the Redis cluster should be aware of the cluster topology. This requires more configuration on the client.
Basic usage
- Following example shows basic usage of
redis-cluster
resource. Only required properties are instanceSize and defaultUserPassword. - The example shows a single node
redis-cluster
.
Copy
resources:myRedisCluster:type: redis-clusterproperties:instanceSize: cache.t3.microdefaultUserPassword: $Secret('redis.password')myFunction:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: path/to/my/lambda.tsenvironment:- name: REDIS_HOSTvalue: $ResourceParam('redisCluster', 'host')- name: REDIS_PORTvalue: $ResourceParam('redisCluster', 'port')- name: REDIS_PASSWORDvalue: $This().resources.myRedisCluster.properties.defaultUserPassword
Lambda function connected to a single-node redis cluster
Copy
import { Cluster } from 'ioredis';const redisClient = new Redis({host: process.env.REDIS_HOST,port: Number(process.env.REDIS_PORT),password: process.env.REDIS_PASSWORD});const handler = async (event, context) => {await redisClient.set('currentTime', `${Date.now()}`);const value = await redisClient.get('currentTime');return { result: value };};export default handler;
Lambda function storing and reading data from a single-node redis cluster
Node instance size
- Different instance sizes have different memory and network capabilities.
- Instance size can be changed without interrupting the cluster or losing the data.
- To learn more, and see a detailed list of available instances, refer to AWS pricing page
Copy
resources:myRedisCluster:type: redis-clusterproperties:instanceSize: cache.t3.microdefaultUserPassword: $Secret('redis.password')
Default user password
- Redis clusters are password protected. To communicate with the cluster, you must configure a password for the default user.
- Communication with the cluster is encrypted in-transit.
- You should not input the password directly. The recommended way is using secrets.
- Password constraints:
- Must be only printable ASCII characters.
- Must be at least 16 characters and no more than 128 characters in length.
- Cannot contain any of the following characters: '/', '"', or '@'.
Copy
resources:myRedisCluster:type: redis-clusterproperties:instanceSize: cache.t3.microdefaultUserPassword: $Secret('redis.password')
Cluster topology
- Cluster topology is based on the amount of replica nodes and the amount of shards.
- Performance and high-availability of the cluster are dependent on the topology.
Single node (non-sharded) cluster
- Single node, non-sharded cluster has only one node. It's not highly available.
Copy
resources:myRedisCluster:type: redis-clusterproperties:instanceSize: cache.t3.microdefaultUserPassword: $Secret('redis.password')
Multi node (non-sharded) cluster
- Adding replica nodes (read replicas) to a cluster has 2 benefits:
- increases the read throughput
- increases availability. Replica node can become the primary node in case of primary node failure.
- Load balancing between replicas is automatically handled by AWS.
- When there are multiple shards, this number specifies the number of replicas for each shard cluster.
Changing numReplicaNodes
when sharding is enabled is not possible and will result in error.
Copy
resources:myRedisCluster:type: redis-clusterproperties:instanceSize: cache.t3.microdefaultUserPassword: $Secret('redis.password')numReplicaNodes: 2
Logging
- When logging is enabled, slow-log will be sent to a pre-created Cloudwatch log group.
Copy
resources:myRedisCluster:type: redis-clusterproperties:instanceSize: cache.t3.microdefaultUserPassword: $Secret('redis.password')logging:format: json
Forwarding logs
It is possible to forward logs to the third party services/databases. See page Forwarding logs for more information and examples.
Accessibility
- You can configure which resources and hosts can access your cluster.
- Redis clusters do not support public IP addresses. Therefore, only 2 access modes are supported.
VPC mode
- The cluster can be accessed only from resources within the default VPC.
- Any function (provided it has
joinDefaultVpc
set to true), batch job or container workload or service within your stack can access the cluster.
Copy
resources:myRedisCluster:type: redis-clusterproperties:instanceSize: cache.t3.microdefaultUserPassword: $Secret('redis.password')accessibility:accessibilityMode: vpc
Scoping workloads in VPC mode
- Similar to vpc mode, but even more restrictive. In addition to resource being in the VPC, any host or resource
trying to access your cluster must explicitly include the cluster in its
connectTo
list.
Copy
resources:myRedisCluster:type: redis-clusterproperties:instanceSize: cache.t3.microdefaultUserPassword: $Secret('redis.password')accessibility:accessibilityMode: scoping-workloads-in-vpc
Referenceable parameters
The following parameters can be easily referenced using $ResourceParam directive directive.
To learn more about referencing parameters, refer to referencing parameters.
In case of NON-sharded cluster(default), this is a hostname of the primary instance that can be used for both reads and writes. In case of sharded cluster, this is cluster's configuration endpoint that can be used for all operations.
- Usage:
$ResourceParam('<<resource-name>>', 'host')
Hostname (address) that can be used for reads only. (only available for NON-sharded clusters). If you use multiple replicas, it is advised to use readerHost for read operations to offload the primary host. ReaderHost automatically balances requests between available read replicas.
- Usage:
$ResourceParam('<<resource-name>>', 'readerHost')
Port of the cluster.
- Usage:
$ResourceParam('<<resource-name>>', 'port')
Indicates whether cluster is sharded. Available values:
enabled
ordisabled
.- Usage:
$ResourceParam('<<resource-name>>', 'sharding')
Pricing
You are charged for:
- Computing nodes, shards and replica nodes:
- Price depends on the instance size, number of shards and number of replica nodes.
- Can be calculated as number of shards * (number of replica nodes + 1 (there is always primary)) * price for node instance
- EXAMPLE 1: Cheapest non-sharded cluster using
cache.t3.micro
instance with 0 replicaNodes: 1 * (0+1) * $0.017 = $0.017/hour (~$12.50/month) - EXAMPLE 2: Sharded cluster with 2 shards using
cache.t3.micro
and 1 replicaNode: 2 * (1+1) * $0.017 = $0.068 per hour (~$49/month)
- Data transfer:
- In most cases there should be no additional cost.
- Backups:
- If retained for at most 1 day: free
- $0.085/GB/month after that
Free Tier (eligible for first 12 months)
- Includes 750 hours of
t2.micro
instances (t3.micro
for the regions in whicht2.micro
is not available) each month.
To learn more about pricing, refer to AWS pricing page