In-depth analysis of the working mechanism of Java I/O (network I/O optimization)

Source: Internet
Author: User

Network I/O Optimization

Network I/O optimization usually has some basic processing principles:

1. One is to reduce the number of Network Interactions: to reduce the number of network interactions, cache is usually set at both ends of the network interaction, such as the JDBC driver of oracle, the cache of the queried SQL results is available on both the client and database, which can effectively reduce access to the database. For more information about oracle JDBC memory management, see Oracle JDBC memory management. In addition to setting cache, You can merge access requests. For example, when querying a database, we need to query 10 IDs. I can check one ID each time or 10 IDs at a time. For example, when accessing a page, multiple JS or CSS
, Then Package the package and send it back to the browser. These are common methods to reduce network I/O.

2. Reduce the volume of data transmitted over the network: to reduce the volume of data transmitted over the network, compress the data before transmission, for example, in an HTTP request, generally, the web server compresses the requested web page gzip and transmits it to the browser. (For more information about gzip compression configuration on the nginx server, see here.) There is also a simple protocol designed to obtain useful value information by reading the protocol header. For example, when designing a proxy program, both layer-4 proxy and layer-7 proxy are used to avoid reading the entire communication data to obtain the required information.

3. Minimize coding: data transmission in network I/O is usually in bytes, that is, serialization. However, the data we send to be transmitted is in character format and must be encoded from character to byte. However, this encoding process is time-consuming, so when it is necessary to pass through the network I/O transmission, try to directly send in bytes. That is, try to convert characters into bytes in advance, or reduce the conversion process from characters to bytes.

4. design an appropriate interaction mode based on the Application Scenario: the so-called interaction scenarios mainly include synchronous and asynchronous blocking and non-blocking modes. The following describes in detail.

Synchronous and asynchronous

Synchronization is a reliable task sequence that depends on another task only after the dependent task is completed. Either the task succeeds or fails, and the status of the two tasks can be consistent. Asynchronous mode does not need to wait for the dependent task to complete. It only notifies the dependent task to complete the task, and the dependent task is also executed immediately, as long as the entire task is completed by itself. As to whether the depended task is actually completed, the task dependent on it cannot be determined, so it is an unreliable task sequence. We can call and send text messages to compare synchronous and asynchronous operations.

When designing Io processing, we often encounter a problem of synchronous or asynchronous processing methods. This problem occurs in database products because synchronous and asynchronous I/O processing methods have a great impact on callers. Because I/O operations are usually very time-consuming operations, I/O in a task sequence is usually a performance bottleneck. However, the synchronous and asynchronous processing methods have a great impact on the program's reliability. synchronization can ensure the program's reliability, while Asynchronization can improve the program's performance and must balance the reliability and performance, there is no perfect solution.

Blocking and non-blocking

Blocking and non-blocking mainly refer to CPU consumption. Blocking means that the CPU stops and waits for a slow operation to finish the CPU before other tasks are completed. Non-blocking means that when this slow operation is executed, the CPU does other things. When this slow operation is completed, the CPU then completes subsequent operations. Although the non-blocking method can obviously improve the CPU utilization, it also has another consequence: the thread switching of the system increases. Whether the increased CPU usage time can compensate the system switching cost requires a good evaluation.

Combination of the two methods

The combination can be divided into four methods: Synchronous blocking, synchronous non-blocking, asynchronous blocking, and asynchronous non-blocking. These four methods affect the I/O performance. The following analysis is provided and some common design case references are provided.

Table 3. Four combination methods

Combination Method

Performance Analysis

Synchronization Blocking

The most common usage is also the simplest, but the I/O performance is generally poor, and most of the CPU is idle.

Non-blocking Synchronization

A common method to improve I/O performance is to change the blocking mode of I/O to non-blocking mode, especially when the network I/O is a persistent connection, at the same time, data transmission is not very effective in many cases.
This method can usually improve I/O performance, but it will increase CPU consumption. Consider whether the increased I/O performance can compensate for CPU consumption, that is, whether the bottleneck of the system is on I/O or CPU.

Asynchronous Blocking

This method is often used in distributed databases. For example, if you write a record in a distributed database on the Internet, there is usually a synchronization blocking record, there are two to three copies of the backup records that will be written to other machines. These backup records are usually written to I/O in asynchronous blocking mode.
Asynchronous blocking improves network I/O efficiency, especially when multiple identical data copies are written at the same time.

Asynchronous non-blocking

This combination method is complex to use, and is only used in some very complex distributed scenarios. For example, the message synchronization mechanism between clusters generally uses this I/O combination method. For example, Cassandra's gossip communication mechanism adopts the asynchronous non-blocking mode.
It is suitable for transmitting multiple copies of the same data to different machines in the cluster at the same time. Although the data transmission volume is small, it is very frequent. This network I/O can achieve the highest performance in this way.

Although asynchronous and non-blocking can improve I/O performance, it also brings some extra performance costs, such as increasing the number of threads and thus increasing CPU consumption, at the same time, the complexity of program design increases. If the design is unreasonable, performance will decrease. Make a comprehensive evaluation based on the Application Scenario during actual design.

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.