zoukankan      html  css  js  c++  java
  • 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目7

    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 }
  • 相关阅读:
    Python中Random随机数返回值方式
    SQL跨库查询
    正则表达式基本语法
    excel VBA使用教程
    使用某些Widows API时,明明包含了该头文件,却报错“error C2065: undeclared identifier”
    电脑开机后数字键盘为关闭状态
    编译Boost 详细步骤 适用 VC6 VS2003 VS2005 VS2008 VS2010
    变量作用域,不能理解,先记下
    解决MySQL 在 Java 检索遇到timestamp空值时报异常的问题
    Annotation
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3687476.html
Copyright © 2011-2022 走看看