ElastiCache is a web service that makes it easy to deploy, operate, and scale an in-memory key-value store in the cloud. The service improves the performance of web applications by allowing you to retrieve information from fast, managed, in-memory key-value stores, instead of relying entirely on slower disk-based databases. ElastiCache supports two popular open source engines – Memcached and Redis.
In this webinar, we will introduce Amazon ElastiCache, go over the benefits it provides to you, discuss Memcached and Redis engines, demo how the service works, and walk you through how you can plug it into your applications in minutes. We will also cover common design patterns where using ElastiCache can dramatically improve performance of your applications, as well as reduce burden and cost of your disk-based databases, both relational and NoSQL.
Learning Objectives:
• Understand key Amazon ElastiCache features
• Learn how to set up Amazon ElastiCache and how to integrate it into your database stack
• Explore sample use cases, best practices, and tips on using Amazon ElastiCache to speed up your application by reducing database latencies and increasing throughput
2. Learning Objectives
• Understand key Amazon ElastiCache features
• Learn how to set up Amazon ElastiCache and how to integrate it into
your database stack
• Explore sample use cases, best practices, and tips on using Amazon
ElastiCache to speed up your application by reducing database
latencies and increasing throughput
3. Modern Applications Require Real-Time
Performance
We live in a connected world (IoT)
• Phones, Tablets, Cars, Air Conditioners, Toasters
Experiences are increasingly contextual
Apps need to provide real-time responses
Database performance is frequently the bottleneck
Load is spikey and unpredictable
9. Amazon
RDS
Amazon S3
Request Rate
High Low
Latency
Low High
Data Volume
Low High
Amazon
Glacier
Amazon
CloudSearch
Structure
Low
High
Amazon
DynamoDB
Amazon
ElastiCache
HDFS
10. ElastiCache - Customer Value
Extreme
Performance
Sub-millisecond
access latencies
Engineered for
Cloud Scale
Open Source
Compatible
Compatible with
Redis and
Memcached
Existing code will
work when you
update node end
points
Fully Managed
Automates tasks
such as failed node
replacement,
software patching,
upgrades and
backups
CloudWatch
enables you to
monitor cache
performance
metrics
Secure and
Hardened
Supports Amazon
VPC and IAM for
secure and fine
grained access.
Monitors your
nodes and applies
security patches
when necessary
Highly
Available and
Scalable
Multi-AZ with
automatic failover to
a read replica, no
human intervention
required.
Easily scale your
Redis (vertically)
and Memcached
(horizontally)
environments
Cost Effective
Pay as low as 17
cents per hour. Get
started with 750
free hours per
month of a micro
node for a year.
No cross availability
zone data transfer
costs.
11. Memcached
Slab allocator
In-memory key-value datastore
very popular as a caching solution
Supports strings, objects
Multi-threaded
Insanely fast!
Very established
No persistence
Patterns for sharding
12. Redis – the in-memory king
Powerful
~200 commands
In-memory data structure server
Utility data structures
strings, lists, hashes, sets, sorted sets,
bitmaps & HyperLogLogs
Simple
Atomic operations
supports transactions
has ACID properties
Ridiculously fast!
<1ms latency for most commands
Highly Available
Persistent
snapshots or append-only log
Open Source
13. Why Memcached and Redis?
Source: http://db-engines.com/en/ranking/key-value+store
15. ElastiCache with Redis - Development
Region
Availability Zone A Availability Zone B
Auto Scaling group
ElastiCache cluster
Nope
16. Primary
Availability Zone A Availability Zone B
Replica
Replica
writes
Use Primary Endpoint
reads
Use Read Replicas
Auto-Failover
Chooses replica with
lowest replication lag
DNS endpoint is same
ElastiCache for Redis Multi-AZ
ElastiCache
for Redis
ElastiCache
for Redis
ElastiCache
for Redis
Automatic Failover to a read replica in case of
primary node failure
ElastiCache
Automates
snapshots for
persistence
17. ElastiCache with Redis Multi-AZ
Region
Availability Zone A Availability Zone B
Auto Scaling group
ElastiCache cluster
18. ElastiCache with Redis Multi-AZ
Region
Availability Zone A Availability Zone B
Auto Scaling group
ElastiCache cluster
19. ElastiCache with Redis Multi-AZ
Region
Availability Zone A Availability Zone B
Auto Scaling group
ElastiCache cluster
20. ElastiCache with Redis Multi-AZ
Region
Availability Zone A Availability Zone B
Auto Scaling group
ElastiCache cluster
33. Not if I
destroy it
first!
It’s
mine!
Very popular for gaming apps
Need uniqueness + ordering
Easy with Redis Sorted Sets
ZADD "leaderboard" 1201 "Gollum”
ZADD "leaderboard" 963 "Sauron"
ZADD "leaderboard" 1092 "Bilbo"
ZADD "leaderboard" 1383 "Frodo”
ZREVRANGE "leaderboard" 0 -1
1) "Frodo"
2) "Gollum"
3) "Bilbo"
4) "Sauron”
ZREVRANK "leaderboard" "Sauron"
(integer) 3
Use Case 2 - Real-time Leaderboard
34. Use Case 3 - Chat and Messaging
• PUBLISH and SUBSCRIBE Redis commands
• Game or Mobile chat, real-time comment streams
• Server intercommunication
SUBSCRIBE chat_channel:114
PUBLISH chat_channel:114 "Hello all"
>> ["message", "chat_channel:114", "Hello all"]
UNSUBSCRIBE chat_channel:114
35. • Popular for recommendation engines and message board ranking
• Redis counters – increment likes/dislikes
• Redis hashes – list of everyone’s ratings
• Process with algorithm like Slope One or Jaccardian similarity
• Ruby example - https://github.com/davidcelis/recommendable
Use Case 4 – Ratings
INCR item:38927:likes
HSET item:38927:ratings "Susan" 1
INCR item:38927:dislikes
HSET item:38927:ratings "Tommy" -1
36. Ex: Throttling requests to an API
Leverages Redis Counters
ELB
Externally
Facing
API
Reference: http://redis.io/commands/INCR
FUNCTION LIMIT_API_CALL(APIaccesskey)
limit = HGET(APIaccesskey, “limit”)
time = CURRENT_UNIX_TIME()
keyname = APIaccesskey + ":” + time
count = GET(keyname)
IF current != NULL && count > limit THEN
ERROR ”API request limit exceeded"
ELSE
MULTI
INCR(keyname)
EXPIRE(keyname,10)
EXEC
PERFORM_API_CALL()
END
Use Case 5 - Rate Limiting
38. Alarms
Monitoring with CloudWatch
• Easily Accessible via the
ElastiCache console
• Track health of your
nodes
• Set alarms to act when
need to address running
into limitations
39. Key ElastiCache CloudWatch Metrics
• CPUUtilization
• Memcached – up to 90% ok
• Redis – divide by cores (ex: 90% / 4 = 22.5%)
• SwapUsage low
• Evictions low
• CacheMisses / CacheHits Ratio low
• CurrConnections stable
41. Thundering Herd
Causes
• Cold cache – app startup
• Adding / removing nodes
• Cache key expiration (TTL)
• Out of cache memory
Large # of cache misses
Spike in database load
Mitigations
• Script to populate cache
• Gradually scale nodes
• Randomize TTL values
• Monitor cache evictions
42. 1. Forks main Redis process
2. Writes backup to disk from child process
3. Continues to accept traffic on main process
4. Any key update causes a copy-on-write
5. Potentially DOUBLES memory usage by Redis
Swapping During Redis Backup (BGSAVE)
43. Reduce memory allocated to Redis
• Set reserved-memory field in parameter groups
• Decreases node memory available for app
Use larger cache node type
• More expensive
• But no data eviction
Write-heavy apps need extra memory
http://bit.ly/elasticache-bgsave
Swapping During Redis Backup – Mitigations
45. Recap
• In-memory data store can help address the most demanding application
requirements, as well as protect from impact of spikes in app access.
• ElastiCache is a reliable, secure and fully-managed, performance-optimized
in-memory data structure service.
• Use Multi-AZ with automatic failover for production workloads.
• Redis provides powerful capabilities for a variety of in-memory use cases
• For much more detail on setting up and using ElastiCache please see our
whitepaper (google “ElastiCache whitepaper”)
46. Free tier: 750 hours of Micro Cache Node per Month
Pay as little as $0.017 per hour thereafter
for a T2.micro node
You can get started for free