b/S Class project improvement

Source: Internet
Author: User
Tags soap

Topics to share
    1. Performance Improvement : How to increase the PV 2 limit of a single serverand increase the TPS 3 while the traffic is increasing gradually?
    2. RESTful: What are the advantages compared to traditional SOAP1,restful style architectures? What are the differences between the practices?
    3. Micro-Services : As enterprises become larger, the system will become larger and more difficult to maintain, how to ensure "stability" at the same time, but also to ensure that small enterprises "flexible"?
Brief introduction to Performance improvements

The most common way to improve performance can be solved by using the server's cluster, and the simple and rude understanding is to increase the number of bank teller. But it's not smart to just consider delivering performance from the service side-it should be cost-effective. Of course, the core must be to increase the server's TPS, that is, in the shortest possible time to provide services to the largest number of customers . Server clusters can dramatically improve overall performance, but we're talking about how to improve the performance of a single server.

    1. Server pressure comes from three main areas: CPU, network, and disk IO. Disk as the most vulnerable side of the bottleneck, you must find ways to reduce IO operations. The database is a big part of data persistence storage and disk overhead, which is mainly to reduce or merge database operations.
    2. The fluency of the system depends on the good cooperation between the server and the client. Web-based projects that leverage browser resources not only reduce server stress, but also provide a better customer experience. Modern browsers generally conform to the RFC26164 specification. Among the important headers are the ETag and the last-modified header-the browser's cache settings switch, which maximizes the use of client resources.
RESTful architectural Style

Like process-oriented programming and object-oriented programming, there is no clear boundary between the two. In the right place with the right style of architecture, the focus is on the use of the most. But RESTful as a new style, must have its advantages:

    1. In a RESTful architecture, the focus is on resources . Each has an address, and the resource itself is the target of the method invocation. The method list is the same for all resources. These methods are standard methods, including HTTP GET, POST, PUT, DELETE, and possibly PATCH, HEADER, and OPTIONS. The idea is that the remote provides a range of resources that the client needs to download, display, edit, and commit changes, with a focus on the local.
    2. In the RPC schema, the focus is on methods . In the client's view, it is the combination of conditions on the client, then execution in the server, and finally feedback to the client. The idea is to hide implementation details, or to correlate other RPC service operations, with a focus on the server.
Micro-service and monomer applications

Modern monolithic applications, usually in a modular manner, are developed in parallel around the core modules. Eventually they needed a joint test , deployed as a monolithic application:C # would be deployed as a Web site for IIS, and Java would be packaged into a War format to deploy Tomcat.

As time goes by, the monomer will become more and more big after dealing with more and more new demands. More unfortunately, many of the hastily-responded code is added to the app because of the misalignment of the company's resources and needs. These codes will not be problematic in the short term, but fixing bugs and adding normal new functionality will become more difficult because it usually involves multiple modules, reaching . This is the bottleneck of the monolithic application, which is considered to be split into multiple subsystems. Of course, this will continue for some time until a similar problem arises.

Many companies, such as Amazon, EBay, and NetFlix, have addressed these issues by adopting a micro-processing architecture model . The idea is not to develop a huge monolithic application, but to break down the application into small, interconnected microservices.

A micro-service typically accomplishes a specific function, such as order management, customer management, and so on. Every micro-service has its own business logic and adapters. Some microservices also publish APIs for use by other microservices and application clients. Other microservices complete a Web UI.

Performance improvements
    1. database static : The database does not contain operational logic, and all operational logic is completed within the program.
    2. reduce external IO: Use data caching, merge database operations, read and write separations.
    3. Async : For non-mandatory methods, asynchronous execution makes it not affect the current logic.
    4. subsystem split : Split long-running logic for a Windows service or JOB.

a chestnut : e-commerce system, the start of a single operation involves the invocation of multiple modules. But when the user orders, do not care about these, as long as you get a success of the order of the results can be. We can analyze: The system first to the user submitted information validity check, and then the business data accuracy check, and finally submitted to the database. A successful e-commerce system, the first two must be able to complete in a short period of time, and in the second kill sell this scenario without causing the database to crash.

