一. 缓存穿透 (请求数据缓存大量不命中)
缓存穿透是指查询一个一定不存在的数据,由于缓存不命中,并且出于容错考虑, 如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义。
例如:下图是一个比较典型的cache-storage架构,cache(例如memcache, redis等等) + storage(例如mysql, hbase等等)架构,查一个压根就不存在的值, 如果不做兼容,永远会查询storage.

1. 缓存空对象
(1). 定义:如上图所示,当第②步MISS后,仍然将空对象保留到Cache中(可能是保留几分钟或者一段时间,具体问题具体分析),下次新的Request(同一个key)将会从Cache中获取到数据,保护了后端的Storage。
public class XXXService { /** * 缓存 */ private Cache cache = new Cache(); /** * 存储 */ private Storage storage = new Storage(); /** * 模拟正常模式 * @param key * @return */ public String getNormal(String key) { // 从缓存中获取数据 String cacheValue = cache.get(key); // 缓存为空 if (StringUtils.isBlank(cacheValue)) { // 从存储中获取 String storageValue = storage.get(key); // 如果存储数据不为空,将存储的值设置到缓存 if (StringUtils.isNotBlank(storageValue)) { cache.set(key, storageValue); } return storageValue; } else { // 缓存非空 return cacheValue; } } /** * 模拟防穿透模式 * @param key * @return */ public String getPassThrough(String key) { // 从缓存中获取数据 String cacheValue = cache.get(key); // 缓存为空 if (StringUtils.isBlank(cacheValue)) { // 从存储中获取 String storageValue = storage.get(key); cache.set(key, storageValue); // 如果存储数据为空,需要设置一个过期时间(300秒) if (StringUtils.isBlank(storageValue)) { cache.expire(key, 60 * 5); } return storageValue; } else { // 缓存非空 return cacheValue; } } }
2. bloomfilter或者压缩filter(bitmap等等)提前拦截
(1). 定义:如上图所示,在访问所有资源(cache, storage)之前,将存在的key用布隆过滤器提前保存起来,做第一层拦截, 例如: 我们的推荐服务有4亿个用户uid, 我们会根据用户的历史行为进行推荐(非实时),所有的用户推荐数据放到hbase中,但是每天有许多新用户来到网站,这些用户在当天的访问就会穿透到hbase。为此我们每天4点对所有uid做一份布隆过滤器。如果布隆过滤器认为uid不存在,那么就不会访问hbase,在一定程度保护了hbase(减少30%左右)。
原文链接请参见:http://carlosfu.iteye.com/blog/2248185