Regional edge cache, tải lên qua CloudFront và nội dung động
Không phải nội dung nào cũng được truy cập thường xuyên, và không phải request nào cũng chỉ đọc; CloudFront vẫn giúp ích cho nội dung ít phổ biến, nội dung động và cả việc tải lên.
CloudFront lưu cache ở hai tầng. POP (edge location) phục vụ trực tiếp người dùng và giữ nội dung phổ biến. Regional edge cache nằm giữa các POP và origin, có dung lượng cache lớn hơn một POP, nên các object kém phổ biến dần (ảnh sản phẩm cũ, nội dung do người dùng tải lên, tin tức đột nhiên được quan tâm trở lại) vẫn được giữ gần người dùng sau khi POP đã loại chúng. Khi cả hai tầng đều không có object, phản hồi của origin được lưu tại cả regional edge cache và POP; các POP trong cùng khu vực dùng chung regional edge cache, nhờ đó giảm request lặp lại tới origin. Cả hai tầng đều thuộc CloudFront và chỉ phục vụ HTTP/HTTPS; ứng dụng dùng giao thức UDP sử dụng AWS Global Accelerator (phần 4).
Không phải request nào cũng đi qua regional edge cache: request dùng PUT, POST, PATCH, OPTIONS, DELETE và request động (xác định tại thời điểm request) được POP chuyển thẳng tới origin; khi origin là S3 và regional edge cache tối ưu nằm cùng Region với bucket, POP cũng truy cập bucket trực tiếp. Ngoài ảnh sản phẩm, MFTECH có thêm hai luồng đi qua cùng distribution: khách hàng tải ảnh đánh giá lên bucket S3, và đối tác vận chuyển tra cứu trạng thái đơn hàng qua API. Chọn tình huống và cấu hình để quan sát đường đi của request:
Chọn tình huống và cấu hình, sau đó nhấn Gửi request. Thử lần lượt từng cấu hình để so sánh đường đi và kết quả.
Tải lên qua CloudFront. Khi cache behavior cho phép PUT và POST, dữ liệu đi vào edge location gần người dùng rồi được chuyển tới origin qua các kết nối persistent mà CloudFront duy trì với origin; CloudFront không cache phản hồi của các method này. Với các văn phòng ở xa cần tải lên và tải xuống tệp video dung lượng lớn từ một bucket tập trung, cả CloudFront (origin là REST endpoint của bucket, OAC được cấp s3:PutObject) và S3 Transfer Acceleration đều cải thiện tốc độ truyền mà không phải tách dữ liệu ra nhiều bucket. Origin là S3 website endpoint không dùng được cho luồng này vì website endpoint chỉ nhận GET và HEAD và không hỗ trợ OAC.
Nội dung động. CloudFront tăng tốc cả nội dung cache được và nội dung động, nhưng với phản hồi không cache được thì mọi request đều tới origin. Trong đợt flash sale với lưu lượng dự kiến gấp 10 lần và nội dung thay đổi liên tục, năng lực xử lý phải đến từ Auto Scaling group phía sau ALB; CloudFront không bổ sung năng lực cho origin. Với API phục vụ đối tác toàn cầu, áp managed cache policy dành cho nội dung tĩnh (CachingOptimized) dẫn tới sai dữ liệu như mô phỏng cho thấy; khi cần IP tĩnh, giao thức TCP/UDP hoặc failover nhanh giữa các Region mà không thay đổi ứng dụng, AWS Global Accelerator là phương án phù hợp.
- POP giữ nội dung phổ biến; regional edge cache giữ lâu hơn nội dung kém phổ biến.
- PUT, POST, PATCH, OPTIONS, DELETE và request động đi thẳng từ POP tới origin.
- Tải lên S3 qua CloudFront cần cache behavior cho phép PUT/POST và OAC được cấp
s3:PutObjecttrong bucket policy; origin request policy chỉ quyết định thông tin gửi kèm, không cấp quyền. - API trả dữ liệu theo từng request dùng CachingDisabled (hoặc cache policy có đủ thành phần cache key), và origin vẫn phải tự co giãn bằng Auto Scaling group.
Regional edge caches, uploads through CloudFront and dynamic content
Not all content is popular and not every request only reads; CloudFront still helps with less popular content, dynamic content and even uploads.
CloudFront caches at two tiers. POPs (edge locations) serve users directly and keep popular content. Regional edge caches sit between the POPs and the origin and have a larger cache than an individual POP, so objects that become less popular (older product photos, user-generated content, news that suddenly regains interest) stay close to users after a POP has removed them. When neither tier has the object, the origin response is cached at both the regional edge cache and the POP; the POPs of an area share the regional edge cache, which removes repeated requests to the origin. Both tiers belong to CloudFront and serve HTTP/HTTPS only; an application that uses UDP uses AWS Global Accelerator (part 4).
Not every request goes through a regional edge cache: requests that use PUT, POST, PATCH, OPTIONS or DELETE, and dynamic requests (as determined at request time), go from the POP directly to the origin; when the origin is S3 and the optimal regional edge cache is in the same Region as the bucket, the POP also goes to the bucket directly. Besides product images, two more MFTECH flows use the same distribution: customers upload review photos to an S3 bucket, and shipping partners look up order status through an API. Choose a scenario and a configuration to watch the request path:
Chọn tình huống và cấu hình, sau đó nhấn Gửi request. Thử lần lượt từng cấu hình để so sánh đường đi và kết quả.
Uploads through CloudFront. When the cache behavior allows PUT and POST, data enters the edge location near the user and is forwarded to the origin over the persistent connections that CloudFront keeps with the origin; CloudFront does not cache responses to these methods. For remote offices that upload and download large video files to and from one central bucket, both CloudFront (origin is the bucket’s REST endpoint, OAC granted s3:PutObject) and S3 Transfer Acceleration improve transfer speed without splitting the data across buckets. An S3 website endpoint origin cannot serve this flow, because a website endpoint accepts only GET and HEAD and does not support OAC.
Dynamic content. CloudFront accelerates both cacheable and dynamic content, but when responses cannot be cached, every request reaches the origin. During a flash sale with an expected 10x traffic increase and constantly changing content, capacity must come from an Auto Scaling group behind the ALB; CloudFront adds no capacity to the origin. For an API used by partners worldwide, applying a managed cache policy built for static content (CachingOptimized) returns wrong data, as the simulation shows; when static IPs, TCP/UDP or fast failover between Regions are needed without changing the application, AWS Global Accelerator is the right fit.
- POPs keep popular content; regional edge caches keep less popular content longer.
- PUT, POST, PATCH, OPTIONS, DELETE and dynamic requests go from the POP directly to the origin.
- Uploading to S3 through CloudFront needs a cache behavior that allows PUT/POST and an OAC granted
s3:PutObjectin the bucket policy; an origin request policy only decides what information is forwarded and grants no permission. - An API whose data depends on each request uses CachingDisabled (or a cache policy with a complete cache key), and the origin still scales on its own with an Auto Scaling group.