Non-volatile resistive switching, also known as memristor1 effect, where an electric field switches the resistance states of a two-terminal device, has emerged as an important concept in the development of high-density information storage, computing and reconfigurable systems2,3,4,5,6,7,8,9. The past decade has witnessed substantial advances in non-volatile resistive switching materials such as metal oxides and solid electrolytes. It was long believed that leakage currents would prevent the observation of this phenomenon for nanometre-thin insulating layers. However, the recent discovery of non-volatile resistive switching in two-dimensional monolayers of transition metal dichalcogenide10,11 and hexagonal boron nitride12 sandwich structures (also known as atomristors) has refuted this belief and added a new materials dimension owing to the benefits of size scaling10,13. Here we elucidate the origin of the switching mechanism in atomic sheets using monolayer MoS2 as a model system. Atomistic imaging and spectroscopy reveal that metal substitution into a sulfur vacancy results in a non-volatile change in the resistance, which is corroborated by computational studies of defect structures and electronic states. These findings provide an atomistic understanding of non-volatile switching and open a new direction in precision defect engineering, down to a single defect, towards achieving the smallest memristor for applications in ultra-dense memory, neuromorphic computing and radio-frequency communication systems2,3,11.