Why do we develop lifelong immunity to some diseases, but not others?

Childhood vaccines can protect against some diseases for a lifetime. Some diseases, like the measles, infect us once and usually grant us immunity for life. For others, like the flu, we have to get vaccinated year after year. So why…