Professional Documents
Culture Documents
9 - CT071!3!3-DDAC - Caching Solutions in Cloud
9 - CT071!3!3-DDAC - Caching Solutions in Cloud
ng
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
M o d u l e 9 : C a c h i n gI n f o r m a t i o n for Scala
bility
Section 1: Introduction
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Module objectives
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3
Module overview
Sections Labs
1. Introduction • Caching Application Data with ElastiCache
• Implementing CloudFront for Caching and
2. Caching overview Application Security
3. Caching with ElastiCache
4. Caching with CloudFront
5. Caching strategies
Knowledge check
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 4
Café business requirement
The café employees have gotten occasional customer feedback that the coffee bean inventory information
takes too long to display on the café website. Sofía wants to reduce the latency and also update the site to
support HTTPS traffic.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 5
Caching as part of developing
a cloud application
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 6
M o d u l e 9 : C a c h i n gI n f o r m a t i o n for Scala
bility
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Challenges of modern application data
Users
Requirements
• Millions of requests
per second
• Microsecond Ecommerce Media Social Online Shared economy
streaming media gaming
latency
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 8
What is caching?
Lower latency
response to
requester
Yes Requested No
Request
data in
Response cache?
Reduced load
on origin
Request TTL
Response
Client Origin
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 9
You can cache at all layers
• Single-page web applications • Caching for application performance
• Browser caching (HTTP cache-control headers) • Amazon API Gateway cache
• Amazon ElastiCache
• Amazon CloudFront
Examples Examples
• Social networking • Recommendation engines
• Gaming • High performance computing (HPC)
• Media sharing simulations
• Question and answer (Q&A) portals
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 11
Factors for considering whether to cache
Is a slow, expensive Is the data relatively Can the data afford Will caching be worth
query required to static and frequently to be stale? the added
generate the data? accessed? development
complexity and
potential costs?
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 12
• Modern applications put high
Section 2 key demands on the performance of
takeaways data sources in an application.
• Caching can improve performance
by serving requests from a copy of
data (a cache) rather than making
the request to the origin.
• Caching can occur at all layers of
your application.
13 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
M o d u l e 9 : C a c h i n gI n f o r m a t i o n for Scala
bility
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
ElastiCache
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 15
ElastiCache engines
Suitability Suitability
• Need the simplest model possible • Need the advantage of data persistence
• Need advanced data types
• Need one of the other features that are
listed
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 16
ElastiCache terminology
Node: Fixed-size chunk of secure,
network-attached RAM Memcached cluster with data partitioning
Cluster
Cluster: Logical
grouping of one or
more ElastiCache
nodes
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 17
ElastiCache terminology: Redis
Redis shard (or node group): A grouping of 1–6 related nodes
AWS Cloud
ElastiCache
1. Request data
from cache
2. If available, return cached data; else,
data not in cache or expired
3. If data not in cache,
Clients request data
from database Database
Elastic Load 4. Return current
Amazon EC2 value
Balancing application
instances
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 19
Cache miss penalty
and time to live (TTL)
ElastiCache
Request from cache
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 20
• ElastiCache is a fully managed, in-
Section 3 key memory data store with sub-
takeaways millisecond latency.
• ElastiCache supports two open-
source in-memory caching engines:
Memcached and Redis.
• A node is a fixed-size chunk of
secure, network-attached RAM, and
a cluster is a logical grouping of one
or more ElastiCache nodes.
21 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lab 9.1: Caching
Application Data
with ElastiCache
22 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lab: Scenario
• Frank and Martha are excited that the bean inventory from the coffee
suppliers application is integrated into the café website. However, they are
getting negative feedback that the bean inventory information takes too
long to display on the page.
• One of the café's customers, Olivia, who is an AWS consultant, proposed
adding database caching to improve the speed of the database query.
• Olivia recommended using the ElastiCache managed service. ElastiCache is
similar to Amazon Aurora Serverless in that it simplifies the process to
deploy and maintain a cache cluster.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 23
Lab: Tasks
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 24
Lab: Final product
Queries
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 25
~ 90 minutes
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 26
Lab debrief:
Key takeaways
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 27
M o d u l e 9 : C a c h i n gI n f o r m a t i o n for Scala
bility
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Challenges of serving data globally
Hop
Greater distance
High network latency Router Hop between the origin
Router
between the origin and and user affects
some users Origin Router performance
Hop
Router
Hop Router
Hop
Hop
Hop
Hop Router
More hops between
Hop the origin and user
affects performance
User is
downloading
video
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 29
What is a CDN?
Locally cached
copies of static
resources
CDN
Hop
POP
Lower network
latency Origin Router
Router
CDN
Dynamic content that is POP
relevant to the User is
geographic location downloading
video
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 30
CloudFront
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 31
How CloudFront works
2. DNS routes to the 3. CloudFront checks its cache and
best CloudFront edge returns the requested files or
location requests them from the origin
1. A user
requests files
S3 bucket or
HTTP server
CloudFront edge
location
4. The origin server sends the
requested files back to the
edge location
5. CloudFront forwards the
files to the user and adds
the files to the cache
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 32
CloudFront distribution types
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 33
Understanding the cache key
The default cache key is the CloudFront distribution domain name plus the URL path of the requested object. Other values
from the request are not included in the cache key by default. Examples follow.
Cache key:
• Domain: d111111abcdef8.cloudfront.net
• URL: /content/stories/example-story.html
Request 1: No matching cache key in cache. Cache miss Request 2: Cache key matches request 1. Cache hit
GET /content/stories/example- GET /content/stories/example-
story.html?ref=0123abc&split-pages=false story.html?ref=xyz987&split-pages=true
HTTP/1.1 HTTP/1.1
Host: d111111abcdef8.cloudfront.net Host: d111111abcdef8.cloudfront.net
User-Agent: Mozilla/5.0 Gecko/20100101 User-Agent: Mozilla/5.0 AppleWebKit/537.36
Firefox/68.0 Chrome/83.0.4103.116
Accept: text/html,*/* Accept: text/html,*/*
Accept-Language: en-US,en Accept-Language: en-US,en
Cookie: Cookie: session_id=wxyz9876
session_id=01234abcd Referer:
Referer:
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. https://rss.news.example.ne 34
https://news.example.com/ t/
Controlling cache keys with cache policies
Settings Category Description Available Settings
Policy information Describes the policy. • Name of policy (which is used to
attach the policy to a caching
behavior)
• Comment
Time to live (TTL) Work together with the Cache-Control and • Minimum TTL
settings Expires HTTP headers (if they’re in the origin • Maximum TTL
response). They determine how long objects in the • Default TTL
CloudFront cache remain valid before CloudFront
checks the origin for updates.
Cache key settings Specify the values in viewer requests that CloudFront • URL query strings
includes in the cache key. The values that you include • HTTP headers
in the cache key are automatically included in • Cookies
requests that CloudFront sends to the origin, which • Compression support
are called origin requests.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 35
Managing cached file expiration
If cache duration of img4.jpg has not
passed return img4.jpg from cache
Else
Request img4.jpg request latest from origin
Modify default duration (24 hours) with CloudFront TTL settings or origin response headers
In CloudFront In the response header
• Set Minimum TTL, Maximum TTL, and Default • Include Cache-Control or Expires
TTL in a cache policy headers
• Modifies duration for all files that match a path • Modifies the cache duration of
pattern individual files
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 36
Using CloudFront to accelerate Amazon
S3 file transfer
Likely speed improvement
S3 bucket for this user
with transfer
acceleration
turned on
Optimized
network path
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 37
Customizing at the edge with functions
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 38
Using CloudFront with Lambda@Edge
A configured CloudFront
event occurs, and the
eu-west-3 function is invoked at a
CloudFront CloudFront Regional
Developer associates an
Regional edge edge cache
AWS Lambda function to location
run based on a CloudFront
distribution event
Lambda replicates your
function to CloudFront
Regional edge locations
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 39
Lambda@Edge example
Lambda function code
def lambda_handler(event, context):
request = event['Records'][0]['cf']['request']
viewerCountry = request['headers'].get('cloudfront-viewer-country')
if viewerCountry:
countryCode = viewerCountry[0]['value']
if countryCode == 'GB' or countryCode == 'DE' or countryCode == 'IE':
domainName = 'eu.example.com' request['origin']['custom']
['domainName'] = domainName
request['headers']['host'] = [{'key': 'host', 'value': domainName}]
return request
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 40
Using CloudFront Functions
CloudFront intercepts
requests and responses
at edge locations and
passes them to your
function
CloudFront
The developer publishes a Regional edge
CloudFront function and location
associates it to a
CloudFront distribution
event
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 41
CloudFront function example
CloudFront function code
function handler(event) {
// NOTE: This example function is for CloudFront function redirects to:
a viewer request event type. https://aws.amazon.com/cloudfront/
// Choose viewer request for event
type when you associate this function
with a distribution.
var response = {
statusCode: 302,
statusDescription: 'Found',
headers: {
'cloudfront-functions': {
value: 'generated-by-CloudFront-
Functions' }, Requested URL (the CloudFront distribution):
'location': { value: https://d111111abcdef8.cloudfront.net/
'https://aws.amazon.com/cloudfront/' }
}
};
return response;
}
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 42
Choosing between Lambda@Edge
and CloudFront Functions
Lambda@Edge CloudFront Functions
Use cases Allows complex functions and full Lightweight, short-running functions
application logic
Ability to build and test No Yes
entirely within Extension of Lambda Native feature of CloudFront
CloudFront
Programming languages Node.js or Python runtime JavaScript
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 43
• CloudFront is a fast CDN service that
Section 4 key delivers data globally with low latency
takeaways and high transfer speeds.
• CloudFront distributions can serve
web, video, or live streaming content.
• A cache key is the unique identifier for
each object in the cache.
• You can use CloudFront Functions and
Lambda@Edge to customize how your
distribution processes requests and
responses.
44 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
M o d u l e 9 : C a c h i n gI n f o r m a t i o n for Scala
bility
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Two primary caching strategies
Disadvantages
• Cache miss penalty
• Potential for stale data
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 47
Lazy loading example
# Python
def get_user(user_id):
# Check the cache
record = cache.get(user_id)
if record is None:
# Run a DB query
record = db.query("select * from users where id = ?",
user_id) # Populate the cache
cache.set(user_id,
record) return record
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 48
Write-through
Database
Update cache
2
Disadvantages
• Write penalty
• Missing data on node failure
• Unused data in cache
• Cache churn
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 49
Write-through example
# Python
def save_user(user_id, values):
# Save to DB
record = db.query("update users ... where id = ?",
values)user_id,
# Push into cache
cache.set(user_id, record)
return record
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 50
• Caching has two primary strategies:
Section 5 key lazy loading and write-through.
takeaways • With lazy loading, the cache is updated
only when necessary (cache hit or
miss).
• With write-through, the cache is
updated whenever data is written to
the database.
• Choose a caching strategy that
matches the use case for your data.
51 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lab 9.2:
Implementing
CloudFront for
Caching and
Application
Security
52 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lab: Scenario
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 53
Lab: Tasks
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 54
Lab: Final product
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 55
~ 90 minutes
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 56
Lab debrief:
Key takeaways
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 57
M o d u l e 9 : C a c h i n gI n f o r m a t i o n for Scala
bility
Module wrap-up
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Module summary
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 59
Complete the knowledge check
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 60
Sample exam question
Which requirement is a reason to choose Memcached over Redis for your ElastiCache
configuration ?
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 61
Additional resources
Video:
• Getting Started with Caching: Turbocharging Your Application Workloads
Blog post:
• Introducing CloudFront Functions – Run Your Code at the Edge with Low Latency at
Any Scale
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 62
Thank you
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from
Amazon Web Services, Inc. Commercial copying, lending, or selling is prohibited. Corrections, feedback, or other questions? Contact us at
https://support.aws.amazon.com/#/contacts/aws-training. All trademarks are the property of their owners.