Design a business platform redeem system

1, described business needs

Now assume that the interviewer to come out to redeem the relevant requirements of this system electronic business platform are as follows:

Electronic business platform where users usually through the purchase of goods, sun single comment can continue to accumulate points after accumulating enough points, they can redeem electronic business platform of the page, choose to use their points to exchange some gifts.

Demand actually so simple, so the interviewer said, given your thought process and implementation of this mechanism should be noted that in some places here for this business scenario.

 

2 , thinking of business processes

 

How to think? First, the user purchase goods and non-stop sun single comment, will continue to earn points, it is not a required standings, designed to store each user points it? Yes, this is necessarily a table, the table can be given the structure of the scene below.

 

Standings :

 

id (increment id primary key)

 

user_id (user id)

 

at Credit (integral)

 

Continues to run, assuming redeem page, users choose to use their 20,000 points to redeem a bottle of shampoo, how should the background logic design?

 

This site also have to give a thought process, this fact seems simple, but a lot of younger, less experienced friends, may not be able to think quickly out.

 

First you use 20,000 points to be redeemed, then it must be in the points table must be deducted 20,000 points this out, right? So in the design process, we must first have a process of deduction of 20,000 points.

 

Second, you use this 20,000 redeem something of it?

 

So you still need a separate table, called redeem record sheet, record the user you redeem this much a piece of merchandise with what?

 

This Redeem record table structure is shown below, you are not required in the following table to insert a record, said the number of users of this product which redeem the use?

 

Redeem table :

 

id (increment id primary key)

 

user_id (user id)

 

exchanged_credit (points for redemption)

 

product_id (exchange of goods id)

 

Finally, just inserted into the piece redeem record is not enough, you have to call the warehousing module interface, a new notification module shipments warehousing business applications, and should be shipped redeem the corresponding application, thus ensuring warehouse you can prepare the corresponding commodity shipments.

 

The application delivery configuration substantially corresponding to the following table:

 

Delivery Application Form :

 

id (increment id primary key)

 

of the type (delivery type, 1: buy, 2: redeem)

 

credit_exchange_id (Redeem table id)

 

product_id (goods to be shipped id)

 

In fact, there's a lot to simplify application delivery, so the number of fields have arguably had shipped goods, but here you can simplify the process is also all right, after all, is the interview site.

 

 

 

A simple drawn flow shown as follows.

 

3 , logistics and distribution status inquiry, taking into account yet?

If the above that the entire business process to the interviewer said, get away with it?

of course not!

You can stand on the user's point of view, do you still need to see is certainly not redeem the record? In redeem this table to see how much you can redeem points for what merchandise.

But you exchange goods distribution schedule, and you can view it? Can not. So you should be in the business process re-taken into account in the distribution of the corresponding logic.

In general, a basic logic, it is time production shipments in the application form, call the third-party logistics logistics company to apply for a single interface, so that the warehouse manager packaged goods are ready, waiting for the logistics company delivery of goods to close it.

Single logistics companies to carry out logistics and distribution according to this delivery address is certainly one of your addresses in the user's own electronic business platform of choice.

At this time, requisition shipping table structure is not shown below?

Delivery Application Form :

id (increment id primary key)

of the type (delivery type, 1: buy, 2: redeem)

credit_exchange_id (Redeem table id)

product_id (goods to be shipped id)

express_no (single stream number)

Therefore, in the production of a single application delivery, come to call third-party logistics logistics company to apply for a single interface, so a single application delivery is a single number of logistics and redeem each record by id shipped with the application single associate.

Such exchange for each record, is not to be found in a single application delivery logistics a single number on the page, and then call the third-party logistics company logistics interface based on a single number, to acquire distribution schedule?

This is a very logical thinking techniques typical business system implementation, a rich experience of qualified engineers, often have a certain amount of business thinking, well to consider user logical business systems, thrust reversers own system technology logic.

Whole procedure, as shown below:

 

 

 

 

4 , to ensure that matters

After the stroke along the entire business process, the next step involves the consideration of the technology. You have to think about it, how can such a business transaction system does not it?

Deduction of points, redeem new record, new shipping application form, three steps must be completed either together or fail together. In other words, these three steps must be within a transaction of.

There is a problem, a business platform for their business systems, contains only integral service. But the warehousing services typically deployed independently of a system or a stand-alone service.

In other words, the deduction of points and redeem new record can be a service there is a transaction, but the new shipping application form, he is in another service where, and how to ensure this transaction it?

A friend may immediately answer: use distributed transactions ah! Do not worry, we can start with the most simple model to achieve it.

In one example, point service transaction code block, execute deduction integration, new redeem recording two steps.

Then remember, in the transaction code block, the last step call interface warehousing services, if the interface call is successful, then the transaction can be committed. If the interface call fails, then throw an exception so that the transaction is rolled back, so you can not?

This process is as follows:

        Integral service transaction {

               -> points deduction

               -> Add redeem record

            -> call Warehousing Services

        }

5 is introduced, messaging middleware

In fact, the above design is theoretically no problem, but here you ignore a problem, in this business scenario, service integration is not necessary synchronous calls warehousing services.

Because Redeem a user operation is performed, assuming your storage service call when generating shipment of Requisition interface third-party logistics company, was stuck or fail, how do?

At this point they'll probably see after the redeem button clicks, there may be tens of seconds card can not be executed successfully, so the system is designed so that errors on the page.

That should be how to do it? You have got to be here to introduce decoupling asynchronous messaging middleware technology, to ensure the user after clicking Redeem button to return as soon as possible. As shown below:

 

 

6 , the introduction of a retry mechanism

Here on the OK yet? not yet!

Once the introduction of messaging middleware, benefit is the user clicks the redeem button is directly deducted points and redeem new record in integrating services, and then sends a message to the messaging middleware in the over, very fast, to ensure that the user experience.

But the downside is, if the implementation of new storage services delivery application fails how to do?

This time on the need to introduce a reliable messaging service, and he needs to go to ensure the completion of the new warehouse delivery services will apply in this matter.

Specific process is as follows:

Integral reliable service send messages to the message service, reliable messaging service message in a new record in the table, and then sends the message to the MQ (Message Oriented Middleware)

Then add warehousing services consumer news delivery application form, if successful callback interface to a reliable messaging service say that they are successful, reliable messaging services can update the local record status message table for success

If there is no reliable notification message service warehousing services for a long time I was a success, reliable sources of non-stop service to retry sending the message again

With this design, we can ensure reliable message service will allow unlimited retries warehousing services to ensure successful implementation. Coupled with the retry mechanism, the overall flow chart as shown below:

 

 

 

7 , the introduction of mechanisms idempotent

 

One last question, if the third-party logistics warehousing services card application system link logistics alone, prolonged obstruction, there is no reliable sources callback notification service.

 

But reliable messaging services over a period of time, the feeling did not receive a callback notification on their own to retry sending the message that this would not make two new warehousing services delivery application form?

 

So we have to ensure that new warehousing service delivery requisition idempotency, in fact, very simple, look at the structure of a single application delivery table:

 

Delivery Application Form :

 

id (increment id primary key)

 

of the type (delivery type, 1: buy, 2: redeem)

 

credit_exchange_id (Redeem table id)

 

product_id (goods to be shipped id)

 

express_no (single stream number)

 

As long as the establishment of a unique index on the "credit_exchange_id" field will be able to ensure that each record can only redeem create a single application delivery, if it will be repeated to create a unique index it is blocked, so as to ensure the power of this behavior, etc. of the.

 

So far, this topic duct system design answers all over.

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/lingboweifu/p/11897357.html