简体   繁体   English

php为“高流量”网站

[英]php for “high-traffic” websites

I have read that PHPs "probable" weakness is how it handles "concurrency". 我已经读过PHP的“可能”弱点是它处理“并发”的方式。 With only sessions and cookies to keep track of user state, how can PHP handle the following situations with high accuracy: 只有会话和cookie来跟踪用户状态,PHP如何高精度地处理以下情况:

  1. multiple users check out with one item that has only 1 stock in inventory (sorry for grammar mistakes, but you pretty much get the picture already) 多个用户签出一个只有1个库存库存的项目(抱歉语法错误,但你已经得到了很多照片)

  2. multiple users logging into the same user account using the same login details 多个用户使用相同的登录详细信息登录同一用户帐户

  3. multiple users editing an the same image at the same time (though this rarely happens in real life) 多个用户同时编辑同一图像(虽然这在现实生活中很少发生)

or any other transactions that demands multiple thread handling 或任何其他需要多线程处理的事务

(I apologize if I misused terms here) (如果我在这里误用了条款,我道歉)

These aren't necessarily problems for PHP. 这些不一定是PHP的问题。 These are problems for developers to overcome given any technology of choice. 这些是开发人员在选择任何技术时都要克服的问题。

  1. Users letting their inventory get to 1 isn't PHP's fault. 让他们的库存达到1的用户不是PHP的错。 You could temporary ignore the 1 when somebody has already added it to their cart though, and free it up if they don't purchase it before their session expires. 当有人已经将它添加到购物车时,您可以暂时忽略1,如果他们在会话到期之前没有购买它,请将其释放。
  2. Okay? 好的? You can logout other users, or manage all sessions. 您可以注销其他用户,也可以管理所有会话。
  3. Again, they likely won't do it at the same microtime . 同样,他们可能不会在相同的微缩时间进行 If they did, toss up a nice error, and ask them to try again. 如果他们这样做了,请抛出一个错误的错误,然后让他们再试一次。 As pointed out in the comments, MySQL has sufficient capabilities to handle these types of occurrences (if they ever happen). 正如评论中所指出的,MySQL具有足够的能力来处理这些类型的事件(如果它们发生的话)。

These aren't real concurrency issues. 这些不是真正的并发问题。 While it's true that PHP as an environment lacks in thread capability, any web server utilizing a PHP module will have multiple threads, each with thier own active PHP environment inside it, all utilizing the same resources. 虽然PHP作为一个环境缺乏线程功能,但任何使用PHP模块的Web服务器都将拥有多个线程,每个线程都有自己的活动PHP环境,所有线程都使用相同的资源。 You would run into these problems with Java, .Net, Perl, or any other web application language. 您可能会遇到Java,.Net,Perl或任何其他Web应用程序语言的这些问题。

  1. You need a transaction on your database, probably with a write lock so that other users can't read it and run the checkout process while someone else is checking out. 您需要在数据库上进行事务处理,可能需要写入锁定,以便其他用户无法读取它并在其他人签出时运行签出过程。 This is not a language thread issue, it's a database transactional issue. 这不是语言线程问题,它是数据库事务问题。
  2. This isn't a threading issue either. 这也不是一个线程问题。 Sessions are fairly trivial with all the tools available, and I've never heard of a "one thread per session" style of implementation on any language platform (that would be non-trivial, difficult to implement, and would just add overhead). 使用所有可用的工具,会话相当简单,而且我从来没有听说过任何语言平台上的“每个会话一个线程”的实现方式(这将是非常重要的,难以实现,并且只会增加开销)。 You either allow multiple session tokens to be active for one account (user can log in multiple times on different tabs or web browsers if they want), or you don't (all session tokens are cleared each time a login procedure occurs so that only one token is active). 您可以允许多个会话令牌在一个帐户中处于活动状态(用户可以根据需要在不同的选项卡或Web浏览器上多次登录),或者您不允许(每次登录过程发生时都清除所有会话令牌,以便仅一个令牌处于活动状态)。
  3. An odd one, but I'm not sure how threading fits here either. 一个奇怪的,但我不确定线程​​如何适合这里。 Image editing would have to be done client-side in the browser. 图像编辑必须在浏览器中的客户端完成。 You can't keep "threads" open to a user's browser in any language... HTTP doesn't work like that. 你无法用任何语言保持用户浏览器的“线程”...... HTTP不能像那样工作。 You'd send them the image and you're done until they hit "save" and send it back. 你给他们发送了图像,你就完成了,直到他们点击“保存”并将其发回。 If you're worried about users overwriting each other's changes, again, you'd just have to put a transactional lock on it. 如果您担心用户会覆盖彼此的更改,那么您只需要对其进行事务锁定即可。 I'd probably just "version" each image, and if an update occurred from one user while another was editing it, you'd inform the other user that they needed to refresh their copy. 我可能只是对每个图像进行“版本化”,如果一个用户发生更新而另一个用户正在编辑它,则会通知其他用户他们需要刷新副本。

