ladderlore
请求新指南
  • 输入服务名称,例如 S3 或 IAM。

概念

Caching strategies

Keep a copy of hot data close to the reader: at the edge, in front of the API, in memory next to the database.

本课尚未翻译,暂以英文显示。

A request can be answered at any stop before the database: browser, edge, API, memory.

A request can be answered at any stop before the database: browser, edge, API, memory.

  • Cache filled

    Lazy loading
    On a miss, when read
    Write-through
    On every write
  • Stale data

    Lazy loading
    Possible until the TTL ends
    Write-through
    Rare
  • Wasted memory

    Lazy loading
    None: only read data
    Write-through
    Data nobody reads
  • First read

    Lazy loading
    Slow: miss, then fill
    Write-through
    Fast: already cached
  • Best for

    Lazy loading
    Read-heavy, a little age is fine
    Write-through
    Fresh data after a write
Most designs use lazy loading with a TTL, and write-through for hot items.

1 / 2

  • Cache filled

    Lazy loading
    On a miss, when read
    Write-through
    On every write
  • Stale data

    Lazy loading
    Possible until the TTL ends
    Write-through
    Rare
  • Wasted memory

    Lazy loading
    None: only read data
    Write-through
    Data nobody reads

2 / 2

  • First read

    Lazy loading
    Slow: miss, then fill
    Write-through
    Fast: already cached
  • Best for

    Lazy loading
    Read-heavy, a little age is fine
    Write-through
    Fresh data after a write
Most designs use lazy loading with a TTL, and write-through for hot items.

是什么

A cache stores the result of work so the next request is fast and cheap. It cuts latency, load on the database and cost. You can cache at every layer between user and data: the browser, Amazon CloudFront at the edge, Amazon API Gateway, Amazon ElastiCache in your VPC, DynamoDB Accelerator (DAX) in front of DynamoDB, and the database’s own buffer pool.

是什么

Two ways fill a cache. Lazy loading (cache-aside): the app checks the cache, and on a miss it reads the database and writes the result to the cache. Only requested data is cached, and a cache failure is survivable, but a miss costs three trips and data can be stale. Write-through: every write goes to the cache and the database. The cache is fresh, but it holds data nobody reads, and writes are slower. Many designs combine both with a TTL.

是什么

A time to live (TTL) is how long an item may stay. A short TTL means fresher data and more misses. Cache only what can be a little old. To remove data early, invalidate it: CloudFront and API Gateway both allow this. ElastiCache for Redis supports replication and Multi-AZ failover. Memcached is simpler and has no replication.

要记住

  • Cache where data is read most: edge, API, application, database.
  • Lazy loading fills the cache on a miss. Write-through updates it on every write.
  • TTL limits how stale data can get. Invalidate to remove data early.
  • DAX caches DynamoDB reads in microseconds and is API compatible with DynamoDB.

要记住

  • A cold cache sends all traffic to the database. Warm it, and add jitter to TTLs so items do not expire together.
  • Do not cache personal or fast-changing data.

考试中

  • “Repeated reads of a rarely changing table slow the database” points to ElastiCache in front of it, or read replicas.
  • “DynamoDB reads need microsecond latency with few code changes” points to DAX.
  • “Static content for global users” points to CloudFront.
  • “Cache API responses for five minutes” points to API Gateway caching.