9. (16 points) Given $a, b \in [1,3], a+b=4$.

Find the maximum value of $f(a, b)=\left|\sqrt{a+\frac{1}{b}}-\sqrt{b+\frac{1}{a}}\right|$.
9. Let $y=\left|\sqrt{a+\frac{1}{b}}-\sqrt{b+\frac{1}{a}}\right|$. Then
$$
\begin{aligned}
y^{2} & =a+b+\frac{1}{a}+\frac{1}{b}-2 \sqrt{\left(a+\frac{1}{b}\right)\left(b+\frac{1}{a}\right)} \\
& =4+\frac{4}{a b}-2 \sqrt{a b+\frac{1}{a b}+2} .
\end{aligned}
$$

Notice that, $a, b \in[1,3], a+b=4$.
Thus, $t=a b=a(4-a)=-a^{2}+4 a$
$$
=-(a-2)^{2}+4 \in[3,4] \text {. }
$$

Therefore, we can construct a one-variable function
$$
g(t)=y^{2}=4+\frac{4}{t}-2 \sqrt{t+\frac{1}{t}+2} \text {, }
$$

where $t \in[3,4]$.
Since $\frac{4}{t}$ is a decreasing function on the interval $[3,4]$, and $t+\frac{1}{t}$ is also a decreasing function on the interval $[3,4]$, $g(t)$ is a decreasing function on the interval $[3,4]$.
$$
\begin{array}{l}
\text { Thus, } g(4) \leqslant g(t) \leqslant g(3) . \\
\text { Also, } g(4)=5-2 \sqrt{6+\frac{1}{4}}=0, \\
g(3)=4+\frac{4}{3}-2 \sqrt{5+\frac{1}{3}} \\
=\frac{16}{3}-\frac{8}{\sqrt{3}}=\left(2-\frac{2}{\sqrt{3}}\right)^{2},
\end{array}
$$

Then $0 \leqslant g(t) \leqslant\left(2-\frac{2}{\sqrt{3}}\right)^{2}$
$$
\begin{array}{l}
\Rightarrow y^{2} \leqslant\left(2-\frac{2}{\sqrt{3}}\right)^{2} \\
\Rightarrow y \leqslant 2-\frac{2}{\sqrt{3}} \\
\Rightarrow\left|\sqrt{a+\frac{1}{b}}-\sqrt{b+\frac{1}{a}}\right| \leqslant 2-\frac{2}{\sqrt{3}} .
\end{array}
$$

The equality holds if and only if $a=1, b=3$ or $a=3, b=1$.