Based on the above two points, we analyzed how to optimize the second kill sale in this scenario of the operation process.

    1. Service-side verification of the validity of information, the operation of the most intensive frequency, the fastest speed, so should not involve in addition to memory operations, such as: Redis and database Read and write, tcp/http remote calls.
    2. Business data validation, correlation module many, speed requirements faster, so should not involve slow IO operations, such as: Database Read and write, HTTP remote call.
    3. Write the database frequently, in a short time to the database caused a lot of continuous pressure, speed requirements quickly, so here can take immediate feedback, later write the way to execute.
Database static

The operation of the database is locked: The Select statement publishes the shared lock 5,insert, Update, and Delete release exclusive lock 6. Therefore, in the operation of the same table under the premise, the database operations are serial 7.

Based on the above considerations, let the database do only storage containers, not responsible for the operation is the way. It is because the operation of the database is serial, in the large concurrency write, any point of Ascension is to fight, so here to put the task of the operation to refer to the execution of the program.

    1. Stored procedures because of the application logic in the database, generally certainly include computing tasks, considering the level of general development can not be guaranteed to use temporary tables to store pre-computed data (can be too cumbersome, very prone to exceptions), and finally unified execution, so first of all, to discard the stored procedure containing the database write class .
    2. the operations within the program, if they are still present after the transaction is opened, are counted into the database's operational tasks. because the database transaction is opened, the exclusive serial has already begun, the operation time of the program not only takes up the operation time of the program, but also occupies the opening time of the database transaction, in essence, it does not reduce the opening time of the database transaction. Strictly, this approach is not even as good as the previous one.

Therefore, the real database static is: First in the program operation, the database to execute the SQL write statements and parameters, continuous operation until all the database write command, and then open the database transactions, in accordance with the FIFO principle sequential Execute the database write command (this time period cannot contain other non-database operations). Only in this way can we ensure that the execution of the command is static write, and the shortest time to lock the database, to ensure the maximum reduction of database pressure.

Another, it is because the operation of the database is serial, so in the case of database writing is not readable, to avoid dirty read , the database read and write separation is necessary. Set up from the library, the main library is responsible for writing, from the library is responsible for reading, the database pressure is divided into multiple platforms.

Read and write separation of database note: The data that has just been written to the database, synchronized to 2-3 seconds from the library, needs to change the process on the business so that the data is synchronized when the user retrieves it.

Reduce external IO

Because of disk limitations, the read and write speed is not proportional to memory, so this is the second place where bottlenecks can occur. You can consider the way that configuration information is pre-read to the cache.

Because the database is based on the hard disk exists, so the database read and write relative to the validation and business verification, is the time consuming large. In the case of purely database writes, it is possible to write to the business from the database in which the order is stripped from the system. To eliminate the meaningless wait for the database to write time, and to reduce the consumption of CPU time slices, the time

The other is the database read and write operations, within a database transaction, there is no second database to perform the same operation. Considering the introduction of data static, when the database transaction is opened, the operation in the program is actually the time of the database Operation . Consider delaying the opening of the database transaction at this point: first in the program to generate the database command to execute, and then open the database transaction, in a continuous period of time to execute the batch database transactions. That is, merging database operations into a database execution.

Asynchronous

In the process of development, it is unavoidable to interact with its associated modules, and these interactions do not affect the current business logic. This kind of operation should be changed into asynchronous way. In the process of database interaction, if the user does not need to wait for the return value of the database, the database can also be executed asynchronously, in the shortest time possible to feedback execution results.

a chestnut : In the process of placing orders, the operation of submitting an order is not concerned about the successful failure of the submission, but only the details of the order will be seen after the subsequent jump to the order details. This process allows the database to be executed asynchronously, and when the server receives the user's submission request, it can submit a successful response to the user after validating the data. Next, it is saved to the database by means of an asynchronous queue. After receiving a successful submission, the client prompts the user for a successful submission, but does not give any information about the order. A database query is performed only if the user clicks the View order list proactively. This time difference is enough for the system to process the actual commit operation of the order.

