Professional Documents
Culture Documents
DAT341 - Working With Amazon ElastiCache For Redis
DAT341 - Working With Amazon ElastiCache For Redis
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What to expect from this session
1) Amazon ElastiCache overview
2) Amazon ElastiCache Usage Patterns
3) Best practices
4) Caching strategies
5) Hands-on workshop Prerequisites
• Lab 1: Performance testing • Have your own laptop
• Lab 2: Working with Amazon ElastiCache for Redis • Have an AWS account set up
• Have installed the latest AWS Command Line
Interface (AWS CLI) tool
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon ElastiCache overview
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
In-memory key-value store supporting
• Redis 3.2.10
• Memcached 1.4.34
High-performance
Fully managed; zero admin
Amazon Highly available and reliable
ElastiCache Hardened by Amazon
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Low HDFS
Amazon
Amazon S3 Glacier
Structure
Amazon
ElastiCache
and Amazon Amazon
DynamoDB Amazon
DynamoDB CloudSearch and
Accelerator
Amazon Elasticsearch
(DAX) Service
High Amazon
RDS
High Low
Request rate
Low High
Latency
Low High
Data volume
Redis overview
Powerful
Open source ~200 commands + Lua scripting
Atomic operations
supports transactions
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
But wait, there’s more!
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis topologies
Vertically Scaled
Cluster Mode Disabled
Slot 0
Keyspace
Max Storage 407 GiB
Slot 16383
I Primary 0–5 Replicas
Horizontally Scaled
Cluster Mode Enabled
Max Storage 6+ TiB Slot 0-5461
Keyspace
Configuration Endpoint Slot 5462-10922
Slot 10923-16383
0–5 Replicas
1–15 Primaries/Shards
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis cluster-mode enabled vs. disabled
Feature Enabled Disabled
Example: Assume
workload needs 175 GB 9 x cache.r3.xlarge ($0.455hr) = $4.095 hr 255.6 GB 1 X cache.r3.8xlarge = $3.640, 237 GB
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Closer look at cluster-mode enabled
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis cluster: automatic client-side sharding
16384 hash slots per cluster
Slot for a key is CRC16(key) mod 16384
Slots are distributed across the cluster into shards
Developers must use a Redis cluster aware client
Clients are redirected to the correct shard
Smart clients store a map
S4 S3
Client
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis cluster—architecture
Example: 3-shard cluster,
2 read replicas
Redis cluster—multi-AZ
A cluster consists of 1 to 15 shards
Redis Cluster
slots 0–5454 slots 5455–10909 slots 0–5454 slots 0–5454 slots 5455–10909
slots 5455–10909
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis cluster—architecture
Each shard has a primary node
Shard and up to five replica nodes
Redis Cluster
slots 0–5454 slots 5455–10909 slots 0–5454 slots 0–5454 slots 5455–10909
slots 5455–10909
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis cluster—architecture
Each shard has a primary node
Shard and up to five replica nodes
Redis Cluster
slots 0–5454 slots 5455–10909 slots 0–5454 slots 0–5454 slots 5455–10909
slots 5455–10909
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis cluster—architecture
Shard Each shard has a primary node
and up to five replica nodes
Redis Cluster
slots 0–5454 slots 5455–10909 slots 0–5454 slots 0–5454 slots 5455–10909
slots 5455–10909
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Scenario 1: Single primary failure
Redis Cluster
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Scenario 1: Single primary failure
Mitigation:
1. Automatic failure detection and replica promotion (~15–30 s)
2. Repair failed node
Redis Cluster
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Scenario 2: Majority of primaries fail
Redis Cluster
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Scenario 2: Majority of primaries fail
Mitigation: Redis enhancements on ElastiCache
• Automatic failure detection and replica promotion
• Repair failed nodes
Redis Cluster
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Resizing via backup and restore Pro tip: DR strategy—enable
CRR on Amazon S3 bucket
triggering AWS Lambda function
to hydrate destination cluster
Downtime
rdb
New writes
not in
snapshot
3 Shards 5 Shards
Step 4 Once the new cluster is up, update your app with new Amazon ElastiCache endpoint, then terminate old cluster
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Zero-downtime Online Re-sharding
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Online Re-Sharding—zero downtime
Simple API
Scale In || Out
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Online Re-Sharding—zero downtime: Scale Out
2910-5094, 5784-6875,
9831--10922 14200-16383
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Online Re-Sharding—zero downtime: Scale In
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Online Re-Sharding—CW alarm triggered
Amazon
CloudWatch AWS SNS
MEMORY
HIGH!
AWS Lambda
…
var params = {
ApplyImmediately: true,
NodeGroupCount: 5,
ReplicationGroupId: ‘rep-group-id’,
…}
elasticache.modifyReplicationGroupShardConfiguration(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
}); …
Cluster Resized
35 Shards
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Healthy
reads
reads/
writes search
reads
AZ1
AZ2
reads
clients
relational
data
search
cache cluster
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Heavy
pressure reads
reads/
writes search
reads
AZ1
AZ2
reads
clients
relational
data
search
cache cluster
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Healthy—
auto scaled reads
reads/
reads
AZ1
AZ2
reads
clients
relational
data
search
cache cluster
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Common Usage Patterns
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Usage Patterns
Session APIs
Database caching
management (HTTP responses)
Streaming data
IOT analytics Pub/sub
(Filtering/aggregation)
Standalone
Social media database Leaderboards
(Sentiment analysis)
(Metadata store)
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Caching Relational data
reads/
writes
Amazon
Clients RDS
mysql.lambda_async
Object data
reads/writes
write-through Amazon
Elastic Load Amazon Amazon
Balancing EC2 S3
ElastiCache
Redis
reads/
writes
DDB streams
Amazon
DynamoDB
Unstructured data
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Caching NoSQL Smaller NoSQL DB clusters needed = lower costs
Faster data retrieval = better performance
Clients
reads
Elasticsearch
Cluster
Amazon
EC2 reads/
writes
Cassandra
Cluster
MongoDB
Cluster
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Caching NoSQL databases with Amazon ElastiCache
Smaller NoSQL DB clusters needed = lower costs
Faster data retrieval = better performance
reads reads
Amazon
Amazon
ElastiCache
ElastiCache
Redis
Redis
Amazon Amazon
EC2 EC2
reads/ reads/
writes writes
MongoDB Cassandra
Cluster Cluster
raw cleansed
Amazon stream stream
AWS Lambda function 1 Amazon Amazon
Kinesis Kinesis Kinesis
Streams Continual data Streams Analytics
filtering/enrichment
Amazon
ElastiCache
(Redis)
Subscribers
Real-time
pub/sub
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Big data architectures using Redis
Collect Process
Amazon Kinesis
Apache Kafka Store
Data Sources
Spark on
Amazon
Apache Storm Amazon EMR
on EMR ElastiCache
AWS
Amazon
Lambda
EC2
Amazon
AWS Lambda
S3 Custom
app
Amazon
Kinesis app
AWS IoT
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
IoT powered by ElastiCache
Direct integration
S3 DDB Kinesis
Sensor store
AWS
IoT
AWS Amazon
Lambda ElastiCache
Redis
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Mobile apps powered by ElastiCache
Amazon
EC2
GEORADIUS
DDB streams
Amazon
DynamoDB
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://aws.amazon.com/blogs/database/amazon-elasticache-utilizing-redis-geospatial-capabilities/
Ad tech powered by ElastiCache
Clients
Advertisers Ad network
Winners bid
ad displayed <40 ms
Ad network
Bidders respond calls for bids
with bids
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://aws.amazon.com/caching/database-caching/
Chat apps powered by ElastiCache
Elastic
Beanstalk
Clients
PubSub
persistent
Server
WebSockets
Application Load connections
Balancer
Chat apps
Amazon
ElastiCache
Redis
SUBSCRIBE chat_channel:114
PUBLISH chat_channel:114 "Hello all"
>> ["message", "chat_channel:114", "Hello all"]
UNSUBSCRIBE chat_channel:114
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. https://aws.amazon.com/blogs/database/amazon-elasticache-utilizing-redis-geospatial-capabilities/
Gaming—real-time leaderboards
• Very popular for gaming apps that need ZADD "leaderboard" 1201 "Gollum”
uniqueness and ordering ZADD "leaderboard" 963 "Sauron"
• Easy with Redis sorted sets ZADD "leaderboard" 1092 "Bilbo"
ZADD "leaderboard" 1383 "Frodo”
ZREVRANGE "leaderboard" 0 -1
1) "Frodo"
2) "Gollum"
3) "Bilbo"
4) "Sauron”
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Rate limiting
FUNCTION LIMIT_API_CALL(APIaccesskey)
limit = HGET(APIaccesskey, “limit”)
Ex: throttling requests to an API time = CURRENT_UNIX_TIME()
keyname = APIaccesskey + ":” + time
uses Redis counters count = GET(keyname)
IF current != NULL && count > limit THEN
ERROR ”API request limit exceeded"
ELSE
MULTI
INCR(keyname)
EXPIRE(keyname,10)
EXEC
ELB PERFORM_API_CALL()
END
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon ElastiCache: Database caching
strategies using Redis
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What data should I cache?
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What data to cache
• Reference data: product categories (user longer TTLs)
• Reference data: product images (user longer TTLs)
• Reference data: product details (user longer TTLs)
• Dynamic: database result sets: (user shorter TTLs)
• Dynamic: API responses (user shorter TTLs)
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
How do I cache my data?
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cache-aside—lazy loading
3) Update cache
1
Amazon
3 ElastiCache
2) If Cache MISS
2
Amazon
Applications RDS
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cache-aside—write-through
Cache-aside—write-through
2) Update cache
2
Amazon
ElastiCache
1) Update primary DB
1
Amazon
Applications RDS
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cache strategies
DB result set
SELECT * FROM x WHERE y ResultSet object (ROW) Key: query, value: CRS as byte array
Pro
When data retrieval logic is abstracted from the code consuming the ResultSet, caching the ROW can
be extremely effective and can be implemented against any RDBMS
Con
Data retrieval still requires extracting values from the ROW and does not further simplify data access, it
only reduces data-retrieval latency
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cache strategies
JSON
SELECT * FROM x WHERE y String firstName = rs.getString(First_Name) Key: 123, Value: ‘{ “firstname”: “Michael”,
“lastname”: “Labib”,
“city”: “Chicago” } ‘
Pro
Very easy to implement. Cache any desired database fields and values into a Redis string. For example,
store your retrieved data into a JSON object stored in a Redis string.
Con
Cannot get individual JSON properties
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cache strategies
Application objects
Pro
Use application objects in their native structure and data state when serialized
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cache strategies
Using Redis data structures
jedis.hmset(“CUSTOMER_ID:123", rsHash);
Pro
In addition to reducing data retrieval latency, cache data into specific data structure that simplifies the
data access pattern
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Caching tips
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon ElastiCache: Best practices
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cluster sizing best practices
• Storage—clusters should have adequate memory
• Recommended: memory needed + 25% reserved memory (for Redis) + some room for growth
(optional 10%)
• Optimize using eviction policies and TTLs
• Scale up or out before reaching max-memory using CloudWatch alarms
• Use memory optimized nodes for cost effectiveness (R4 support)
• Performance—performance should not be compromised
• Benchmark operations using Redis Benchmark tool
• For more READIOPS—add replicas
• For more WRITEIOPS—add shards (scale out)
• For more network IO—use network optimized instances and scale out
• Use pipelining for bulk reads/writes
• Consider Big(O) time complexity for data structure commands
• Cluster isolation (apps sharing key space)—choose a strategy that works for your workload
• Identify what kind of isolation is needed based on the workload and environment
• Isolation: No Isolation $ | Isolation by Purpose $$ | Full Isolation $$$
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis benchmark tool
Open source utility to benchmark performance
Syntax:
redis-benchmark -h <host> -p <port> -c 50 -n 1000 -d 500 –q
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis max-memory policies
Select a max-memory policy based on your workload needs
• noeviction: return errors when the memory limit has been reached and the client is trying to execute
commands that might result in more memory to be used
• allkeys-lru: evict keys trying to remove the less recently used (LRU) keys first
• volatile-lru: evict keys trying to remove the less recently used (LRU) keys first, but only among keys
that have an expire set
• allkeys-random: evict random keys to make space for the new data added
• volatile-random: evict random keys to make space for the new data added, but only evict keys with an
expire set
• volatile-ttl: evict only keys with an expire set, and try to evict keys with a shorter time to live (TTL) first
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Key ElastiCache CloudWatch metrics
• CPUUtilization
• Memcached—up to 90% ok
• Redis—divide by cores (ex: 90% / 4 = 22.5%)
• SwapUsage low
• CacheMisses/CacheHits Ratio low/stable
• Evictions near zero
• Exception: Russian-doll caching
• CurrConnections stable
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
ElastiCache modifiable parameters
• Maxclients: 65000 (unchangeable)
• Use connection pooling
• timeout—closes a connection after it has been idle for a given interval
• tcp-keepalive—detects dead peers given an interval
• Databases: 16 (default) for non-clustered mode
• Logical partition
• Reserved-memory: 25% (default)
• Recommended
50% of maxmemory to use before 2.8.22
25% after 2.8.22—ElastiCache
• Maxmemory-policy:
• The eviction policy for keys when maximum memory usage is reached
• Possible values: volatile-lru, allkeys-lru, volatile-random, allkeys-random, volatile-ttl,
noeviction
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Hands-on workshop overview
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What we’re building
Performance testing lab—infrastructure VPC: REINVENT 10.0.0.0/16
topology
Test
Instance
SSH,
HTTP
REINVENT-APP-SG
Public1:
10.0.0.0/24
REINVENT-RDS-SG REINVENT-EC-SG
Private1: 10.0.2.0/24
Availability Zone #1
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Workshop: JMeter and JUnit tests overview
RDSLoad:testRDSLoad() (This executes only one time) Data loading
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Workshop: JMeter and JUnit tests overview
Stress testing: 20k requests/second
RDSBenchmark:testRDS()
1. Randomly selects a customer row to retrieve from customer IDs 1–1000
2. Retrieves the SELECT SQL STATEMENT and appends the random customer ID integer referencing SQL stored in
SelectCustomerTbl
3. Executes SQL statement and iterates/displays the fetched row
RedisBenchmark:testRedis()
1. Randomly selects a customer hashmap to retrieve from customer IDs 1–1000
2. Retrieves the SELECT SQL STATEMENT and appends the random customer ID integer referencing SQL stored in
SelectCustomerTbl
3. Executes Redis HASHMAP HGETALL command to retrieve/display the fetched hash
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Workshop: JMeter and JUnit tests overview
//Generating a random num 1-1000 representing customer ids
randomKey = Integer.toString(rand.nextInt(1000));
//Retrieving Customer Select SQL Statement & appending customer id
sql = query + randomKey;
stmt = connection.createStatement();
//executing query
rs = stmt.executeQuery(sql);
//iterating and displaying results
while( rs.next() ) { …
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Hands-on workshop overview
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Thank you!
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.