Epitaxial Fe3O4 thin films grown on single crystal MgO(001) present a well-defined model system to study fundamental multivalent ion diffusion and associated phase transition processes in transition metal oxide-based cathodes. In this work, we show at an atomic scale Mg2+ diffusion pathways, kinetics, and reaction products at the Fe3O4/MgO heterostructures under different oxygen partial pressures with the same thermal annealing condition. Combining microscopic, optical and spectroscopic techniques, we demonstrate that an oxygen rich environment promotes facile Mg2+ incorporation into the Fe2+ sites, leading to the formation of Mg1-xFe2+xO4 spinel structure, where the corresponding portion of the Fe2+ ions are oxidized to Fe3+. Conversely, annealing in vacuum results in the formation of a thin interfacial rock-salt layer (Mg1-yFeyO), which serves as a blocking layer leading to significantly reduced Mg2+ diffusion to the bulk Fe3O4. The observed changes in transport and optical properties as a result of Mg diffusion are interpreted by the electronic structures determined by x-ray photoelectron spectroscopy and x-ray absorption spectroscopy. Our results reveal the critical role of available anions in governing cation diffusion in the spinel structures, and the need to prevent unwanted reaction intermediates (e.g., Mg1-yFeyO) for facile cation diffusion.