Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS re:INVENT
Cache Me If You Can
M i n i m i z i n g L a t e n c y W h i l e O p t i m i z i n g C o s t
Through Advanced Caching Strategies
M a r k u s O s t e r t a g , V P E n g i n e e r i n g , T e a m I n t e r n e t , @ o s t e r j o u r
C o n s t a n t i n G o n z a l e z , P r i n c i p a l S o l u t i o n s A r c h i t e c t , A W S , @ z a l e z
A T C 3 0 3
N o v e m b e r 2 7 , 2 0 1 7
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What you’ll get out of this session
• Speed!
• Best practices
• Architecture patterns
• Real-world examples from
Team Internet
• Lower cost
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AdTech
• Publishers, advertisers
• Bidding
• Limited time
• Hard cutoff
• Typical: x00 ms
Photo by Antonio Barroro on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
It‘s all about speed!
Photo by Jakub Gorajek on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Three ways to become faster
1.Increase rate
2.Parallelize
3.Do less
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Three ways to become faster
1.Increase rate Complex
2.Parallelize
3.Do less
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Three ways to become faster
1.Increase rate Complex
2.Parallelize Complex
3.Do less
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Three ways to become faster
1.Increase rate Complex
2.Parallelize Complex
3.Do less Easy!
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Three ways to become faster
1.Increase rate
2.Parallelize
3.Do less Caching
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Caching
•Do time-consuming stuff once
•Re-use results many times
Because memory is cheaper and faster
than CPU
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Edge
• Web tier
• App tier
• Database
Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Your app
Database App Web ClientEdge
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Edge caching
Database App Web ClientEdgeEdge
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon CloudFront
• 107 Edge locations
• 55 cities
• 24 countries
• Static and
dynamic content
• Optimized last-mile
connectivity
• Lower cost than
directly from region
• PCI DSS compliant
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Markus Ostertag
V P o f E n g i n e e r i n g , T e a m I n t e r n e t A G
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Who is Team Internet?
• Domain monetization business
• 35 people
• HQ in Munich, Germany
• Tech focused
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
TONIC.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
The challenge
•Need to support clients worldwide
•Need latency of <300 ms
•Need 100% consistency at the DB level
Should we go multi-region?
Or is there an alternative?
?
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Multiple regions
EU
AZAZ
US
AZAZ
AP
AZAZ
Route 53
Some kind of syncing
between the regions
ELB ELB ELB
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
CloudFront dynamic content acceleration
AZAZ
CloudFront
EU APUS
ELB
US
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda@Edge
Available in all
Amazon CloudFront
edge locations
• Low-latency
request/response
customization
• Supports viewer
and origin events
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
CloudFront triggers for
Lambda@Edge functions
Origin
server
End user CloudFront
cache
Viewer response Origin response
Viewer request Origin request
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda@Edge use cases
Content
customization
Visitor
validation
A/B testing
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Edge caching recap
• Use CloudFront for
reducing last-mile
latency
• Almost always a win!
• Can save cost, too
• Consider Lambda@Edge
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Edge
• Web tier
• App tier
• Database
Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
ClientEdge
Web tier caching
Database App Web
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
ClientEdge
Web tier caching
Database App WebWeb Cache
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Popular web cache solutions
• Varnish
• Nginx
• Apache with mod_cache/mod_proxy
• Squid
• Language/framework caches (APC, Zend)
• Cache in-memory
• Choose high RAM instances, like the R4 family
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon API Gateway: Serverless APIs
Internet
Mobile
apps
Websites
Partner
Services
AWS Lambda
functions
API
Gateway
response
cache
Endpoints on
Amazon EC2
Any publicly
accessible
endpoint
Amazon
CloudWatch
Amazon
CloudFront
API
Gateway
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon API Gateway: Serverless APIs
Internet
Mobile
apps
Websites
Partner
Services
AWS Lambda
functions
API
Gateway
response
cache
Endpoints on
Amazon EC2
Any publicly
accessible
endpoint
Amazon
CloudWatch
Amazon
CloudFront
API
Gateway
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Web cache tips
• Cache all static content
• Cache logged out users
• Look for heavy hitters in your logs
• Monitor hit/miss ratios
• Choose TTLs wisely:
• Don‘t affect deployments
• Even small TTLs (for example, 60 seconds) help
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Web caching recap
• Web caching makes sense, even
if you already use CloudFront
• Amazon API Gateway comes
with built-in caching: Easy
• Choose TTLs wisely
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Edge
• Web tier
• App tier
• Database
Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
ClientEdge
App tier caching
Database App Web
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What to cache
• Sessions
• Results
• Aggregations
• Templates
• Environments
• Configurations
In short: Everything!
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Everything counts (in larger amounts)
• X0,000–X00,000 requests
• Per second
• Even microseconds add up!
• Everything worth caching
• Even for very short times
• 1 ms @ 10K rps
=> 10 app seconds per real
seconds saved
=> 7120 Instance h per month
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Monitor everything
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Think 80/20
• Find your heavy hitters
• Likely a very small percentage
• Have them in memory, all the time
• It pays off to do special things for
them
Vilfredo Pareto, 1848–1923
Image: sie-ase.org/
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
RAM
• Every instance type has RAM
• Use it!
• Duplicate data is good
• Pre-load popular data
• Leverage file system cache
• Use your language‘s caching
framework
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
In-memory key-value store
High-performance
Redis and Memcached
Fully managed; zero admin
Highly available and reliable
Hardened by Amazon
Amazon
ElastiCache
Add more RAM with Amazon ElastiCache
Memcached
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Memcached—fast caching
Slab allocator
In-memory key-value datastore
Supports strings, objects
Multi-threaded
Fast!
Very established
No persistence
Open source
Easy to scale
Memcached
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Redis—the in-memory leader
Powerful
~200 commands + Lua scripting
In-memory data structure server
Utility data structures
strings, lists, hashes, sets, sorted
sets, bitmaps, and HyperLogLogs
Simple
Atomic operations
supports transactions
Very fast!
Highly available
replication
Persistence
Open source
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
App caching recap
• Monitor everything!
• Find the 20% of users with 80%
impact
• Cache everything, in RAM!
• Consider using ElastiCache
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Edge
• Web tier
• App tier
• Database
Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
ClientEdge
Database caching
Database App Web
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Caching the traditional way
Amazon
DynamoDB
Amazon
RDS
Amazon
DynamoDB
Amazon
RDS
ElastiCache
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cache invalidation
“There are only two hard things in Computer Science: cache
invalidation and naming things.” –Phil Karlton
Two ways to cache:
• Time to Live (TTL) with invalidation
• Keep the cache in sync all the time
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Synchronous writes
Sync/check in the app with after-write return values
ElastiCache
write/update always
write/update always
DynamoDB
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
After-write return with DynamoDB
var updateDynamo = function(value, key, callback) {
var params = {
Key: {key: {"S": key}},
TableName: "dynamodb_table",
AttributeUpdates: {
Action: "ADD", Value: {N: JSON.stringify(value)}
},
ReturnValues: "UPDATED_NEW"
};
dynamodb.updateItem(params, callback);
};
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Compare after-write values
var calls = [
function(callback) {
updateDynamo(value, key, function(err, result) {
callback(err, result.Attributes["val"].N);
});
},
function(callback) { redis.incrbyfloat(key, value, callback); }
];
async.parallel(calls, function(err, result) {
if(Math.abs(parseFloat(result[0]) - parseFloat(result[1])) > 0)
logger.error("diffs @ the result"); // do something against it!
callback(err);
});
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Uncoupled writes/invalidation
Sync/updates via AWS Lambda (uncoupled)
Downside: Small delay for cache updates
DynamoDB
or Amazon Aurora
ElastiCache
write/update always
no update
AWS Lambda
DDB Stream
or stored
procedure
update
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon DynamoDB Accelerator (DAX)
DynamoDB API compatible:
// If you have this...
var dynamodb = new AWS.DynamoDB();
// change it to
var dynamodb = new AmazonDaxClient({
endpoints: ["your-dax.amazonaws.com"],
region: "us-east-1"
});
Multiple tables with one DAX
cluster possible!
DynamoDB
DAX
DynamoDB
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Average ~400–450 µs
Very consistent performance
Warming phase (average ~4500 µs on cold
keys)
No metrics per request
W i t h D A XW i t h o u t D A X
Performance of DAX
Average ~5000–6000 µs
Consistent performance
No warming phase
Detailed metrics per request
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cache hit ratio 25%–30% Cache hit ratio 89%–95%
Cache everything
With negative cachingWithout negative caching
Hit
Miss
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Database caching recap
• Cache everything, even negative results!
• Consider cache auto-update with Lambda
• Can combine app with database cache
• Use DAX
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Edge
• Web tier
• App tier
• Database
Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Things to do:
• Check out Amazon CloudFront/API Gateway
• Monitor everything
• Understand your top 20% of users
• Consider adding a web cache
• Explore app caches with Amazon ElastiCache
• Use DAX
• Cache everything!
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Photo by Robin Pierre on Unsplash
Speed on!
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Like d this talk? Me e t u s ag ain!
ARC303–Running Lean Architectures:
How to Optimize for Cost Efficiency
Tu e sday, Nove mbe r 29, 2:30 p.m.
V e ne tian, Le ve l 2, V e ne tian E
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Thank you!
m a r k u s @ t e a m i n t e r n e t . c o m
g l e z @ a m a z o n . d e
A T C 3 0 3

