Far above the Yellow River, between white clouds, there is a lonely town with mountains. Qiang Flute why blame willows, spring breeze does not do Yumenguan. -- < Liangzhou Ci >> Wang ZhihuanCopy the code


Where there are transactions on the Internet, there are orders.

Order status in e-commerce system

There is no doubt that the order system, as the core of e-commerce, involves almost every system of e-commerce, so its position is very important. This article will document some of the key technical points involved in order design.

Order architecture design

Logical architecture

From the user’s perspective, look at the basic functional requirements of the order:

The data architecture
The basic information module contained in the order

Simplified version of class diagram

Payment Information

Payment method, payment number, payment amount, payment status, payment time, etc.

Promotion Offers

Preferential method, preferential amount, etc.

Delivery Information

Consignee, consignee address, consignee telephone number, express, etc.

OrderDetail Order details

Merchandise, store, price, quantity.

The Order of the Order

Users, order time, order status, etc.

AfterSaleOrder

Deployment architecture

Order-service Indicates the order service

It is autonomous and does not depend on other services except MQ services. Order creation and status changes are published to MQ via MQ-service, and other services that need to pay attention to the order can subscribe to MQ.

Order-gateway Indicates the order gateway

The interface is exposed to external services as an aggregation service, for example, when searching for orders, search-service and order-service are invoked through the order-gateway to aggregate data and return it to the business side.

The development of architecture

The order-service is used as an example to list the package structure for your reference

The repository storage bag

Interact with the bottom storage, responsible for data increase, delete, change and check, no business logic

The business the business package

Implementation of business logic processing, stateless, for example, to provide the order state transition function

Aggregation aggregation package

Responsible for calling business for business processing and storing the results of business processing through Repository

The controller control layer

Complete the user requirement by calling Aggregation

Order status machine

The state machine

A state machine is an abstraction of the real world, a mathematical model. There are four elements:

  1. State: The current State
  2. Event: A condition that triggers an operation
  3. Action: Indicates the operation to be performed after conditions are met. After the action is completed, it can be migrated to a new state, or it can remain in the original state. The action is not required. If the condition is met, you can migrate to the new state without performing any action.
  4. Transition, “Transition,” is a secondary state, a new state to which you have to move when conditions are met. “Substate” is relative to “state”.
The order status
  1. Order status: to be paid, to be shipped, to be received, order completed, refund in progress, order closed
  2. Order events: place, pay, deliver, receive, apply for refund, confirm refund

The state machine:

Order idempotent operation

F (f(x)) = f(x) is characterized by the fact that any number of executions have the same effect as one execution.

Idempotent functions are functions that can be executed repeatedly with the same parameters and obtain the same results. These functions do not affect system state, nor do they have to worry about system changes caused by repeated execution.

In an order process, ensuring that state changes such as order, payment, etc. are idempotent. If the idempotent operation is not guaranteed, multiple orders may be generated because users submit orders quickly or repeat requests due to network problems.

Means of guaranteeing idempotent:

  1. Database unique index: Order number sets a unique index
  2. Distributed lock: when placing an order, the order number is a lock, and the distributed lock is obtained first. After the lock is obtained, the operation of placing an order can be performed. If the lock is not obtained, the direct and rapid failure response will be made

Order search

The order involves the buyer order and the seller order, both sides have the demand to query the order, the single library single table is good to say, but after the single quantity is large, it involves the sub-library sub-table, this time it is not good to query.

One solution is redundancy, where a user keeps an order based on the seller dimension when placing an order.

  1. Customer order form
  • Save key fields for table display, such as order status, order time, order amount, order quantity, etc. Other details can be queried from the order details table.

  • With buyer_ID (buyer identification) as the dimension of sub-database sub-table

  1. Store order sheet
  • Save key fields for table display, such as order status, order time, order amount, order quantity, etc. Other details can be queried from the order details table.
  • Select seller_ID (seller id) as the dimension for database classification
  1. Order sheet
  • Save order details data
  • Warehouse and table with order_NO as dimension
  1. Place an order to synchronize data to ES
  • This function does not query the database directly, but searches for keywords from ES and returns list data. If you look at the details, then according to the order number to the details.
  • When placing an order, the order data is typically sent to and written to a search engine via MQ

  • Asynchronous write queue, and then to ES, there will be a certain delay, product design should consider the user experience, generally, the success of the order will jump to a success page.

Orders and Inventory

There are three common ways to reduce inventory:

  • Order destocking
  • Payment successfully reduce inventory
  • Pre-capture inventory: Pre-capture inventory when placing an order and hold it for a certain time, such as 30 minutes. After the order times out, the inventory is released.

Here we focus on the pre-occupation inventory process:

  1. Pre-occupy the inventory according to the order number when placing an order
  2. After successful payment, inventory will be deducted according to order number
  3. Order time out, release inventory according to order number

Order and Payment

The process of using third-party payment

  1. After the user successfully pays through wechat or Alipay, the payment status of the order is pending confirmation
  2. Update payment status via payment gateway asynchronous callback notifications
  3. If the payment callback notification is not received after a certain period of time, such as 30 seconds, the order scheduling service proactively requests the payment gateway to query the payment result