所谓雪崩问题,是在缓存失效的情景下,大并发高访问量同时涌入数据库中查询, 数据库无法同时承受如此大的查询请求, 往前影响到网站整体响应缓慢。
Created
January 16, 2012 04:00
-
-
Save JacksonTian/1618990 to your computer and use it in GitHub Desktop.
避免重复调用,从容应对雪崩问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var emitter = new event.Emitter(); | |
var status = "ready"; | |
var select = function (callback) { | |
emitter.once("selected", function (results) { | |
callback(results); | |
}); | |
if (status === "ready") { | |
status = "pending"; | |
db.select("SQL", function (results) { | |
emitter.emit("selected", results); | |
status = "ready"; | |
}); | |
} | |
}; | |
select(function (results) { | |
// TODO | |
}); | |
select(function (results) { | |
// TODO | |
}); | |
select(function (results) { | |
// TODO | |
}); |
emitter对象上的方法都在prototype上的,即使new出来,也不怎么占用空间。
并且这里不是每次都要new的。这个是针对这个接口一直存在的emitter对象。只有select方法被外部多次调用。emitter一直贮存。
你把异步变成串行了,我觉得至少得用连接池或者计数器措施吧。
没有变串行啊,就是让多个请求等一个结果而已。
请使用setTimeout来模拟,让代码能跑起来,更有说服力。
雪崩问题常规解决思路是允许一定数量的请求访问数据库;或者让一个请求入数据库回写缓存让其它请求等待。刚才我看错了,这个代码没问题。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
每次都new 一个 emitter对象,性能如何呢?