Effective clearance of an infection requires that the immune system rapidly detects and neutralizes invading parasites while strictly avoiding self-antigens that would result in autoimmunity. The cellular machinery and complex signaling pathways that coordinate an effective immune response have generally been considered properties of the eukaryotic immune system. However, a surprisingly sophisticated adaptive immune system that relies on small RNAs for sequence-specific targeting of foreign nucleic acids was recently discovered in bacteria and archaea. Molecular vaccination in prokaryotes is achieved by integrating short fragments of foreign nucleic acids into a repetitive locus in the host chromosome known as a CRISPR (clustered regularly interspaced short palindromic repeat). Here we review the mechanisms of CRISPR-mediated immunity and discuss the ecological and evolutionary implications of these adaptive defense systems.