2014-04-24 00:38
题目:加入你要设计一个服务,供至多1000个client查询每日股票的最高、最低、开盘、收盘价,请描述你要如何设计这个服务。
解法:可以用SQL数据库组织,也可以用K-V数据库来提供更高性能。如果股票数量相对不大,还可以用XML文件组织数据。在百度里搜“BIDU”,或在Google里搜“GOOG”,就能看见例子了。
代码:
1 // 10.1 Suppose you're to design the backend part of a system, which provides queries of real-time stock prices. How would you do it? 2 // Answer: 3 // Go and search "GOOG" on Google or Baidu, you'll get the real-time stock price of Google. The data transferring is best done with AJAX to the frontend, which can greatly save the amount of data transferred. 4 // As for the backend part, you have to use some techniques to store the data: 5 // 1. SQL database, easy for various kinds of structured queries. You won't expose the direct SQL API to external users because it is not safe. Write a wrapper with the web programming language. 6 // Strength: Ability to provide more information than you expected. 7 // Weakness: Not so agile, if the data structure changes. Altering a SQL table is always expensive. 8 // 2. XML files, a good way to store structured data. Good performance when the data scale is small or medium. 9 // Strength: Agile, easy to modify the schema. Regenerating the XML file is usually easy with script languages such as python. 10 // Weakness: Not suitable for large data scale, as you have to parse everything in the XML only to find an item inside. 11 // 3. No-SQL database 12 // Strength: suitable for large scale, high concurrency, flexible with data schema changes. 13 // Weakness: Don't know yet, maybe more space usage. 14 int main() 15 { 16 return 0; 17 }