The Pacific oyster Crassostrea gigas lives in microbe-rich marine coastal systems subjected to rapid environmental changes. It harbours a diversified and fluctuating microbiota that cohabits with immune cells expressing a diversified immune gene repertoire. In the early stages of oyster development, just after fertilization, the microbiota plays a key role in educating the immune system. Exposure to a rich microbial environment at the larval stage leads to an increase in immune competence throughout the life of the oyster, conferring a better protection against pathogenic infections at later juvenile/adult stages. This beneficial effect, which is intergenerational, is associated with epigenetic remodelling. At juvenile stages, the educated immune system participates in the control of the homeostasis. In particular, the microbiota is fine-tuned by oyster antimicrobial peptides acting through specific and synergistic effects. However, this balance is fragile, as illustrated by the Pacific Oyster Mortality Syndrome, a disease causing mass mortalities in oysters worldwide. In this disease, the weakening of oyster immune defences by OsHV-1 µVar virus induces a dysbiosis leading to fatal sepsis. This review illustrates the continuous interaction between the highly diversified oyster immune system and its dynamic microbiota throughout its life, and the importance of this cross-talk for oyster health. This article is part of the theme issue 'Sculpting the microbiome: how host factors determine and respond to microbial colonization'.