We can derive the Compton scattering formula using the 4-momentum. The 4-momentum before the collision is equal to the 4-momentum after the collision:
$$\tag{1}P_{b}=P_{a}\label{1}$$
The 4-momentum before the collision is given by $P_{\gamma}+P_{e}$ and the 4-momentum after the collision is given by $P'_{\gamma}+P'_{e}$. In general case, the 4-momentum components are:
$$\tag{2}p_{\mu}=\left(\frac{E}{c},\vec{p}\right)\label{2}$$
The dot product of 4-momentum with itself is:
$$\tag{3}p_{\mu}\cdot p^{\mu}=\frac{E^2}{c^2}-p^2=m^2c^2\label{3}$$
Because photons are massless, the dot product of their 4-momentum is zero.
We can rewrite equation (\ref{1}) in terms of photon and electron 4-momenta:
$$\tag{4}P_{\gamma}+P_{e}=P'_{\gamma}+P'_{e}\label{4}$$
Rearranging equation (\ref{4}) and squaring:
$$\tag{5}\left(P_{\gamma}+P_{e}-P'_{\gamma}\right)^2=\left(P'_{e}\right)^2\label{5}$$
Considering that the incoming photon is traveling along the $x$ axis, we can write its 4-momentum as $P_{\gamma}=\left(\frac{E_{\gamma}}{c},p_{\gamma},0,0\right)$. After the collision, the photon is scattered and will have a spatial momentum component in both $x$ and $y$ direction, so its 4-momentum can be written as $P'_{\gamma}=\left(\frac{E'_{\gamma}}{c},p'_{\gamma}\cdot \cos(\theta),p'_{\gamma}\cdot\sin(\theta),0\right)$. Also, considering the electron at rest before the collision, its 4-momentum is $P_{e}=\left(\frac{E_{e}}{c},0,0,0\right)$. Going back to the equation (\ref{5}) and using the 4-momentum expressions we obtain:
$$\tag{6}0+m^2c^2+0+2\cdot\frac{E_{\gamma}E_{e}}{c^2}-2\left(\frac{E_{\gamma}E'_{\gamma}}{c^2}-p_{\gamma}\cdot p_{\gamma}'\cdot\cos(\theta)\right)-2\cdot\frac{E'_{\gamma}E_{e}}{c^2}=m^2c^2\label{6}$$
$$\tag{7}2\cdot\frac{E_{\gamma}E_{e}}{c^2}-2\cdot\frac{E'_{\gamma}E_{e}}{c^2}=2\left(\frac{E'_{\gamma}E_{\gamma}}{c^2}-p_{\gamma}\cdot p_{\gamma}'\cdot\cos(\theta)\right)\label{7}$$
$$\tag{8}E_{e}\left(E_{\gamma}-E'_{\gamma}\right)=E_{\gamma}E'_{\gamma}-p_{\gamma}c\cdot p_{\gamma}'c\cdot\cos(\theta)\label{8}$$
$$\tag{9}mc^2\left(\frac{hc}{\lambda}-\frac{hc}{\lambda'}\right)=\frac{hc}{\lambda}\frac{hc}{\lambda'}\left(1-\cos(\theta)\right)\label{9}$$
$$\tag{10}\left(\lambda'-\lambda\right)\frac{hc}{\lambda\lambda'}mc^2=\frac{hc}{\lambda}\frac{hc}{\lambda'}\left(1-\cos(\theta)\right)\label{10}$$
So we get the Compton scattering formula:
$$\tag{11}\lambda'-\lambda=\frac{h}{mc}\left(1-\cos(\theta)\right)\label{11}$$