I read a lot of wonderful articles written in my blog. I can't help but want to write it myself, but the level is indeed limited. I have never known how to write it. It is not easy to write. You are welcome to make a brick.
I have been doing e-commerce for more than three years. Can I write the basic shopping cart of E-commerce. Currently, we use shopping cart storage methods, including Session, Cookie, and database storage. Let's analyze the advantages and disadvantages one by one.
1. Session (Memcached) Method
Advantage: the shopping cart information is saved on the server and can be saved as 1 MB.
Disadvantage: large websites occupy too many server memory resources, resulting in excessive server pressure. Session information will be lost after the user logs out. The next time you log on to the shopping cart, the product information is lost.
2. Cookie Method
Advantage: the shopping cart information is stored on the client, which basically reaches persistent storage without occupying server resources.
Disadvantage: the Cookie size cannot exceed 4 kb and is not safe enough. If it is a personal PC, the Cookie can store the shopping cart information, but if it is a public office environment, the information stored in the Cookie is basically invalid (will be overwritten by other people's shopping cart information ). For a large e-commerce website, we need to analyze the user's purchase behavior and recommend the items that interest the user. If we store the shopping cart information in cookies, therefore, you cannot analyze user Purchase behaviors.
3. database storage
Advantage: Persistent storage, which can analyze users' purchase behaviors.
Disadvantage: the website speed slows down and the cost and maintenance increase.
For a large e-commerce website, we need to know what kind of products users are interested in and recommend similar products to users. Therefore, it is necessary to analyze users' purchase behaviors. Here we will only detail the evolution of database storage. In the beginning, we used cookies to store the information about the shopping cart. However, after a period of time, many customers complained about it. Many items are often added to the shopping cart, and they are not the products the customer chooses. The Data Mining Department also complained that the information about the shopping cart could not be analyzed .... We decided to change the storage mode of the shopping cart. The table we designed at the beginning is as follows:
For login users, we query the shopping cart Information Based on UserId. For non-logged-on users, select the query method based on the browser. For Firefox browser, query by SessionId. for IE or 360 browser, query by IP address. When there are only tens of thousands of orders per day, the system runs very stably. However, when the number of orders per day exceeds 0.1 million, especially during peak hours, the write pressure on the shopping cart database is extremely high. Query times out frequently. We have to clear the data stored for more than 10 days. We create a new Job to delete data that exceeds 10 days every night. However, as the order volume increases. The database write pressure is still high. At this time, we should consider database sharding to solve the database write pressure. We split the database based on the login user and anonymous user. Usage
For login users, we split the UserId into two databases, one being an odd database and the other being an even database. For anonymous users, we split them by browser. Firefox (SessionId query) has one library, IE or 360 browser (query by IP address), and one library.
We have also adjusted the table structure. Use UserId and CreatedDateTicks for the joint primary key, and log on to the user's shopping cart table structure:
Anonymous user (Firefox browser) Storage table structure:
IE or 360 browser storage table structure:
We have also made adjustments to data storage. A large number of database update operations are deleted, because a large number of update operations will cause the lock table. When the shopping cart information changes, Content stores the XML of the user's shopping cart information. Now you only need to perform the insert operation. When querying data, you can return the last data record. The database sharding reduces the pressure on a single database to write data. The Content saves the XML of the shopping cart information to avoid a large number of update operations. Now the system can run smoothly!
Thank you for reading this article. I hope this article will help you!