Companies have lousy internal pricing systems that make turnover seem a lot cheaper than it is.
TL;DR: Raises are very obvious in terms of higher costs. Changes in productivity can often be completely missed or just assigned no value at all.
The 5-year tenured engineer has deep knowledge of the internal systems and practices and is more capable of teaching his knowledge to his colleagues.
The challenge with this is that its value can only be measured in terms of productivity and the value of productivity needs to be measured in dollars. Companies don't even try to measure productivity except with agile points and those usually don't incorporate most of the work involved in development. They also rarely attach productivity to dollars.
Consider something a friend's co-worker had to do these past two weeks. He had to go through and bold all sorts of things and remove various colons from others and change the radius of various borders. That's $3000 of work. If it were put in those terms to the product owner, I doubt it would have been done. But nowhere in the project is anything costed because it is all internal. Internal projects are interesting because price is no longer even considered, so anything gets ordered.
I work on an internal project where the pace is slowing, perhaps by 1/2 in the past few months with no clear path back to normal. If the price of a product doubled, alarm bells would be going off everywhere. Our slowness hasn't been discussed. The price has effectively doubled, but because nobody has put it into dollars, nobody really thinks of it.
Also, the co-worker is a solid mostly backend engineer with many years experience. That task really should have gone to my friend as he is a junior, mostly Angular, engineer. Instead he is implementing an SQL database.
It goes the other way as well. How many complaints do we hear about on here because a company won't spend $50 on a development tool or an issue tracker? For a cheap junior like myself, it needs to save an hour and a half of my time to make sense. For more senior devs, that can be well under an hour. But even if it would give you an extra week a year, in many companies it is difficult to get that tool.
It is more expensive to replace an experienced engineer than it is to retain one.
This is true, but who's budget does it come out of? In my government agency, a retention raise is paid for by the department budget. Hiring costs come out of the HR budget, most in terms of their salaries. Sure, the department might have to pay for someone to sit down and interview these people, but that gets lost in general salary costs and productivity losses. Most companies don't even track what it costs to hire someone or how effective their hiring is.
Same with my friend at a big bank (as well as when I worked at another bank). Hiring is centralized and handled by HR up until the final interview, so much of the cost of high turnover is not carried by the department with it. That manager can also blame "the job market" for not easily letting him fill the post. Because that reason is highly accepted, it makes it cheap for the manager to use.
Most companies have these unaccounted for externalities and ways for departments to pass costs off to other departments without it being traced. They aren't deliberately thinking in this way, but on their departmental balance sheet, a raise costs money and a replacement doesn't.
New engineers can take years to come up to the experience level of a current tenured engineer.
See point 1. Also, a lot of the time, nobody is considering experience beyond the point where they hire the employee. This is evident on my project. We never really consider who actually knows what when assigning work. We had this expensive Java consultant for a while who kept being asked to do React work. It means I get to learn a heck of a lot by doing tons of different things, but it does hurt my productivity.
You may get lucky and hire someone who is very capable, or you may be unlucky and hire the candidate who is a drain on resources and just happened to research similar topics to your interview questions and/or has a likeable personality.
Can anyone but their technical co-workers point out who that is anyway? I keep seeing management get this absurdly wrong simply because they can't really assess software quality beyond the front-end and end-user usage.
There must be some pervasive in-built market incentive going on here that causes this dynamic to take place year after year, company after company. Why do companies give higher raises to engineers that switch instead of engineers that stay?
The incentive is that they cannot price it effectively without a lot of effort so they often do not price it at all. The metrics used are the metrics that are easy to get like net budget, revenue/salary ratio, and how low expenses are kept. None of those value experience.
If you spent $140,000 a year on a project and it took 18 months or spent $170,000 a year on a project and it took a year, plenty of companies would see the former as cheaper simply because they would never do the multiplication of this calculation or even consider time as a factor in cost.