Discount retailer Dollar General (DG -0.36%) is what some might call a boring business. However, the stock's returns have been anything but. Shares have outrun the S&P 500 by a nearly 3-to-1 ratio since the company went public in late 2009.

Not only could this continue, but Dollar General could be one of the best stocks to hold in your portfolio during a recession, something 6-in-10 economists believe could happen in the next year.

What's the company's secret recipe for success? It turns out that the company's Dollar General brand is quite misleading when you look at the company's financials. Here is what you need to know.

Who knew selling cheap goods could be so profitable?

Dollar General sells low-cost items. Its name might imply that it's an actual dollar store where items cost a dollar or less, but that's not true. The company sells various goods, including groceries, clothing, household items, toys, and electronics. You won't find the latest high-definition television at Dollar General, but you'll find lower-tier brands of simple electronics like Bluetooth speakers or headphones.

Chart showing Dollar General's operating margin beating Walmart's since 2014.

DG Operating Margin (TTM) data by YCharts

But don't assume that cheap merchandise can't be profitable. Dollar General has a robust operating margin for a discount retailer. Above, you'll see that the company is more than twice as profitable as Walmart on an operating margin basis. Dollar General also generates cash flow better than Walmart; it turns roughly $0.05 of every sales dollar into free cash flow versus $0.02 for Walmart.

Does this mean Dollar General is managed better than Walmart, arguably the world's most ruthless retailer? Not necessarily. It turns out that Dollar General and Walmart have some fundamental differences.

Strategic expansion into its target market

Walmart famously boasts that roughly 90% of Americans live within 10 miles of a Walmart store, but that can be misleading. Studies estimate that approximately 83% of the U.S. population lives in urban areas. In other words, Walmart is in every major population center, not every town in America.

Dollar General targets rural towns with insufficient traffic to support a Walmart or other large retailers. Dollar General stores are typically smaller, and the low-cost items they sell appeal to lower-income populations. People need household necessities, even during recessions. You can see below that the company's growth didn't miss a beat during the Great Recession, the worst economic downturn in recent memory.

Chart showing rise in Dollar General's revenue and its annual YoY growth since 2008.

DG Revenue (TTM) data by YCharts

There's still a lot of room for expansion throughout rural areas, and Dollar General remains aggressive in tapping that opportunity. Its companywide store square footage grew 6.3% in 2022 over the prior year, and there are more than 3,000 planned real estate projects this year, including 1,050 new stores and 2,000 remodels.

Should investors buy shares today?

Shares are trading right at their long-term average valuation, a price-to-earnings ratio (P/E) of 20. The great thing about that is that you don't need the valuation to change to get solid investment returns; analysts believe earnings per share (EPS) will grow by 11% annually over the next three to five years. That growth and the company's dividend offer potential 12% annual returns.

Of course, the broader market could take shares to a lower valuation -- Wall Street can do silly things. But investors can feel confident that Dollar General's business will hold its own in a recession, and its expansion plans also paint a pretty clear growth path.

Chart showing Dollar General's PE ratio falling since mid-2022.

DG PE Ratio data by YCharts

Long-term investors can buy shares of a quality business at a fair price today; any potential drop in shares could be a gift. Dollar General's playbook of expanding in rural markets has proven itself over the years, and there's no indication it won't continue working.