Saving for retirement isn't easy, especially as costs continue to rise. The average worker expects to need around $1.7 million to retire comfortably, according to a 2022 survey from Charles Schwab, and that's an intimidating number for most people.

It's especially difficult to invest when the stock market is rocky, and many workers have watched their retirement funds shrink over the past year. But there's one lesson from legendary investor Warren Buffett that can help you save more over time, while minimizing risk.

The stock market is safer than it seems

When you're saving for retirement, it can be tempting to invest only when the market is thriving. After all, when stock prices are sinking, it often feels like you're throwing money away.

However, one of the best ways to maximize your savings over time is to continue investing consistently -- even when the market is shaky.

Warren Buffett looking at the camera.

Image source: The Motley Fool.

Back in 2008, at the height of the Great Recession, Warren Buffett wrote an opinion piece for The New York Times to help reassure nervous investors. In it, he explained that, while it may seem counterintuitive, it's wise to stay in the market despite volatility. He said:

A simple rule dictates my buying. Be fearful when others are greedy, and be greedy when others are fearful. And most certainly, fear is now widespread, gripping even seasoned investors.

He went on to write:

Fears regarding the long-term prosperity of the nation's many sound companies make no sense. These businesses will indeed suffer earnings hiccups, as they always have. But most major companies will be setting new profit records 5, 10 and 20 years from now.

A long-term outlook is key right now

There's still a chance the market has further to fall, especially if we face a recession later this year. But the market's long-term potential is far more important than short-term volatility.

Historically, the market has earned positive average returns over time, despite facing significant ups and downs in the near term. In the past two decades alone, the market has experienced the dot-com bubble burst, the Great Recession, the crash in the early stages of the pandemic, and the current downturn. Despite everything, though, the S&P 500 is still up by nearly 200% since 2000.

^SPX Chart

^SPX data by YCharts.

The best way to take advantage of these gains is to invest consistently, regardless of what's happening in the market. Your portfolio may take a hit in the near term, but as Buffett accurately predicted back in 2008, most healthy companies will thrive over decades.

Keeping your retirement savings safe

Continuing to invest during market downturns can help you save more over time, but it's equally important to ensure your investing strategy aligns with your timeline. As you get closer to retirement, it's a good idea to double-check that your asset allocation is appropriate for your age.

Asset allocation refers to how your investments are divided within your portfolio. When you still have decades left to save, you can afford to invest more heavily in stocks. If your savings take a hit during a downturn, you have plenty of time for your investments to recover.

As you get older, however, you may want to shift your portfolio gradually toward bonds and other conservative investments. Bonds generally earn lower returns than stocks but are less impacted by market volatility. This is important when you're planning on needing your retirement savings in the near future.

Saving for retirement during periods of volatility can be challenging, but keeping a long-term outlook is key. By continuing to invest consistently and staying focused on the future, you can protect your retirement while maximizing your savings.