2014-04-24 22:06

题目:搜索引擎问题,如果有一列100台服务器的集群,用来响应查询请求,你要如何设计query分发和cache策略?

解法:query分发可以用计算数字签名并对机器数取模来确定分发到的目标机器。当然这个和hash一样,会存在冲突和故障的情况,需要额外处理。至于cache策略,基本思想是LRU,对于数据变更,采取另加工作线程来定期更新,遇到特定事件也可以主动更新。cache的作用不只是加速查询,特定情况下也可以容灾,所以得根据情况分层设计。

代码:

 1 // 10.7 You have a search engine, the main backend has a cluster of 100 machines. If the processSearch(string query) method is the main procedure to handle the search. How would you design the caching strategy?
 2 // Answer:
 3 // Load balancing:
 4 //     1. Keep strict watch on CPU, IO and memory usage. If occupation is too heavy, start forwarding the incoming requests to its next neighbors.
 5 //    2. Cache may be multi-level, secondary level cache may be used to enlarge cache results, saving the energy of doing new searches.
 6 //    3. If the hit rate of cache is getting abnormally low, force the cache to clear up. These things do happen when some kind of popular events occur. People will flood in to search about some event, and the old results in cache is no longer useful.
 7 //        For example, if the debris of aircraft MH370 is found, there will be awfully lot of searches about it within one miniute. The cache has to be refreshed.
 8 // Caching:
 9 //    1. Every string is computed as digital sum, which is an unsigned integer.
10 //    2. The integer is modulo by num_of_machine in the cluster to get a remainder, this query is usually processed by machine[remainder].
11 //    3. The cache on machine[remainder] most likely will have ready result for the query.
12 //    4. If the machine is too busy, however, it will forward the request to its next alive neighbor.
13 // After all, none of the strategies come out of nowhere, it must be drawn from observation, and verified with real experiments.
14 int main()
15 {
16     return 0;
17 }

 

 posted on 2014-04-24 22:12  zhuli19901106  阅读(175)  评论(0编辑  收藏  举报