More Related Content

ATC303-Cache Me If You Can Minimizing Latency While Optimizing Cost Through Advanced Caching Strategies.pdf

  • 1. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS re:INVENT Cache Me If You Can M i n i m i z i n g L a t e n c y W h i l e O p t i m i z i n g C o s t Through Advanced Caching Strategies M a r k u s O s t e r t a g , V P E n g i n e e r i n g , T e a m I n t e r n e t , @ o s t e r j o u r C o n s t a n t i n G o n z a l e z , P r i n c i p a l S o l u t i o n s A r c h i t e c t , A W S , @ z a l e z A T C 3 0 3 N o v e m b e r 2 7 , 2 0 1 7
  • 2. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. What you’ll get out of this session • Speed! • Best practices • Architecture patterns • Real-world examples from Team Internet • Lower cost
  • 3. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AdTech • Publishers, advertisers • Bidding • Limited time • Hard cutoff • Typical: x00 ms Photo by Antonio Barroro on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 4. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. It‘s all about speed! Photo by Jakub Gorajek on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 5. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Three ways to become faster 1.Increase rate 2.Parallelize 3.Do less
  • 6. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Three ways to become faster 1.Increase rate Complex 2.Parallelize 3.Do less
  • 7. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Three ways to become faster 1.Increase rate Complex 2.Parallelize Complex 3.Do less
  • 8. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Three ways to become faster 1.Increase rate Complex 2.Parallelize Complex 3.Do less Easy!
  • 9. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Three ways to become faster 1.Increase rate 2.Parallelize 3.Do less Caching
  • 10. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Caching •Do time-consuming stuff once •Re-use results many times Because memory is cheaper and faster than CPU
  • 11. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 12. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. • Edge • Web tier • App tier • Database Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 13. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Your app Database App Web ClientEdge
  • 14. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Edge caching Database App Web ClientEdgeEdge
  • 15. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon CloudFront • 107 Edge locations • 55 cities • 24 countries • Static and dynamic content • Optimized last-mile connectivity • Lower cost than directly from region • PCI DSS compliant
  • 16. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Markus Ostertag V P o f E n g i n e e r i n g , T e a m I n t e r n e t A G
  • 17. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Who is Team Internet? • Domain monetization business • 35 people • HQ in Munich, Germany • Tech focused
  • 18. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. TONIC.
  • 19. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. The challenge •Need to support clients worldwide •Need latency of <300 ms •Need 100% consistency at the DB level Should we go multi-region? Or is there an alternative? ?
  • 20. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Multiple regions EU AZAZ US AZAZ AP AZAZ Route 53 Some kind of syncing between the regions ELB ELB ELB
  • 21. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. CloudFront dynamic content acceleration AZAZ CloudFront EU APUS ELB US
  • 22. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda@Edge Available in all Amazon CloudFront edge locations • Low-latency request/response customization • Supports viewer and origin events
  • 23. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. CloudFront triggers for Lambda@Edge functions Origin server End user CloudFront cache Viewer response Origin response Viewer request Origin request
  • 24. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda@Edge use cases Content customization Visitor validation A/B testing
  • 25. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Edge caching recap • Use CloudFront for reducing last-mile latency • Almost always a win! • Can save cost, too • Consider Lambda@Edge
  • 26. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. • Edge • Web tier • App tier • Database Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 27. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. ClientEdge Web tier caching Database App Web
  • 28. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. ClientEdge Web tier caching Database App WebWeb Cache
  • 29. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Popular web cache solutions • Varnish • Nginx • Apache with mod_cache/mod_proxy • Squid • Language/framework caches (APC, Zend) • Cache in-memory • Choose high RAM instances, like the R4 family
  • 30. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon API Gateway: Serverless APIs Internet Mobile apps Websites Partner Services AWS Lambda functions API Gateway response cache Endpoints on Amazon EC2 Any publicly accessible endpoint Amazon CloudWatch Amazon CloudFront API Gateway
  • 31. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon API Gateway: Serverless APIs Internet Mobile apps Websites Partner Services AWS Lambda functions API Gateway response cache Endpoints on Amazon EC2 Any publicly accessible endpoint Amazon CloudWatch Amazon CloudFront API Gateway
  • 32. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Web cache tips • Cache all static content • Cache logged out users • Look for heavy hitters in your logs • Monitor hit/miss ratios • Choose TTLs wisely: • Don‘t affect deployments • Even small TTLs (for example, 60 seconds) help
  • 33. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Web caching recap • Web caching makes sense, even if you already use CloudFront • Amazon API Gateway comes with built-in caching: Easy • Choose TTLs wisely
  • 34. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. • Edge • Web tier • App tier • Database Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 35. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. ClientEdge App tier caching Database App Web
  • 36. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. What to cache • Sessions • Results • Aggregations • Templates • Environments • Configurations In short: Everything!
  • 37. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Everything counts (in larger amounts) • X0,000–X00,000 requests • Per second • Even microseconds add up! • Everything worth caching • Even for very short times • 1 ms @ 10K rps => 10 app seconds per real seconds saved => 7120 Instance h per month
  • 38. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Monitor everything
  • 39. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Think 80/20 • Find your heavy hitters • Likely a very small percentage • Have them in memory, all the time • It pays off to do special things for them Vilfredo Pareto, 1848–1923 Image: sie-ase.org/
  • 40. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. RAM • Every instance type has RAM • Use it! • Duplicate data is good • Pre-load popular data • Leverage file system cache • Use your language‘s caching framework
  • 41. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. In-memory key-value store High-performance Redis and Memcached Fully managed; zero admin Highly available and reliable Hardened by Amazon Amazon ElastiCache Add more RAM with Amazon ElastiCache Memcached
  • 42. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Memcached—fast caching Slab allocator In-memory key-value datastore Supports strings, objects Multi-threaded Fast! Very established No persistence Open source Easy to scale Memcached
  • 43. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Redis—the in-memory leader Powerful ~200 commands + Lua scripting In-memory data structure server Utility data structures strings, lists, hashes, sets, sorted sets, bitmaps, and HyperLogLogs Simple Atomic operations supports transactions Very fast! Highly available replication Persistence Open source
  • 44. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. App caching recap • Monitor everything! • Find the 20% of users with 80% impact • Cache everything, in RAM! • Consider using ElastiCache
  • 45. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. • Edge • Web tier • App tier • Database Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 46. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. ClientEdge Database caching Database App Web
  • 47. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Caching the traditional way Amazon DynamoDB Amazon RDS Amazon DynamoDB Amazon RDS ElastiCache
  • 48. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Cache invalidation “There are only two hard things in Computer Science: cache invalidation and naming things.” –Phil Karlton Two ways to cache: • Time to Live (TTL) with invalidation • Keep the cache in sync all the time
  • 49. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Synchronous writes Sync/check in the app with after-write return values ElastiCache write/update always write/update always DynamoDB
  • 50. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. After-write return with DynamoDB var updateDynamo = function(value, key, callback) { var params = { Key: {key: {"S": key}}, TableName: "dynamodb_table", AttributeUpdates: { Action: "ADD", Value: {N: JSON.stringify(value)} }, ReturnValues: "UPDATED_NEW" }; dynamodb.updateItem(params, callback); };
  • 51. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Compare after-write values var calls = [ function(callback) { updateDynamo(value, key, function(err, result) { callback(err, result.Attributes["val"].N); }); }, function(callback) { redis.incrbyfloat(key, value, callback); } ]; async.parallel(calls, function(err, result) { if(Math.abs(parseFloat(result[0]) - parseFloat(result[1])) > 0) logger.error("diffs @ the result"); // do something against it! callback(err); });
  • 52. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Uncoupled writes/invalidation Sync/updates via AWS Lambda (uncoupled) Downside: Small delay for cache updates DynamoDB or Amazon Aurora ElastiCache write/update always no update AWS Lambda DDB Stream or stored procedure update
  • 53. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon DynamoDB Accelerator (DAX) DynamoDB API compatible: // If you have this... var dynamodb = new AWS.DynamoDB(); // change it to var dynamodb = new AmazonDaxClient({ endpoints: ["your-dax.amazonaws.com"], region: "us-east-1" }); Multiple tables with one DAX cluster possible! DynamoDB DAX DynamoDB
  • 54. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Average ~400–450 µs Very consistent performance Warming phase (average ~4500 µs on cold keys) No metrics per request W i t h D A XW i t h o u t D A X Performance of DAX Average ~5000–6000 µs Consistent performance No warming phase Detailed metrics per request
  • 55. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Cache hit ratio 25%–30% Cache hit ratio 89%–95% Cache everything With negative cachingWithout negative caching Hit Miss
  • 56. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Database caching recap • Cache everything, even negative results! • Consider cache auto-update with Lambda • Can combine app with database cache • Use DAX
  • 57. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. • Edge • Web tier • App tier • Database Photo by Annie Spratt on Unsplash© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 58. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Things to do: • Check out Amazon CloudFront/API Gateway • Monitor everything • Understand your top 20% of users • Consider adding a web cache • Explore app caches with Amazon ElastiCache • Use DAX • Cache everything!
  • 59. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Photo by Robin Pierre on Unsplash Speed on! © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 60. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Like d this talk? Me e t u s ag ain! ARC303–Running Lean Architectures: How to Optimize for Cost Efficiency Tu e sday, Nove mbe r 29, 2:30 p.m. V e ne tian, Le ve l 2, V e ne tian E
  • 61. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Thank you! m a r k u s @ t e a m i n t e r n e t . c o m g l e z @ a m a z o n . d e A T C 3 0 3