I have read that a vaccine against a pathogen typically works by using a dead or weakened version of that pathogen and then inciting an immune response against the pathogen so that the immune system then recognizes that pathogen in the future and attacks it immediately.
However, suppose that a person gets vaccinated against a virus. Why couldn't the virus infect a few cells before it runs into immune system cells and starts replicating as normal? Does the immune system somehow recognize virus-infected cells?
Also, how would a vaccine against a virus that attacks immune system cells work (like HIV, for instance)? In this scenario, wouldn't any immune cell that tries to destroy the virus get infected itself?