Retiring during a market downturn and soaring inflation can feel like sailing into the wind instead of the sunset.The market’s performance in the first few years of retirement can have a big impact on how long a nest egg lasts, partly because losses take a bigger bite out of a portfolio when it is typically at its largest, advisers and economists say.Of course, it isn’t always possible to time your retirement to coincide with a bull market.But those nearing retirement right now can take some comfort in research that shows that even people who retired in the worst time to do so since 1926 would have made their money last 30 years by sticking to certain rules. As the stories of the four retirees The Wall Street Journal profiled this week show, even those who retired in 2008 have done fine provided they managed their money well.Negative returns at the start of retirement, when a portfolio is usually largest, create a problem because the combination of market losses and withdrawals can leave a portfolio too depleted to last decades.“The five years after retirement are a pivotal period for determining a sustainable lifestyle in retirement,” said Wade Pfau, a professor at the American College of Financial Services in King of Prussia, Pa., and author of “Retirement Planning Guidebook.”Consider a 62-year-old who retired on Jan. 1 with $1 million and is following the 4% rule to determine how much to spend in retirement. (Such an approach, which has been questioned recently, calls for spending 4% of a balance in the first year of retirement and adjusting that amount in subsequent years to account for inflation.)After taking the first annual withdrawal of 4%, or $40,000, the investor would have $960,000 left. With a 15% loss in the first year, the balance would fall to $816,000. Two more years of similar withdrawals and 15% losses would leave about $527,000 to last potentially for decades.By contrast, a 62-year-old who retires with $1 million and experiences 15% annual gains would have about $1.36 million after three years of $40,000 withdrawals.Despite the market’s importance in early retirement, history shows that the portfolios of people who retire in down markets can recover.Thanks to the long bull market and low inflation that followed the financial crisis of 2008, someone with 50% in stocks who retired with $1 million on Jan. 1, 2007, and spent $40,000, adjusted annually for inflation, would have had about $874,000 left after two years, but would have about $1.63 million today.“As long as you didn’t panic and sell your stocks in 2008 you’d be doing fine today,” said Mr. Pfau, who crunched the numbers for a portfolio with 50% in U.S. large-cap stocks and 50% in intermediate-term U.S. government bonds.Another lesson for retirees contending with losses is to cut spending if possible, since “if you’re overspending from a portfolio that is simultaneously dwindling, that just leaves less in place to repair itself when the markets eventually recover,” said Christine Benz, director of personal finance at Morningstar Inc.The worst 30-year period in which to retire began in the late 1960s. Those who retired then were clobbered with back-to-back bear markets that started around 1969 and 1973, plus years of high inflation. These factors caused many to drain their nest eggs faster than they would have otherwise, although many in that era were able to fall back to some extent on traditional pension benefits.If markets slide and inflation remains high for the next couple of years, as some economists have predicted, Mr. Pfau said it could create “the perfect storm,” leaving investors with a choice between withdrawing more from a shrinking portfolio or cutting spending to try to protect their nest eggs even as prices rise.Here are steps retirees can take to improve their odds of making their money last:Cut spending when markets declineThe 4% rule would have protected retirees from running out of money even in the worst 30 year period since 1926 in which to retire, which turned out to be from 1966 to 1995, according to Mr. Pfau.For current retirees, Mr. Pfau recommends forgoing inflation adjustments following any year in which your portfolio incurs losses.“A very small change in spending can have a dramatic effect,” said Mr. Pfau.For example, someone who retired in 1966 and stuck to the 4% rule would have run out of money after 30 years. But by spending 3.8% to start instead, the investor would have preserved most of his or her original nest egg by year 30, he said.Manage volatilityPeople entering retirement often have 40% to 60% or more in stocks to help their nest eggs grow.A 2014 study by researchers including Mr. Pfau finds that those who start retirement by reducing their stockholdings to 20% to 30% of their portfolio and then gradually push it back up to 50% to 70% in stocks have the highest probability of making their money last 30 years using the 4% spending rule.Those who take a different approach, tapering stockholdings from 60% to 30%, are likely to run out of money after 28 years in the worst-case scenarios, according to the research.That said, the conventional approach of starting retirement with more in stocks and reducing that exposure over time comes out ahead if stocks fare well in the early years of retirement. But reducing stock market exposure up front provides better downside protection in those early years, when retirees are most vulnerable to financial losses, says Mr. Pfau.Use other assetsWhen markets decline, rather than sell stocks at a loss, retirees with whole life insurance may be able to withdraw from their policies to meet living expenses. Another option is to tap home equity with a reverse mortgage line-of-credit. There can be downsides, including high fees on reverse mortgages, so weigh the pros and cons carefully, Mr. Pfau said.