KV Cache的本质是为了 Reduce latency for accessing active data,把常用数据的数据库的O(logn)的读写和复杂的查询变成O(1)的读写,cache的设计有很多pattern,常见的有 read-through/write-through(or write-back) 和 cache aside.
在分布式系统中,这些 pattern 的组合都是 consistency, availability, partition tolerance 之间的 trade-off,要根据你的业务需求具体选择。
## read-through/write-through(or write-back)
- Read-through: clients 和 databases 之间加一层 cache layer,clients 不直接访问数据库,clients 通过 cache 间接访问数据库。读的时候 cache 里面没有东西则从database更新再返回,有则直接返回。
- Write-through: clients 先写数据到 cache,cache 更新 database,只有 database 最终更新了,操作才算完成。
- Write-around: clients 写的时候绕过 cache 直接写数据库。
- Write-back: clients 先写数据到 cache,先返回。回头将 cache 异步更新到 database.
一般来讲 write-back 是最快的
## cache aside pattern
Cache 不支持 Read through / write through 的时候用 Cache aside pattern
https://msdn.microsoft.com/en-us/library/dn589799.aspx