Meitu Mobile DNS Optimization Practices and Non‑Intrusive SDK Integration on Android
This article details Meitu's comprehensive DNS optimization for Android apps, comparing LocalDNS and HTTP DNS, describing metric‑driven strategies, caching mechanisms, and a non‑intrusive SDK that works in both Java and native layers while handling HTTP, HTTPS, and SNI challenges.
In the mobile Internet era, DNS resolution time and stability directly affect user experience, especially for Meitu's Android apps which suffer from DNS hijacking and latency spikes.
The article first explains the role of DNS and the problems of LocalDNS, such as ISP caching and NAT forwarding, which can cause domain hijacking and request failures.
It then introduces HTTP DNS as an alternative: instead of sending UDP queries to ISP DNS servers, the client sends an HTTP request containing the domain name and its own IP to a dedicated HTTP server that returns the resolved IP address.
Advantages of HTTP DNS include bypassing ISP DNS, precise traffic steering, and strong extensibility.
Meitu adopts a hybrid strategy: core API domains use HTTP DNS, while non‑critical requests first try LocalDNS and fall back to HTTP DNS when quality metrics degrade.
Key quality metrics used to evaluate DNS servers are TTL anomalies, resolution latency, and IP connectivity tests.
Because Android's standard APIs provide limited information, Meitu's SDK constructs its own DNS query packets and queries multiple ISP DNS servers.
To reduce latency, the SDK caches DNS records locally and employs a lazy‑update mechanism: expired records are returned immediately while an asynchronous refresh updates the cache.
A periodic background task scans the cache to proactively refresh stale entries.
For integration, the SDK offers a non‑intrusive approach. In the Java layer, it replaces the system AddressCache via reflection so that all Java‑level network libraries (HttpURLConnection, OkHttp, etc.) automatically use the SDK's DNS results without URL rewriting.
In the native layer, the SDK patches the ELF .rel.plt table of loaded .so files to redirect calls to getaddrinfo (or gethostbyname2 ) to a custom implementation, enabling DNS interception for WebView, media players, and other native components.
When using IP‑direct connections for HTTP, the SDK sets the Host header manually. For HTTPS, it reconfigures HostnameVerifier to verify the original domain and discusses handling SNI by providing a custom SSLSocketFactory .
Code examples for HTTP and HTTPS IP‑direct connections are provided:
URL htmlUrl = new URL("http://1.1.1.1/");
HttpURLConnection connection = (HttpURLConnection) htmlUrl.openConnection();
connection.setRequestProperty("Host","www.meitu.com"); final URL htmlUrl = new URL("https://1.1.1.1/");
HttpsURLConnection connection = (HttpsURLConnection) htmlUrl.openConnection();
connection.setRequestProperty("Host","www.meipai.com");
connection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return HttpsURLConnection.getDefaultHostnameVerifier().verify("www.meipai.com", session);
}
});Performance results after SDK deployment show a reduction of DNS lookup latency, a ~100 ms decrease in overall request time, and improved success rates, demonstrating the effectiveness of the combined LocalDNS/HTTP DNS strategy and the seamless SDK integration.
Images illustrating DNS flow, SDK architecture, and performance metrics are included throughout the article.
High Availability Architecture
Official account for High Availability Architecture.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.