Recently, Dolbeault–Esteban–Figalli–Frank–Loss (Camb J Math 13(2):359–430, 2025) established the optimal stability of the first-order Sobolev inequality with dimension-dependent constant. Subsequently, Chen–Lu–Tang (Adv Math 479:110438, 2025) obtained the optimal stability for the fractional Sobolev inequality of order s when \(0<s<1\) with dimension-dependent and order-dependent constants. However, it left the optimal stability question for Sobolev inequality of order s when \(1<s<\frac{n}{2}\) unsolved. Furthermore, the optimal stability for Hardy–Littlewood–Sobolev (HLS) inequality still remains open although the authors in Chen et al. (Adv Math 450:109778, 2024) have established the stability of the HLS inequality with the explicit lower bound. The purpose of this paper is to solve these problems. Our strategy is to first establish the optimal stability for the HLS inequality. The main difficulty lies in establishing the optimal local stability of HLS inequality when \(1<s<\frac{n}{2}\) . The loss of the Hilbert structure of the distance appearing in the stability of the HLS inequality brings much challenge to establishing the desired stability. To achieve our goal, we develop a new strategy based on the \(H^{-s}\) -decomposition instead of \(L^{\frac{2n}{n+2s}}\) -decomposition to obtain the local stability of the HLS inequality with \(L^{\frac{2n}{n+2s}}-\) distance. However, this kind of “new local stability” adds new difficulties to deduce the global stability from the local stability using the rearrangement flow because of the non-uniqueness and non-continuity of \(\Vert r\Vert _{\frac{2n}{n+2s}}\) for the rearrangement flow. We establish the norm comparison theorem for \(\Vert r\Vert _{\frac{2n}{n+2s}}\) and “new continuity” theorem for the rearrangement flow to overcome this difficulty (see Lemmas 3.1, 3.3 and 3.5). This method we develop here is particularly useful in dealing with the stability of our geometric inequalities here involving the non-Hilbertian distance. As an important application of the optimal stability of the HLS inequality together with the duality theory of the stability developed in Chen et al. (2024), we deduce the optimal stability of the Sobolev inequality of order s when \(1\le s<\frac{n}{2}\) with the dimension-dependent constants. As another application, we can derive the optimal stability of Beckner’s (Acta Math Sin Engl Ser 31:1–28, 2015) restrictive Sobolev inequality on the flat sub-manifold \({\mathbb {R}}^{n-1}\) and the sphere \({\mathbb {S}}^{n-1}\) with dimension-dependent constants.