Nodejs Request Same Resource under High Concurrency

  node.js, question

Problem description:
For example, for buying tickets, there is only one ticket in the database. Now two people buy this ticket at the same time. How does nodejs handle it? Some people say that nodejs is a single thread and will not cause this problem. What if multi-core and multi-thread are used? Or N servers, M threads requesting this resource at the same time? Thank you!

This problem may occur when buying every ticket, not just the last one. In addition, Node.js is single-threaded processing, so the single-machine service will not have similar problems, even if it is multi-core. However, if n servers are running and using data from the same database, this is a competition for resources among multiple processes.

If you want to solve this kind of problem simply, lock the table. If you want to deal with this kind of problem more efficiently, you need to add some cache, if it is a high concurrency situation, you need to do some preliminary screening in the cache layer.