As far as I'm aware, no language uses threads to accomplish any of these tasks. 据我所知,没有语言使用线程来完成任何这些任务。 Because of the stateless nature of HTTP communication, cookies are sessions are a mainstay of every web language, so no matter what platform you use, you're going to see very much the same strategy in all of them for handling a given problem. 由于HTTP通信的无状态特性,cookie是会话是每种 Web语言的支柱,因此无论您使用什么平台,您都会在处理给定问题的所有平台中看到非常相同的策略。

  1. 您的数据库应该自动处理事务并删除最后一项,将其从php的责任中删除

Just like all languages you'll need to find some way of locking these files. 就像所有语言一样,您需要找到一些锁定这些文件的方法。 If you're new to concurrency you might start out here and do some research on the different methods available to you. 如果您不熟悉并发性,可以从这里开始,并对可用的不同方法进行一些研究。

But the real question I have is whether this is actually going to be a problem. 但我真正的问题是这实际上是否会成为一个问题。 And if you are going to be in a high concurrency system how high is the damage in the case of a collision. 如果你要进入一个高并发系统,那么碰撞时的损坏程度会有多高。 If the cost of a collision is really high, it might be work contracting out to someone who already has cut their teeth on this and just watch what methods they use. 如果碰撞的成本非常高,那么可能会将工作外包给那些已经开始研究并且只关注他们使用的方法的人。

Have you ever heard of database transactions? 你听说过数据库交易吗? Used properly, these can fix all of your problems (which aren't PHP problems, by the way). 如果使用得当,这些可以解决所有问题(顺便说一下,这不是PHP问题)。

If your question is about transactions, then the answer is yes, but it is not a feature of the language itself. 如果你的问题是关于交易的,那么答案是肯定的,但它不是语言本身的一个特征。 Transaction safety is the task of the database layer (usually a relational database like MySQL). 事务安全是数据库层的任务(通常是像MySQL这样的关系数据库)。

But if I read your question like "Is PHP scalable?", then the answer also is yes. 但是,如果我读到你的问题,如“PHP可扩展吗?”,那么答案也是肯定的。

PHP handles "concurrency" just as perfect as possible, because it hides any concurrency related details completely from the application, which is a good thing for web applications. PHP处理“并发”尽可能完美,因为它完全隐藏了与应用程序相关的任何并发相关细节,这对Web应用程序来说是一件好事。 It makes applications inherently scalable, just as HTTP made the "web" scalable. 它使应用程序具有固有的可扩展性,就像HTTP使“Web”可扩展一样。 HTTP is stateless, so PHP is stateless in a sense. HTTP是无状态的,因此PHP在某种意义上是无状态的。 This allows easily horizontal scalability, eg by adding more hardware without change to the application code (though this still requires some application support beforehand). 这允许容易的水平可伸缩性,例如通过添加更多硬件而无需更改应用程序代码(尽管这仍然需要事先提供一些应用程序支持)。

Check out these great articles for an explanation. 查看这些精彩的 文章以获得解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM