Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 63

AW S A c a d e m yC l o u d D e v e l o p i

ng

Module 9: Caching Information for


Scalability

© 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

At the end of this module, you should be able to do the following:


• Explain when caching is used
• Describe caching with Amazon ElastiCache
• Describe caching with Amazon CloudFront
• Describe and compare Lambda@Edge and CloudFront Functions
• Apply caching strategies

© 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

Section 2: Caching overview

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Challenges of modern application data

Users

• Millions Access methods


• Globally distributed • Mobile
• Devices
• Internet of Things
(IoT)

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

Copy of some or all origin data

© 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

Users Internet Applications Database

Mobile and Web


desktop clients content

• Web content caching • Database query caching


• Web session management • Amazon ElastiCache
• Proxies and resolvers
• Amazon CloudFront
• Amazon ElastiCache
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 10
Workloads that benefit from caching

Read-Heavy Workloads Compute-Intensive Workloads


Benefits Benefits
• Reduce latency • Manipulate large datasets in real time
• Improve input/output operations per
second (IOPS)

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

Section 3: Caching with ElastiCache

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
ElastiCache

Fully managed in-memory data store with sub-


millisecond latency

• Key-value, in-memory store that sits between


Amazon ElastiCache your application and the origin data store
• Decreases access latency, increases throughput,
and eases load of databases and applications
• Scales out, in, up, or down to meet demands
• Fully compatible with open-source Redis and Memcached
engines

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 15
ElastiCache engines

ElastiCache for Memcached ElastiCache for Redis


Features Features
• Multithreading • Support for complex data types
• Low maintenance • Persistence
• Easy horizontal scalability with Auto • High availability (replication)
Discovery • Automatic failover
• Publish/subscribe capabilities

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

Node Node-1 >>> Node-n

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

Replica Replica Replica


node-1 node-1 node-1
P Primary Primary
r node node
i Replica R
m node-n e
a p
r l
y Shard-0001 i
n c
o a
Redis cluster without cluster
d mode Redis cluster with cluster mode configured
n
configured e o
Replica d
node-n e
Redis cluster (or replication group): A logical grouping of one
-
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. or more Redis shards 18
n
Example: ElastiCache in an application
External API requests (for example, third-party logins or data streams)

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

5. Update cache with retrieved value

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 19
Cache miss penalty
and time to live (TTL)

ElastiCache
Request from cache

1 Data not in cache


or TTL expired

Request from database


Database
2
Amazon EC2
application
instances Update cache with new data
3

© 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

1. Preparing the development environment


2. Configuring the subnets for ElastiCache to use
3. Creating the ElastiCache cluster
4. Loading records into the cache from the database
5. Updating data
6. Creating a new record
7. Deleting a record
8. Updating the application container

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 24
Lab: Final product

Queries

Beanstalk deployment Responses


Coffee supplier application

ElastiCache for Aurora Serverless database


Memcached

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 25
~ 90 minutes

Begin Lab 9.1: Caching


Application Data with
ElastiCache

© 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

Section 4: Caching with CloudFront

© 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

Fast content delivery network (CDN) service that


securely delivers data, videos, applications, and
APIs to customers globally with low latency and
high transfer speeds
Amazon CloudFront
• Optimized for performance and scale
• Built-in security features
• Deeply integrated with AWS services
• Highly customizable

© 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

Web content Video on demand Live event


• Used for serving content • Used for video content that • Used to stream live events.
over HTTP or HTTPS, such users can watch at any • Videos must be encoded
as static and dynamic time. and compressed for viewing
content. • Videos must be encoded to devices.
• The origin can be an HTTP support streaming. • The origin can be AWS
server or an S3 bucket. • The origin can be an HTTP Elemental MediaStore or
server or an S3 bucket. AWS Elemental
MediaPackage.

© 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

If cached version = latest origin version


CloudFront edge return 304 Not Modified Origin
location Else S3 bucket
return latest version of img4.jpg

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

Little improvement or possibly slower


performance for this user

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 37
Customizing at the edge with functions

• Use edge functions to customize how your distribution processes requests


and responses.
• Code runs close to viewers to minimize latency.
• Two ways to write and manage edge functions:
• Lambda@Edge
• CloudFront 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

Event sources Viewer request/response and origin Viewer request/response


request/response

Scale Greater duration, memory capacity, Limited to sub-milliseconds of duration, 2


and function size than CloudFront MB memory, and 10 KB in size
Functions

© 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

Section 5: Caching strategies

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Two primary caching strategies

Lazy loading Write-through


• The cache is updated only when • The cache is updated whenever
necessary (cache hit or cache data is written to the database.
miss). • Use when data must be updated in
• Use when data will be read often real time.
but written infrequently. • Examples: Top 100 game
• Example: User profile leaderboard or top 10 most
information popular news stories
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 46
Lazy loading

Application gets a request


for data Advantages
ElastiCache • Less unneeded
Cache hit or miss
data in cache
1 • Non-fatal node
2 failures

If cache miss: Database


• Request data from database
• Update cache
3

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

Application gets a request


for data Advantage
• No stale data in
ElastiCache
the cache
Update database
1

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

• The café website is still running on HTTP and


does not yet support HTTPS.
• The café would like to ensure webpages will
load quickly for users globally.
• Sofía decides it is time deliver the café website
through a content delivery network (CDN).

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 53
Lab: Tasks

1. Preparing the development environment


2. Configuring an Amazon CloudFront distribution for static website content
3. Securing network access to the CloudFront distribution using AWS WAF
4. Securing a REST API endpoint using AWS WAF
5. Configuring a CloudFront Function on the website
6. Adjusting the cache duration

© 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

Begin Lab 9.2: Implementing


CloudFront for Caching and
Application Security

© 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

In summary, in this module, you learned how to do the following:


• Explain when caching is used
• Describe caching with ElastiCache
• Describe caching with CloudFront
• Describe and compare Lambda@Edge and CloudFront Functions
• Apply caching strategies

© 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 ?

A. You need data replication.


B. You need the simplest model possible.
C. You have complex data types.
D. You need an in-memory cache.

© 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.

You might also like