Water heater tanks are typically coated with a ceramic coating to limit the amount of points that rust can occur inside the tank.
Where any defects exist in this coating and water can contact the steel, rust can occur. If it does occur it will quickly work it's way through the tank wall, causing a leak and effectively scrapping the water heater.
Water heater manufacturers don't want this to happen, at least during the warranty period, so they install a galvanic anode rod inside the water heater. That rod (typically magnesium) has a higher anodic potential than the steel tank. This means the corrosion that would normally occur in the tank occurs instead at the anode rod.
Basically the anode is using galvanic corrosion TO PROTECT your water heater tank by sacrificing itself to protect the tank. In fact, sometimes we call these anodes "sacrificial" anodes.
However, if your metallic plumbing is connected directly to the water heater (with no dielectric union or other insulator) your water heater anode is now trying to protect not only the inside of the tank but also the inside of all of any electrically continuous metallic house piping. Worst case this could be trying to protect the city's cast iron water main even. That's 1000 times more protection than the water heater's anode can accomplish....so failure of the tank will occur quite quickly.
Typically water heaters with a longer warranty include a LARGER anode rod that will take longer to corrode away completely.
About 2 years ago, I replaced the anode rod on my 5 year old power vent NG water heater. It was totally consumed. I'm going to replace it again in about a year and see if it lasted 3 years.
I'm pretty confident that if I can keep that anode rod in good shape, I can make that water heater tank last longer than I will.
A new anode rod was $29 from Amazon. I had to get one that was flexible because I don't have the headroom over the top of the water heater to install a solid one. If I did, I 'd get a solid one.
https://www.amazon.com/dp/B082VG7TVR?tag=atomicindus08-20