Cache là trade-off
Cache tạo một bản sao tạm thời gần consumer. Hãy quyết định cache key, TTL, invalidation, behavior khi miss và quyền truy cập. CloudFront và ElastiCache không cùng vị trí/abstraction.
Bài tập
Thiết kế cache cho catalog, profile và session. Ghi freshness requirement, source of truth, cache miss path, invalidation khi update và hành vi khi cache cluster lỗi. Không dùng cache để che một query/database design tệ mà không đo evidence.
Bài giảng chuyên sâu
Cache là bản sao có hợp đồng freshness
Catalog thường là ứng viên cache vì read-heavy, nhưng giá và quyền truy cập không giống nhau. CloudFront cache HTTP/object ở edge; ElastiCache/MemoryDB là application/data layer. Với cache-aside, app đọc cache, miss thì đọc origin rồi ghi cache; khi update phải quyết định invalidate ngay, TTL ngắn hay chấp nhận stale.
Một cache key sai có thể trả dữ liệu tenant này cho tenant khác; TTL quá dài gây giá cũ; cache stampede làm origin quá tải khi cùng miss. Cache không thay source of truth, backup hay authorization. Cluster lỗi phải có fallback, timeout và metric hit ratio.
Bài tập: thiết kế cache matrix cho catalog, profile và session gồm key, TTL, invalidation, miss behavior, authorization và cleanup. Failure drill: stale value, cache unavailable và thundering herd.