The most direct benefit of this is to improve the responsiveness of the Web site, optimize the user experience, while upgrading the server TPS, while not increasing the pressure on the database. In the second to kill the special sale, can maximize the avoidance of oversold situation.

subsystem splitting

Continuing with the above example, the database submits the order operation, and the site does not have much of a relationship. At this point you can consider splitting this part into a Windows service that communicates through Message Queuing. The serial reading of the queue is exactly the same as the serial execution of the database, and it does not exceed the limit of the database during the peak period, causing the outage. In the case of exceeding the service limit, it is always better to handle slow than to handle.

From the operating system, system scheduling is equal to each process. Here, the execution of the database from the Web site, reducing the database operation on the CPU time slice, the side of the site to enhance the service capabilities.

RESTful architectural style and SOAP architecture style
    1. attribute Routing : replaces the traditional flat-panel method name URI with an incremental URI.
    2. Client Cache Header : Use ETag and last-modified header to relieve server pressure.
    3. crud: Use the GET, POST, PUT, and DELETE methods to differentiate database CRUD operations.
Attribute Routing

The first WEBAPI version uses a convention-based route. In this type of route, you can define one or more templates that are parameterized strings. When the framework receives a request, it matches a URI to the routing template.

One advantage of convention-based routing is that the template is defined in a single place where the routing rules are consistently applied to all controllers. Unfortunately, convention-based routing is difficult to support the exact URI pattern, and this exact URI pattern is common in RESTful APIs. For example, resources often contain sub-resources: Customers place orders, movies have actors, books have authors, etc., it is natural to create these URIs to reflect these relationships:

/customers/3/orders

This type of URI is more difficult to implement under convention-based routing. Although it can be done, if you have a lot of controllers or a lot of resource types, you can't be very well extended. But for attribute routing, it's easy to define a route for this URI, and you can simply add a property to the Controller's action:

[Route("customers/{customerId}/orders")]public IEnumerable<Order> GetOrdersByCustomerId(int customerId) { ... }
Convenient API version control

Sometimes we need to develop a new version of a feature, but don't want to have an impact on existing functionality, such as: api/v1/products and api/v2/products can be routed to different controllers. A good distinction is made during the development phase, and when the new version is commercially available, it is also easy to expire or deactivate the V1 version of the controller.

Overloaded URI Fragment

In the following example, 12306 a specific ticket is represented, and a notravelled collection of tickets is not travelling.

/tickets/12306/tickets/notravelled

Through the natural semantics, people can easily understand the meaning of these URIs, but the convention-based approach is not very convenient to solve the problem.

Routing constraints

Attribute routing adds constraints that are not available in the Convention routing age, allowing you to restrict the matching of parameters in the routing template. The general syntax is {parameter:constraint} , for example:

