Jumping just as you're attacked allows you to take little to no horizontal knockback
While sprinting forward, when you press your jump key at the same time when you get attacked, you will take reduced/no horizontal knockback depending on how accurately you time it. This works on attacks from mobs and players, in both singleplayer and multiplayer Minecraft.