I just saw some ideas about shopping carts from classmate Wu Lei. I happen to be familiar with this aspect of e-commerce, so I jumped out to show off my shame. I hope it will be of some use to some colleagues. I originally wanted to reply to the following, but it turns out that it takes a lot to write, so I might as well write it here. It will be easier to find it by myself in the future. Haha. Questions: 1. Should the data in the shopping cart be stored in the database?
I especially want to know how real software engineers think about this problem in real projects. After searching on Google, I found an article from a netizen in our garden: The shopping cart should be a module for temporarily storing data, and he stored it in the Session object. What this netizen said makes sense, but I don't like this approach. If everyone stores it in the Session object and thousands of users shop together, the ASP.NET server will definitely bear a huge load. Maybe our domestic websites may be better, but how do we do this for websites like Amazon? The Amazon China website, which is Joyo's website, does not store it in the Session object, because if I do not submit an order for the products I put in the shopping cart this time, these products will still be in the shopping cart after logging in next time. So I thought they might be putting the data from those shopping carts into a database.
Reply: Storing the shopping cart in the Session seems to only exist in course design in universities or in some internship projects that no one cares about. In fact, almost all e-commerce websites store shopping cart data in the database. Here are some explanations and design considerations:
1. Session is not suitable for storing large amounts of data. When there are many users, it will inevitably affect server performance, which should be avoided.
2. There is a problem of accidental loss of Session, or when the user accidentally closes the browser, all items in the shopping cart will be lost, resulting in a very bad user experience.
3. Cookies can solve the Session problem in the above item, but due to the length limit of Cookies, the communication overhead when using Cookies, and security considerations, Cookies are not suitable for shopping carts.
4. A better user experience is that regardless of whether the user is logged in or not, the shopping cart status can be recorded within a certain period of time. This requires that the shopping cart in the database cannot be tied to the user.
5. The products placed in the shopping cart are generally products with purchase intention, but they may not necessarily become actual orders. At this time, retaining this data plays a vital role in data mining and business analysis.
Question: 2. About concurrency?
It turns out that when I was developing my own mock website, I once thought of this question: If a customer puts some books into the shopping cart on the website, should these number of books be subtracted from the inventory? That's what I did. I subtract the quantity of the corresponding books in the shopping cart from the database to prevent other users from seeing the false inventory quantity at this time (if it is not subtracted, then other users can purchase it. For example: the number of books in the inventory is 10 This book, customer A puts 10 copies into his shopping cart, and customer B also puts 10 copies into his shopping cart, then who will buy this book will become a conflict). However, the result of this is that every time a customer updates the shopping cart, there will be a communication with the database, which increases the burden on the data server. Amazon.cn is not doing very well in this regard. A few days ago, I believe you may have encountered the problem that when you purchased the book "In-depth Understanding of Operating Systems", you originally placed an order, but was informed that it was out of stock the next day. matter. This incident has indeed greatly affected the credibility of Amazon.cn. I don’t know if their system has solved this problem now, but the Joyo price of the book "In-depth Understanding of Operating Systems" is not what it used to be. I don’t know how the experts solved this problem. You are welcome to write down your successful experience in the comments.
Reply: First of all, let’s talk about the burden on the database server. Think about how many times the database needs to be accessed every time a page is accessed, and then think about how many times it takes to exchange for one operation of adding a shopping cart (the number of accesses mainly depends on the ease of use of the website). Design, this is another topic), so although modifying the design here can reduce some database pressure, it is not a bottleneck here. Ding Xue thinks there is no need to pay too much attention here.
It is a common practice at present that the goods in the shopping cart will not be immediately deducted from inventory. This is mainly to prevent someone from maliciously occupying the goods through the shopping cart. In addition, a redundancy is generally given, because most of the goods in the shopping cart will not be deducted from stock. Entering the final successful order, you must not let the shopping cart affect sales. This must be done. Inventory is generally deducted when the order is successfully submitted. That is, when the user submits the order, you have another opportunity to remind the user that there is no inventory, so there is no need to deduct the inventory when placing the order in the shopping cart. For successful orders, not all orders submitted by users are considered successful orders. There is an automatic order review process. This program is difficult to write, but it is indeed very important. Based on previous data analysis, user behavior, user reputation, etc. The data comes from the system that automatically completes an review of the order within a few minutes. The intensity of the review is related to the industry. This can eliminate most fake orders, and some of them may have to be transferred to manual review by the automatic review system.
There is a special situation here. For some special products such as concert tickets, there may be online seat selection. In this case, it becomes more useful to reserve a seat after placing the shopping cart. The current practice is generally to reserve a seat immediately after placing the shopping cart. , but it will be automatically released if it does not become a real order within a certain period of time, such as ten minutes. Although it cannot completely eliminate malicious seat occupation, it can solve most problems. Nowadays, successful orders in ticketing are different from most other industries. The criterion for judging successful online seat selection orders in the ticketing industry is whether the payment has been successful, which means that unless you pay, you will only be able to stay for ten minutes.
Question: 3. The relationship between orders and order details and shopping carts
I think this problem may have always been a big problem for this type of website! Two days ago, Teacher Chen from CSTP interviewed me on the phone about this question. I was very nervous at the time and my answer to the question was not very clear. In fact, this problem is not difficult to think about simply: there are two tables, orders and details. Each column in the order table points to the corresponding column in the details table. The foreign key is the order number in the order table.
Reply: This question is relatively simple. One is to put it in the shopping cart and treat it as an order. Mark it with a status. In this state, the order can be modified and the shopping cart is merged into the order system (pay attention to handling user login and non-login) status); the second is to have a separate shopping cart table. When the order is finally submitted, the information in the shopping cart is copied into the order and order details table. The latter one is more commonly used, and the specific choice depends on the industry and product attributes.
Question: 4. How to generate the order number in the detailed list?
This question is inherited from question 3. I still don't know how to solve this problem. I have two solutions, one is using a trigger and the other is programming. The former adds a detail each time the customer puts a product in the shopping cart, generates an order after confirming the purchase, and changes the purchase status in the detail table to trigger the trigger and generate an order number (of course, this order number can be either Programming in the trigger can also be to set a column of the order number in the order table to automatically generate a serial number). The latter will judge the order number and then add 1 to it to generate a new order number. But I always feel that these two solutions are very bad, and I would like to know how order numbers are handled on commercial websites.
Reply: First of all, I personally think that the trigger solution is not advisable. I won’t explain the reasons, otherwise it will be a big mess. There are also two methods here. One is to automatically generate numbers from the order table. When generating an order, first write it into the order table, then retrieve the order number and then update the order details table; the other is to generate the order number according to business rules. When the order number After it is known, you can generate an order record or a detail record first, but you must ensure that the detail record must have an order record in the end, otherwise there will be a lot of weird details. There are two methods for the latter method. One is that the order number is generated by the database, and a temporary table is generally used. The advantage is that the serial number can be used universally across all businesses. The other is that the order number is generated by a program. GUID can be used when generating the program, but A better way is to use the order time plus the identification value. The time part can be based on the order The size of the granularity is determined based on the volume. The identification part is numbered in order. The time granularity should also be considered to prevent others from roughly counting your business volume (sweat~~~ This is also another problem. There are many methods. It depends on the situation. I will write another day when I have time. Let’s write an article about order number generation. I’ll reply so many first, so I probably have enough information...)