Most of us struggle to think well after a poor night’s sleep – feeling foggy and failing to perform at our usual standard at school, university or work. You may notice that you’re not concentrating as well, or that your memory doesn’t seem up to scratch. Decades of bad sleep, however, may potentially lead to cognitive decline.Bad sleep also affects people’s mood and behaviour, whether they are young infants or older adults. So how much sleep does our brain need to operate properly in the long term? Our new research study, published in Nature Aging, provides an answer.Sleep is an important component of maintaining normal brain functioning. The brain reorganises and recharges itself during sleep. As well as removing toxic waste byproducts and boosting our immune system, sleep is also key for “memory consolidation”, during which new memory segments based on our experiences are transferred into long-term memory.An optimal quantity and quality of sleep enables us to have more energy and better wellbeing. It also allows us to develop our creativity and thinking.When looking at babies three to 12 months of age, researchers have noted that better sleep is associated with better behavioural outcomes in the first year of life, such as being able to adapt to new situations or regulating emotions efficiently.These are important early building blocks for cognition, including “cognitive flexibility” (our ability to shift perspective easily), and are linked to wellbeing in later life.Sleep regularity seems to be linked to the brain’s “default mode network” (DMN), which involves regions that are active when we are awake but not engaged in a specific task, such as resting while our mind wanders. This network includes areas that are important for cognitive function, such as the posterior cingulate cortex (which deactivates during cognitive tasks), parietal lobes (which process sensory information) and the frontal cortex (involved in planning and complex cognition).There are signs that, in adolescents and young adults, poor sleep may be associated with changes in connectivity within this network. This is important as our brains are still in development into late adolescence and early young adulthood.Disruption in this network may therefore have knock-on effects on cognition, such as interfering with concentration and memory-based processing, as well as more advanced cognitive processing.Alterations in sleep patterns, including difficulty falling and staying asleep, are significant characteristics of the ageing process. These sleep disturbances are highly plausible candidate contributors to cognitive decline and psychiatric disorders in older people.Our study aimed to better understand the link between sleep, cognition, and wellbeing. We found that both insufficient and excessive sleep contributed to impaired cognitive performance of a middle-aged to old population of nearly 500,000 adults from the UK BioBank. However, we did not study children and adolescents, and since their brains are in development, they may have a different requirement for optimal sleep duration.Our key finding was that seven hours of sleep per night was optimal, with more or less than that bringing fewer benefits for cognition and mental health. In fact, we found that people who slept that amount performed – on average – better on cognitive tests (including on processing speed, visual attention and memory) than those who slept less or more. Individuals also need seven hours of sleep consistently, without too much fluctuation in duration.That said, we all respond slightly differently to a lack of sleep. We discovered that the relationship between sleep duration, cognition and mental health was mediated by genetics and brain structure. We noted that the brain regions that are the most affected by sleep deprivation include the hippocampus, well known for it’s role in learning and memory, and areas of the frontal cortex, involved in top-down control of emotion.But although sleep may affect our brains, it could also work the other way around. It might be that age-related shrinkage of brain regions involved in the regulation of sleep and wakefulness contribute to sleep problems in later life. It may, for example, decrease the production and secretion of melatonin, a hormone that helps control the sleep cycle, in older adults. This finding seems to support other evidence suggesting there is a link between sleep duration and the risk of developing Alzheimer’s disease and dementia.While seven hours of sleep is optimal for protecting against dementia, our study suggests that getting enough sleep can also help alleviate the symptoms of dementia by protecting memory. This highlights the importance of monitoring sleep duration in older patients with psychiatric disorders and dementia in order to improve their cognitive functioning, mental health and wellbeing.So what can we do to improve our sleep for optimal cognition and wellbeing in our daily lives? A good start is ensuring that the temperature and ventilation in your bedroom is good – it should be cool and airy. You should also avoid too much alcohol and watching thrillers or other exciting content before going to bed. Ideally, you should be in a calm and relaxed state when you are trying to fall asleep. Thinking about something pleasant and relaxing, such as the last time you were on the beach, works for many people.Technological solutions such as apps or wearable devices can also be beneficial for mental health as well as for tracking sleep and ensuring consistency of sleep duration.To enjoy life and to function optimally in everyday life, you may therefore want to monitor your own sleep patterns to ensure that you are getting seven hours of sleep on a regular basis.