A device that counts and records the number of events experienced by an individual cell could have many uses in experimental biology and biotechnology. Here, we report a DNA-based 'latch' that switches between two states upon each exposure to a repeated stimulus. The key component of the latch is a DNA segment whose orientation is inverted by the actions of ϕC31 integrase and its recombination directionality factor (RDF). Integrase expression is regulated by an external input, while RDF expression is controlled by the state of the latch, such that the orientation of the invertible segment switches efficiently each time the device receives an input pulse. Recombination occurs over a time scale of minutes after initiation of integrase expression. The latch requires a delay circuit, implemented with a transcriptional repressor expressed in only one state, to ensure that each input pulse results in only one inversion of the DNA segment. Development and optimization of the latch in living cells was driven by mathematical modelling of the recombination reactions and gene expression regulated by the switch. We discuss how N latches built with orthogonal site-specific recombination systems could be chained together to form a binary ripple counter that could count to 2N - 1.