Architecture Design: Producer/consumer model [1]: How to determine a data unit?

Source: Internet
Author: User

Since the previous post
I have already worked on literacy, so I should start to talk about some specific programming technical issues. However, before entering the specific technical details, we must first understand the question: how to determine the data unit? Only by clearly analyzing data units can we develop the technical design later.

What is a data unit?

What is data unit pinching? Simply put, each producer put in a buffer zone is a data unit. Each consumer fetches data from the buffer zone. For the previous post
For example, we can regard each letter as a data unit.
However, this introduction is too simple to help everyone analyze this stuff. So let's take a look at the features that data units need. After understanding these features, it is easy to analyze what is suitable for data units from complicated business logic.

Features of data units

To analyze data units, consider the following features:
◇ Associate with Business Objects
First, data units must be associated with certain business objects. When considering this issue, you must have a deep understanding of the current producer/consumer modelBusiness Logic
To make a proper judgment.
Because the business logic of "Sending mail" is relatively simple, everyone can easily determine what a data unit is. But in real life, it is often not so optimistic. Most of the business logic is complex. The business objects contained in the logic are of various levels and different types. In this case, it is difficult to make a decision.
This step is very important. If the wrong business object is selected, the complexity of subsequent programming and coding implementation will be greatly increased, increasing the development and maintenance costs.
◇ Integrity
Integrity means to ensure the integrity of the data unit during transmission. OrWhole
The data unit is either passed to the consumer, or completely not to the consumer. Not AllowedPart
Transfer situation.
For a mail, you cannot put half a mail into a mail box. Similarly, the postman can neither take the mail from the mail box, nor just take out a part of the mail.
◇ Independence
The so-called independence means that each data unit is not mutually dependent. The transmission failure of a data unit should not affect the unit that has completed the transmission, nor affect the unit that has not yet been transmitted.
Why does the transmission fail? If the producer's production speed exceeds the consumer's processing speed for a period of time, it will cause the buffer to grow and reach the upper limit, and the subsequent data units will be discarded. If data units are independent of each other and the producer's speed drops, subsequent data units will not be affected. Otherwise, if some coupling exists between data units, the discarded data units will affect the processing of other units in the future, which will complicate the program logic.
For a mail, the loss of a mail will not affect the delivery of subsequent mail; of course, it will not affect the delivery of delivered mail.
◇ Granularity
As mentioned above, data units must be associated with certain business objects. So do data units and business objects need to be interconnected one by one? In many cases, it is indeed a one-to-one correspondence.
However, sometimes n business objects may be packaged into a data unit for performance and other reasons. Then, how to set the N value is a matter of granularity. The granularity is exquisite. A large granularity may cause some waste; a small granularity may cause performance problems. The balance of granularity should be based on many factors, as well as some experience values.
Or an example of sending a mail. If the granularity is too small (for example, set to 1), the postman will retrieve only one letter at a time. If there are too many letters, you have to go back and forth for a long time.
If the granularity is too large (for example, set it to 100), the sender must wait until 100 messages are collected to put them into the mailbox. If you rarely write a letter at ordinary times, you have to wait for a long time.
Some may ask: Can the granularity of the producer and consumer be set to different sizes (for example, set the sender to 1 and the postman to 100 ). Of course, this can be done theoretically, but in some cases it will increase the complexity of Program Logic and code implementation. We may discuss the specific technical details later.

Well, this is the topic of data units. I hope that through this post, everyone can understand what the data unit is. Next post, let's talk about "queue-based buffer zone
", Technical implementation.


Copyright Notice

All original articles in this blog are copyrighted by the author. This statement must be reprinted to keep this article complete, and the author's programming will be noted in the form of hyperlinks
And the original address of this article:

Http://program-think.blogspot.com/2009/03/producer-consumer-pattern-1-data.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.