Learn how to make your AWS databases up to 10x faster and up to 90% less expensive with Amazon ElastiCache for Redis. We’ll look at how to determine whether caching will benefit your database environment and show how to easily test and implement a high speed solution.
3. Demanding Real-Time Workloads
Thousands of Customers Implementing Large Scale Caching, Leaderboards,
Session Management, Messaging, Queuing and more
Gaming AdTech Media Mobile Other
4. Modern Applications Require Fast Performance
• We live in a connected world
• Phones, Tablets, Cars, Air Conditioners, Toasters, Drones …
• Experiences are increasingly contextual
• Analytics everywhere, for everything
• Apps need to provide real-time responses
• Database performance is frequently the bottleneck
• Loads are variable and unpredictable
5. Redis – the fast in-memory database
Powerful
~200 commands + Lua scripting
In-memory database
Utility data structures
strings, lists, hashes, sets, sorted sets,
bitmaps & HyperLogLogs
Simple
Atomic operations
supports transactions
has ACID properties
Ridiculously fast!
<500microsecond latency for
most commands
Highly Available
replication
Persistent
snapshots
Open Source
5
Current Version: 3.4.2 (Oct 2016)
6. One Simple Example ….
+
on-demand c3.8xlarge
3,000,000 objects
+
100 bytes each
memtier benchmark
50 read : 50 write
1,228,432 TPS
Source: http://highscalability.com/blog/2014/8/27/the-12m-opssec
-redis-cloud-cluster-single-server-unbenchmar.html
7. * Throughput determined by Requests per Second. Commands: Average% across SET, GET, INCR, LPUSH, RPUSH, LPOP, RPOP, SADD, SPOP, LPUSH, LRANGE_100, LRANGE_300, LRANGE_500, MSET was used.
Load: 1 Million Requests with Pipelined commands, Payload 3 bytes; Redis Instance compared: cache.m4.xlarge, cache.m3.xlarge; Same instance used to issue requests ( c4.xlarge with Redis Client/benchmark tool installed).
7
Passion for delivering constant innovation
ElastiCache for Redis delivers 34% greater throughput across various Redis commands using M4 instances vs. M3 instances.*
Amazon Web Services and Intel
8. 8
Fully managed service = Automated Operations
Database hosted on Amazon EC2 Amazon Managed DBS
9. Amazon
ElastiCache
Redis Multi-AZ
Easy Monitoring via CloudWatch
Fully Managed
Enhanced Redis Engine
Easy to Deploy & Use
ElastiCache vs. Self-Managed Redis
No Cross-AZ Data Transfer Costs
Engineered For Cloud Scale
10. Clusters!
• Enables Horizontal Scaling
• In ElastiCache for Redis 3.2:
• a Cluster is one or more Shards
• a Shard is a primary node plus up to 5 replica nodes
• Up to 15 Shards per Cluster
• 15 r3.8xlarge = 3.5TiB!
• Up to 20 million reads per second
• Up to 4.5 million writes per second
• Cluster-level backup and restore
S5
S1
S2
S4 S3
Client
ElastiCache for Redis Cluster
11. Autosharding • 16384 hash slots per Cluster
• Slot for a key is CRC16 modulo {key}
• Slots are distributed across the Cluster into Shards
• Developers must use a Redis cluster client
• Clients are redirected to the correct shard
• Smart clients store a map
• Any open-source Redis cluster client will work
S5
S1
S2
S4 S3
Client
Shard S1 = slots 0 – 3276
Shard S2 = slots 3277 – 6553
Shard S3 = slots 6554 – 9829
Shard S4 = slots 9830 – 13106
Shard S5 = slots 13107 - 16383
ElastiCache for Redis Cluster
12. Faster Recovery • A Cluster consists of 1 – 15 Shards
• Each Shard has a primary node and up to 5 replica nodes
• In the event of a primary node outage, a replica node in
that Shard will be promoted
Primary
S1a
S1b S1c
S2a
S2b S2c
S3a
S3b S3c
Replica
Shard Shard Shard
X
S2c
S2a
ElastiCache for Redis Cluster
13. Data Types for Rapid Development
Redis Data Type Contains Read/write ability
String
Binary-safe strings (up to 512 MB), Integers or
floating point values, Bitmaps.
Operate on the whole string, parts, increment/decrement
the integers and floats, get/set bits by position.
Hash
Unordered hash table of keys to string
values
Add, fetch, or remove individual ítems by key, fetch the w
hole hash.
List Doubly linked list of strings
Push or pop items from both ends, trim based on offsets,
read individual or multiple items, find or remove items by
value.
Set Unordered collection of unique strings
Add, fetch, or remove individual items, check membership
, intersect, union, difference, fetch random items.
Sorted Set
Ordered mapping of string members to
floating-point scores, ordered by score
Add, fetch, or remove individual items, fetch items based
on score ranges or member value.
Geospatial
index
Sorted set implementation using geospatial
information as the score
Add, fetch or remove individual items, search by coordina
tes and radius, calculate distance.
HyperLogLog
Probabilistic data structure to count unique
things using 12Kb of memory
Add individual or multiple items, get the cardinality.
Value1Key1
Value2Key2
Lon.:
-103.55328
Lat.:
20.63373
Value
10000110
...10
I m a string!
...0000110
ACBD
CBCA
C: 250A: 250D: 0.3B: 0.1
Source:: https://cs.brown.edu/courses/cs227/archives/2011/slides/mar07-redis.pdf
14. Some Data Type Examples
Source: http://www.slideshare.net/FedericoDanielColomb/redis-introduction-54750742
14
18. IGA Works Uses ElastiCache to Power Real-Time App Monetization
By using RDS we saved on operations
cost for our relational databases. Using
ElastiCache we could boost the read
performance of RDS and offload the
massive request rate
Jeongsang Baek
VP Engineering, IGA Works
”
“
• IGA Works is Korea’s leading mobile business platform
• Adpopcorn is an ad monetization platform for mobile applications
that supports 130 million devices
• Storing Ad Inventory in ElastiCache in front of RDS guaranteed real-
time ad bidding and serving, while RDS provides the durable
database layer
19. Expedia’s Real-time Analytics Application Uses Amazon ElastiCache
Expedia is a leader in the $1 trillion travel industry, with an extensive portfolio that
includes some of the world’s most trusted travel brands.
With ElastiCache Redis as caching
layer, the write throughput on
DynamoDB has been set to 3500,
down from 35000, reducing the cost
by 6x.
Kuldeep Chowhan
Engineering Manager, Expedia
”
“ • Expedia’s real-time analytics application
collects data for its “test & learn” experiments
on Expedia sites.
• The analytics application processes ~200
million messages daily.
• Re:invent talk:
https://www.youtube.com/watch?v=ie4dWGT
76LM
21. Query Caching
• For each query, check to see if the query is in the cache
• If so, return the result from the cache
• If not, query the database and cache the result
• Caching isn’t forever
• Use a TTL value to invalidate
• Invalidate-on-load using a trigger (or a Lambda function)
21
22. Example with PHP
if (isset($queryParam['search'])) {
$redis = new Client();
$hash = md5($_SERVER['QUERY_STRING']);
if (!$redis->get($hash . '-results')) {
[[insert database search here, generating '-results' and '-info']]
$redis->set($hash . '-results', serialize($search->call()));
$redis->expire($hash . '-results', 86400);
$redis->set($hash . '-info', serialize($search->call(true)));
$redis->expire($hash . '-info', 86400);
}
$results = unserialize($redis->get($hash . '-results'));
$info = unserialize($redis->get($hash . '-info'));
22
23. Data Caching
• Loading actual database rows (or items) into the cache
• Speedy access for high-load data
• Reduce the load on the database
• For smaller databases, cache the whole database!
23
24. Example with Python
import redis
r = redis.Redis(
host='hostname',
port=port,
password='password')
row = table.get(row_id)
r.set('row:' + row_id, json.dumps(row.to_dict())
24
25. Best Practices for Redis Cluster
• Use latest engine version – 3.2.4
• Use Redis Cluster (even for a single shard) to mitigate node failure impact
• Set reserved-memory to at least 10% of total available memory
• Swap usage should be zero or very low.
• Put read-replicas in a different AZ from the primary
• For important workloads use 2 read replicas per primary
• Write to the primary, read from the read-replicas
• Avoid “KEYS” command and other long running commands