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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)