Write-Through缓存策略 ElastiCache

直写

直写策略会在将数据写入数据库时在缓存中添加或更新数据。

直写的优点和缺点

直写的优点如下:

  • 缓存中的数据永不过时。

    由于每次将数据写入数据库时都会更新缓存中的数据,因此缓存中的数据始终保持最新。

  • 写入性能损失与读取性能损失。

    每次写入都涉及两次往返:

    1. 对缓存进行写入

    2. 对数据库进行写入

    这将增加流程的延迟。即便如此,与检索数据时的延迟相比,最终用户通常更能容忍更新数据时的延迟。有一个内在的意义,即更新的工作量更大,因而花费的时间会更长。

直写的缺点如下:

  • 缺失的数据。

    如果您启动新节点,无论是由于节点故障还是横向扩展,都会存在缺失数据。在数据库上添加或更新数据之前,这些数据一直缺失。您可以通过实施采用直写的延迟加载来最大限度地减少此情况。

  • 缓存扰动。

    大多数数据永远不会被读取,这是浪费资源。通过添加生存时间 (TTL) 值,您可以最大限度地减少浪费的空间。

Write-Through

The write-through strategy adds data or updates data in the cache whenever data is written to the database.

Advantages and Disadvantages of Write-Through

The advantages of write-through are as follows:

  • Data in the cache is never stale.

    Because the data in the cache is updated every time it's written to the database, the data in the cache is always current.

  • Write penalty vs. read penalty.

    Every write involves two trips:

    1. A write to the cache

    2. A write to the database

    Which adds latency to the process. That said, end users are generally more tolerant of latency when updating data than when retrieving data. There is an inherent sense that updates are more work and thus take longer.

The disadvantages of write-through are as follows:

  • Missing data.

    If you spin up a new node, whether due to a node failure or scaling out, there is missing data. This data continues to be missing until it's added or updated on the database. You can minimize this by implementing lazy loading with write-through.

  • Cache churn.

    Most data is never read, which is a waste of resources. By adding a time to live (TTL) value, you can minimize wasted space.

猜你喜欢

转载自www.cnblogs.com/cloudrivers/p/11628845.html