How does the immune system actually work?
Through two cooperating systems — one fast and general, one slow and specific — and the key insight is that they solve different problems and that the second one remembers.
Innate immunity: fast and general. Present from birth, responding within minutes to hours to anything recognisably foreign.
Physical and chemical barriers — skin, mucus, stomach acid, tears — which stop most things before anything else happens.
Pattern recognition. Innate cells detect molecular signatures common to whole classes of pathogen — bacterial cell wall components, viral genetic material in the wrong place — rather than specific organisms.
Phagocytes that engulf and destroy, inflammation that brings blood and cells to the site, fever, and the complement system of proteins that punctures and marks microbes.
Its limit: it does not improve with exposure. It responds identically the hundredth time.
Adaptive immunity: slow and specific. Takes days to mount a first response, and is precisely targeted.
B cells produce antibodies — proteins that bind one specific target, neutralising it and marking it for destruction.
T cells come in kinds: helper T cells coordinate the response; cytotoxic T cells kill the body's own infected cells, which is the only way to deal with a virus already inside a cell.
How specificity arises. Receptors are generated by randomly rearranging gene segments, producing an enormous diversity of cells each recognising something different, before any exposure. When one encounters its match, it multiplies. The repertoire is built at random and then selected by experience.
Memory is the crucial output. After an infection, long-lived memory cells persist, so a second encounter produces a response that is faster and stronger — frequently before symptoms appear. This is what vaccination exploits.
How it avoids attacking you. Cells reacting to your own tissues are largely eliminated during development. Autoimmune disease is this process failing, and allergy is the system responding vigorously to something harmless.
General information, not medical advice.