Scaling your application as you grow should not mean slow to load and expensive to run. Learn how you can use different AWS building blocks such as Amazon ElastiCache and Amazon CloudFront to “cache everything possible” and increase the performance of your application by caching your frequently-accessed content. This means caching at different layers of the stack: from HTML pages to long-running database queries and search results, from static media content to application objects. And how can caching more actually cost less? Attend this session to find out!
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
• Start with basic infrastructure. – Single m1.large EC2 instance running Amazon Linux
– Single non-Multi-AZ m1.xlarge RDS MySQL 5.6 instance
– Apache httpd-2.2.25
– PHP 5.3.27
– Drupal Commerce Kickstart 7.2
– EIP on Instance
• Throw a ton of traffic at it. – 8,000,000 queries, 40 per second, from 4 other instances.
• Profile it. – New Relic PHP agent
– webpagetest.org
Experiment Infrastructure
Web/App
Instance
Elastic IP Amazon RDS
DB Instance Clients
Results? (not pretty)
14.7 second responses
Mostly Static Data
Low RPM
Web Cache
Webserver or proxy caches would live between
your CDN/Users and your web tier and can offer
up increased cost performance via reducing
internal application and database load. Can also
offer up increased edge to origin speed for lots of
content.
Web Cache
Popular solutions: – Varnish
– Nginx
– Apache with mod_cache/mod_proxy
– Squid
– Perlbal
– Language/framework caches (i.e., APC, Zend)
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing
Internet
Gateway
Amazon
Route 53
Amazon
CloudFront
Customer
Traffic
Web Cache
• Opt for in-memory caching when possible.
• Pay attention to your cache hit/miss ratios. It could be a sign that you need to re-size the instances or re-size the number of nodes in your cache pool.
• Set smart TTLs so that you don’t affect new deploys or cache content for too long.
• Be smart about what cookies can burst cache and what cookies can’t. Don’t serve up other people’s content or stale dynamic pages.
Web Cache
How do you decide what to cache? – All logged out user pages
– Any completely static pages
– Traffic/log analysis
• Look at your web logs/CDN logs
• Find heavily hit pages
• Figure out how often they actually change
• Apply a TTL to that page to be cached
– Even 60 second TTLs could help drastically!
Browser Cache
Edge Cache
Web Cache
Application Cache
Application Cache
Application level caches for information such
as session data, temporary application data
such as cart information, and live aggregation
of data feeds.
Application Cache
Popular solutions: – Memcached
– Redis
– Cassandra
– Amazon DynamoDB
Application Cache
Availability Zone
VPC Subnet VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing ElastiCache
Cache Node
Availability Zone
VPC Subnet VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing ElastiCache
Cache Node
Internet
Gateway
Application Cache
Availability Zone
VPC Subnet VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing ElastiCache
Cache Node
Availability Zone
VPC Subnet VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing ElastiCache
Cache Node
Internet
Gateway
Amazon DynamoDB Session Handler for PHP
https://aws.amazon.com/sdkforphp/
Application Cache
Internet
Gateway
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing Squid Proxy
Instances Amazon
DynamoDB
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing Squid Proxy
Instances
Application Cache
Internet
Gateway
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing Squid Proxy
Instances Amazon
DynamoDB
Availability Zone
VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing Squid Proxy
Instances
Application Cache
Use Cases: – Session information
– Temporary data
• Cart info, metadata
– Rate limiting
• Fight abuse of APIs, spamming, functionality abuse
– Counters
• Views, Scores, Leader Boards
Application Cache
How do you decide what to cache?
– Ideally, you have to treat data you cache at this tier as
loss tolerant if working with in-memory caches.
– Session information that wouldn’t make sense in
cookies or in a true DB.
– Look at the kind of data your application is generating
and storing in a DB that it might not need to.
Browser Cache
Edge Cache
Web Cache
Application Cache
Database Cache
Database Cache
Reduce workload on database servers by
caching commonly requested information, or
any information that might not change
frequently (i.e., user info, listing info, product
info).
Database Cache
Popular solutions: – In-engine query caches
– Memcached
• On dedicated host
• On DB host (built in w/ MySQL 5.6)
– Redis
• On dedicated host
Database Cache
A word of caution: In-engine DB caches are often not recommended for
many use cases, as they can significantly impact the
performance of many databases. Depending on the
workload and dataset you have, an in-engine query
cache might not be a good idea for you. We recommend
off DB caches where possible.
Database Cache
Availability Zone
VPC Subnet VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing ElastiCache
Cache Node
Availability Zone
VPC Subnet VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing ElastiCache
Cache Node
Internet
Gateway
RDS Instance
Primary (M-AZ)
RDS Instance
Standby (MAZ)
Database Cache
Availability Zone
VPC Subnet VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing ElastiCache
Cache Node
Availability Zone
VPC Subnet VPC Subnet VPC Subnet
Elastic Load
Balancing Cache
Instances
Web/App
Instances
Elastic Load
Balancing ElastiCache
Cache Node
Internet
Gateway
RDS Instance
Primary (M-AZ)
RDS Instance
Standby (MAZ)
Memcached – Code Sample function retrieveValue($query)
Varnish Cache is a web application accelerator also
known as a caching HTTP reverse proxy. You install it
in front of any server that speaks HTTP and configure
it to cache the contents. Varnish Cache is really, really
fast. It typically speeds up delivery with a factor of 300
- 1000x, depending on your architecture.
- varnish-cache.org
Reverse Proxy / Web Accelerator
Nginx (pronounced "engine x") is an open
source reverse proxy server for HTTP,
HTTPS, SMTP, POP3, and IMAP protocols,
as well as a load balancer, HTTP cache, and
a web server (origin server). The nginx
project started with a strong focus on high
concurrency, high performance and low
memory usage.
- Wikipedia
App-level caching Memcached
Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering
App-level caching
Redis
Redis is an open source, BSD licensed, advanced
key-value store. It is often referred to as a data
structure server since keys can contain strings,
hashes, lists, sets and sorted sets. – Redis.io
Edge – The Onion Skin
“A content delivery network is a large distributed
system of servers deployed in multiple data
centers across the Internet. The goal of a CDN is
to serve content to end-users with high availability