App-Side network optimization (DNS optimization, HTTP optimization)

Source: Internet
Author: User
Tags webp

I. Using HTTPDNS to optimize DNS resolution and caching

Generally in the app with the domain name to send requests through the DNS to resolve the IP, and then according to the IP to take the corresponding resources, the process, if there is a localdns in the domain corresponding to the IP, it will be directly returned to this IP, similar to the cache in the app. If it does not exist, it will go to the authoritative DNS query to access which IP, and then the queried IP will be cached in Localdns. That is, if we want to visit Sina http://api.weibo.cn, if the localdns inside the domain corresponding to the IP, it is directly returned to the IP. (DNS Fundamentals: HTTP://WWW.JIANSHU.COM/P/A73E963B63B1)

1. There are two problems here

If the previous access to api.weibo.cn is Unicom users, now new users use telecommunications to access api.weibo.cn, due to the existence of Localdns cache, will not go to query the authoritative DNS of Sina, so that the return of the IP is connected to the carrier's IP, which will make users appear slow access and other conditions. Caching also leads to the fact that when authoritative DNS changes the domain name and IP mappings, the user accesses the wrong server or accesses the resource directly because the LOCALDNS cache is not changed in time.

Many tier three or four operators point their operations to their cache servers and replace the ads in their pages with their own, or embed their own ads. (The previous app has been in this situation, the complaint will be good for a while, but after a while the ads will appear).

There is a problem in DNS resolution, that there is a precise scheduling, low cost, convenient configuration based on the domain name of the traffic scheduling system? The answer is yes. Httpdns traffic Scheduling solution based on HTTP protocol and domain name resolution can prevent the above problems to a large extent.

Httpdns principle:

A, the client directly accesses the Httpdns interface, obtains the service to configure the domain name Configuration management system the access delay optimal IP. (For disaster-tolerant considerations, the app must retain the use of operator Localdns to resolve domain names.) )

B, the client sends a business agreement request to this IP directly after the acquired IP. As an example of an HTTP request, send a standard HTTP request to the IP returned by Httpdns by specifying the host field in the header.

In general, the use of Httpdns to resolve the domain name, bypassing the three or four-tier operators to resolve the problem of domain names, after Httpdns returned the correct IP, we are directly using IP to make HTTP requests, only need to focus on the security of the communication content.

2, based on Httpdns extension

A. Maintain a serve IP List within the app. Each time the app takes the IP storage from Httpdns into the array and sets the weights, theoretically the IP weights that are parsed from the Httpdns are the largest. This list can be updated when the app is launched, and the most weighted IP of the locally cached serve IP list is taken out to initialize the data (if the first start, without the list, use Localdns to parse).

Serve IP list inside the weight of the mechanism, it is clear that the DNS is resolved from the IP has the largest weight, each time from the list to take the IP should take the most weight of the IP. The IP in the list is also required to dynamically update the configuration, depending on the connection or the successful failure of the service to dynamically adjust, so that even if the DNS resolution fails, the user will be able to access the appropriate IP after a period of time.

B, data statistics for the IP. In all apps to count the average time, the longest time, the shortest time, the number of successful requests, the number of failures, it is necessary to distinguish the network environment for statistics, WiFi, 4G, 3G, in different network environment, the data excellent IP storage, issued to the app used up. In this way, each time you launch the app, you can speed up the collected IP according to the different network environment, select the best IP to make the request. It is important to note that the speed test must be re-tested when the network environment switches. To do this, you can save DNS parsing time, and the problem of hijacking.

C, the picture, audio and other resources placed in a separate server, and other resources to separate.

The first is that multiple domain names can increase the number of parallel downloads because the client has a limit on the number of domain name downloads in the same domain, so multiple domains increase the number of parallel download bars, which speeds up loading. Of course, the two-level domain name can not be used too much, because too much to consider the DNS parsing time spent.
The second is easy to manage, in general, the picture in the site load is the most bandwidth, you can use a standalone server to facilitate post-management; You can also use asynchronous loading to enhance the user experience. At the same time, the picture is more static content, can be better use CDN acceleration.
Third, if the use of a standalone server, the security settings can have a different set of settings, it is convenient.

D, in the prevention of hijacking this piece, you need to pay attention to the resource suffix, such as. Mp3\.json such a suffix, so as not to hit the operator interception.

Second, resource optimization

Resource optimization is basically to reduce the size of the transmitted data as much as possible, the first is the image size solution.

1, to a certain extent using WEBP to replace JPG, PNG pictures

          

Is the size of a variety of images of the same quality, where WEBP is the smallest. When we download the image display, if we use the WEBP image to some extent, we can greatly reduce the loss of user traffic and the time required to download the picture.

In general, the app will be fixed using several sizes to do the display, for example, banner map is 640*640,cell display may be 320*320, 280*280, similar avatars may be 80*80 and so on. It should be noted that WEBP images to be usable JPG images through parsing, in iOS development, you can use the Sdwebimage framework for parsing, Webp->nsdata->image,app internal parsing is sure to take a certain amount of time and performance.

According to the actual use of feedback, found in the WiFi conditions, more than 300*300 pictures using webp pictures, parsing time + download time is faster than the direct use of jpg\png pictures, and in the traffic is also very small consumption. Below 300*300 can be downloaded directly using jpg\png pictures.

Under 4G conditions, users may be more sensitive to traffic, it is recommended to go WEBP pictures.

2, can use Protocolbuffer instead of JSON for data transmission

Protocolbuffer (abbreviated as PROTOBUF or PB) is a data interchange format introduced by Google, which is independent of language and platform-independent. Google offers implementations in three languages: Java, C + +, and Python, each with a compiler and library file for the appropriate language. It can be used in data communication between distributed applications or in heterogeneous environments. Unlike traditional XML and JSON, it is a binary format that avoids all kinds of problems with the conversion of text format, and the conversion efficiency is very fast, because of its cross-platform, cross-programming language characteristics, make it more and more popular, especially the network data exchange is becoming a mainstream.

In TCP, we can use Protocolbuffer instead of JSON for data transfer because the Protocolbuffer data is smaller than JSON and is cross-platform, and the sequence number and deserialization are simple. In a real project, the transfer speed is significantly increased when the data becomes smaller.

App-Side network optimization (DNS optimization, HTTP optimization)

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.