[Route("users/{id:int}"]public User GetUserById(int id) { ... }[Route("users/{name}"]public User GetUserByName(string name) { ... }

If the fragment of the URI id is of a int type, then the first route will be selected, otherwise the second route will be selected. The routing of the attribute routing Convention special rules matches the route first, and finally matches the route without any constraints. Be careful not to have two possible matches, or there will be a multi-match problem, such as:

[Route("{id:int}")]public string Get(int id)[Route("{id:decimal}")]public string Get(decimal id)

It is important to note that the WEBAPI framework has a Bug that does not support decimal points, such as: It /values/v1/8.3 will not be parsed into a decimal type.

The following is a list of supported constraints:

Constraints Description Usage Demo
bool Type match (Boolean type)
Datetime Type match (DateTime type) {x:datetime8}
Decimal Type match (Decimal type) {x:decimal9}
Double Type match (64-bit floating-point number) {x:double9}
Float Type match (32-bit floating-point number)
Guid Type Match (GUID)
Int Type match (32-bit integer)
Long Type match (64-bit integer)
Alpha Character composition (must consist of Latin alphabet)
Regex Character composition (must match the specified regular expression)
Max Value range (less than or equal to the specified maximum value)
Min Value range (greater than or equal to the specified minimum value)
Range Value range (between the specified minimum and maximum values)
MaxLength String maximum length (less than or equal to the specified length)
MinLength String minimum length (greater than or equal to the specified length)
Length String length (equal to the specified length or length within the specified range)
Client Cache Header Basics What is Last-Modified

When the browser requests a URL for the first time, the server-side return status is 200, the content is the resource you requested, and there is a Last-Modified property that marks the last time the file was modified at the end of the service period, similar in format:

Last-Modified: Fri, 12 May 2006 18:53:33 GMT

When the client requests this URL for the second time, according to the HTTP protocol, the browser sends a header to the server If-Modified-Since asking if the file has been modified after that time:

If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT

If the server-side resources do not change, the HTTP 304 (not Modified) status code is returned automatically, and the content is empty, thus saving the amount of data transferred. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. This ensures that the resources are not duplicated to the client, and that the client is able to get the latest resources when the server changes.

What is ETag

The HTTP protocol specification is defined ETag as the entity value of the requested variable , and the other is a token that ETag can be associated with a Web resource: A typical Web resource can be an HTML page, but it could also be a JSON or XML document. The server is solely responsible for judging what the token is and what it means, and transmitting it to the client in the HTTP response header, which is the format returned by the server side:

ETag: W/"9e10cdada3f741f6b0802ee31179837d"

The client's query update format is this:

If-None-Match: W/"9e10cdada3f741f6b0802ee31179837d"

If it ETag does not change, the return status Code 304 content does not return, this is also the Last-Modified same. I test ETag mainly in the breakpoint download is more useful.

Last-ModifiedAnd ETagHow can I help improve performance?

Smart developers will use the Last-Modified ETag HTTP header with the request, which can take advantage of the caching of the client (for example, the browser). Because the server first generates Last-Modified / ETag marks, the server can later use it to determine if the page has been modified. Essentially, the client requires the server to validate its (client) cache by passing the token back to the server. The process is as follows:

    1. The client requests a page (a).
    2. The server returns page A, and adds a/to Last-Modified a ETag .
    3. The client presents the page and caches the page Last-Modified along ETag with/.
    4. The customer requests page A again and passes the last request to the server that was returned by the server Last-Modified ETag .
    5. The server examines the Last-Modified or ETag , and determines that the page has not been modified since the last client request, directly returning the status code 304 and an empty response body.

The client here generally refers to the browser, which is used programmatically by the client, typically does not handle these two HTTP request headers.

Micro-Service

No further discussion is currently being made.

  1. SOAP(Simple Object access Protocol) is a protocol specification for exchanging data, a lightweight, simple protocol based on XML (a subset of standard generic markup languages). It is designed to exchange structured and solidified information on the web.

  2. PV: Page view, which is usually the main indicator for measuring a web news channel or Web site or even a Web news message. Web page views is one of the most commonly used indicators for evaluating website traffic, referred to as PV. Monitoring the website PV trends and analysis of the reasons for the change is a lot of webmaster regularly do the work. Page views in the page generally refers to ordinary HTML pages, but also contains PHP, JSP and other dynamically generated HTML content. An HTML content request from the browser is considered to be a PV, which gradually accumulates as PV total.

  3. TPS: (Transaction per Second) The number of transactions or transactions that the system can process per second. It is an important indicator to measure the system's processing ability. TPS is an important indicator of performance parameters in LoadRunner.

  4. RFC2616: The specification is now partially updated.

  5. shared locks : Similar to read locks in read and write locks. You can read more than one, but exclude write locks. Only after the read lock is released can the write lock be entered.

  6. exclusive lock : Similar to a write lock in a read-write lock. Only one write, the remaining operations must wait until the current write lock is released.

  7. serial : Only one thread is allowed to operate at the same time, and the remaining threads can only wait to complete before proceeding.?

  8. The constraint of a datetime type, if used as / a delimiter, must be placed in the last one, and * preceded by: {*x:datetime} . Currently, only this notation can span multiple URI segments.

  9. decimaland double two types of numbers, if the inclusion of the decimal point will not be normal parsing, currently can be counted as a WebApi framework of a Bug.

b/S Class project improvement

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.