在大流量网站上生成记分牌
这是一个有点奇怪的问题,但我希望有人能指出我正确的方向。基本上我有两种情况,我想知道哪一种最适合我的情况(用户在高流量网站上检查记分牌)。
- 每次用户点击页面时都会重新生成前 10 名 - 服务器负载增加,尤其是在高流量时,用户将尽快看到他/她的正确排名。
- 前 10 名按设定的时间间隔(例如每 10 分钟)重新生成。 - 仅生成一组结果,导致每 10 分钟一次尖峰,而不是每 x 秒一次,如果用户在刷新之间点击,他们将看不到更新的分数。
每种方法都有其优点和缺点,根据您的经验,哪一种最好使用,或者是否有任何神奇的替代方案?
编辑 - 更新,在考虑了每个人所说的内容后,我决定重建应用程序的这一部分。我不是处理单独的分数,而是处理总计,然后将其保存到一个单独的表中,该表的作用类似于缓存的数据源。
感谢大家的宝贵意见。
Bit of an odd question but I'm hoping someone can point me in the right direction. Basically I have two scenarios and I'd like to know which one is the best for my situation (a user checking a scoreboard on a high traffic site).
- Top 10 is regenerated every time a user hits the page - increase in load on the server, especially in high traffic, user will see his/her correct standing asap.
- Top 10 is regenerated at a set interval e.g. every 10 minutes. - only generates one set of results causing one spike every 10 minutes rather than potentially once every x seconds, if a user hits in between the refresh they won't see their updated score.
Each one has it's pros and cons, in your experience which one would be best to use or are there any magical alternatives?
EDIT - An update, after taking on board what everyone has said I've decided to rebuild this part of the application. Rather than dealing with the individual scores I'm dealing with the totals, this is then saved out to a separate table which sort of acts like a cached data source.
Thank you all for the great input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
添加到马塞尔的答案中,我建议仅在写入事件(例如新分数或删除分数)时更新记分板。通过这种方式,您可以为热门查询(例如 Top 10 等)保留静态答案。使用 MemCache 之类的工具来为请求缓存数据,或者如果您没有/无法在服务器上安装 MemCache 之类的工具,则可以序列化常见请求并写入将它们保存为平面文件,然后在写入事件时删除/更新它们。让您的代码首先查找缓存的结果(或文件),然后如果它丢失,则执行查询并创建数据
Adding to Marcel's answer, I would suggest only updating the scoreboards upon write events (like new score or deleted score). This way you can keep static answers for popular queries like Top 10, etc. Use something like MemCache to keep data cached up for requests, or if you don't/can't install something like MemCache on your server serialize common requests and write them to flat files, and then delete/update them upon write events. Have your code look for the cached result (or file) first, and then iff it's missing, do the query and create the data
对于网络来说,没有什么是实时需要的。我会选择选项 2,用户不会注意到分数没有变化。每次缓存清空后,可以使用一些JS来刷新前10名
Nothing is never needed real time when it comes to the web. I would go with option 2 users will not notice that there score is not changing. You can use some JS to refresh the top 10 every time the cache has cleared
要添加 Jordan 的建议:我将记分卡放在生成的单独(HTML 格式)文件中每次当新数据到达时。您可以将此文件包含在包含记分卡的 PHP 页面中,甚至让访问者的浏览器使用 XMLHttpRequests 定期获取它(以节省带宽)。禁用 JavaScript 或使用不支持 XMLHttpRequests(目前很少见,但有可能)的浏览器的用户将只能看到静态页面。
To add to Jordan's suggestion: I'd put the scorecards in a separate (HTML formatted) file, that is produced every time when new data arrives and only then. You can include this file in the PHP page containing the scorecard or even let a visitor's browser fetch it periodically using XMLHttpRequests (to save bandwidth). Users with JavaScript disabled or using a browser that doesn't support XMLHttpRequests (rare these days, but possible) will just see a static page.
Drupal 投票模块将为您处理这个问题,让您可以选择何时重新计算。如果您自己实现它,那么将前 10 个缓存在某个地方是一个好主意 - 您可以定期重新生成它,也可以在某些点使缓存无效。您需要查看人们投票的频率、导致前 10 名页面更改的频率、查看前 10 名页面的频率以及重新生成它所涉及的性能影响。
如果您还没有使用 Drupal/MySQL,那么 CouchDB 在这里会很有用。您可以创建一个计算前 10 个数据的视图,并且它将被缓存,直到发生需要重新计算的情况为止。您还可以放入内联的 http 缓存代理来将结果缓存一定的分钟数。
The Drupal voting module will handle this for you, giving you an option of when to recalculate. If you're implementing it yourself, then caching the top 10 somewhere is a good idea - you can either regenerate it at regular intervals or you can invalidate the cache at certain points. You'd need to look at how often people are voting, how often that will cause the top 10 to change, how often the top 10 page is being viewed and the performance hit that regenerating it involves.
If you're not set on Drupal/MySQL then CouchDB would be useful here. You can create a view which calculates the top 10 data and it'll be cached until something happens which causes a recalculation to be necessary. You can also put in an http caching proxy inline to cache results for a set number of minutes.