Anyone can choose good stocks when the market is booming, but you have to be more careful when the economy seems to be cooling. Some stocks, even ones associated with strong companies, fall precipitously along with the rest of the market when recessions hit because people have less buying power.

There is one tried-and-true way of investing in hard times: Find companies that thrive in recessions, ones that people turn to when money is tight, such as Dollar General (DG -1.36%) and Costco Wholesale (COST 0.84%). Both are solid defensive choices for investors in a downturn.

Dollar General keeps growing

Dollar General chief operating officer Jeffrey Owen was promoted to be the company's CEO last month when Todd Vasos stepped down after seven years. Dollar General's expansion has gone on unbridled by COVID-19, inflation, supply issues, and labor shortages that have baffled other companies.

The discount variety store company had 18,356 stores across 47 states as of April 29, making it the largest retailer in the United States in terms of locations. It plans to open 1,110 new stores this fiscal year, along with 1,750 store remodels and 120 store relocations. So far this year, Dollar General's stock is up more than 7%, and the company has a price-to-earnings ratio (P/E) of 25.54.

Because it has so many stores, Dollar General has strong pricing power with suppliers. It also has a loyal customer following, often because its stores serve areas that are underserved by retailers -- small towns, low-income neighborhoods, and places where the nearest grocery store may be 20 or more miles away.

The company has increased revenue every year since it had its initial public offering (IPO) in 2009. That's not to say the current climate hasn't had an adverse effect, as inflation has cut into its margins. In the first quarter, the company reported revenue of $8.8 billion, up 4.2% year over year, but earnings per share (EPS) were down 14.5% to $2.41.

Dollar General was already doing well, and the recession helps its business. It has had five consecutive years of double-digit stock returns and has increased revenue 113.6% over the past 10 years. The company has had increased annual revenue every year since 1987, including from December 2007 to June 2009, which is considered the last recession.

Dollar General also has a dividend, which it has raised for five consecutive years by a total of 112%. It just raised its quarterly dividend to $0.55 per share, giving it a yield of 0.87%. That's not especially generous, but it is safe with a cash dividend payout ratio of 27.15%.

The company, in its guidance, said it expects another year of double-digit revenue growth (between 10% and 10.5%) and EPS growth between 12% and 14%.

Costco: Helping stretch shoppers' and investors' dollars

At first glance, Costco appears to be a pricey stock, with a price-to-earnings ratio of 42.69. There's a good reason for that -- the company has been so dependable that investors flock to in when a downturn is looming. Its shares have fallen slightly more than 3% so far this year but are up slightly the past three months.

Costco is known for its jumbo-sized toilet paper packages and its $1.50 hot dog-soft drink combos (which are the same price it was in 1984), the type of items budget-minded customers appreciate. The company, which operates 830 warehouses, including 574 in the United States, is made for recessions, but also has great buying power for the less-budget-minded (it sells more wine than any other merchant in the world). Over the past 10 years, its revenue has increased by 97.63%, and its EPS has grown by 189.7%.

COST Revenue (Annual) Chart

COST Revenue (Annual) data by YCharts

Costo is a members-only discount warehouse that has grown revenue for the past 12 consecutive years. When the last recession hit in 2007, the company went from being the fifth-biggest retailer in the U.S. in terms of revenue to No. 3, behind only Walmart and Amazon, and it has stayed there. The company makes money from its products and from its yearly memberships, which gives it a steady cash flow.

This year, through July 3, the company reported that revenue is up by 15.3% compared to the same 44-week period in 2021. In the most recently reported quarter, the third quarter, the company reported revenue of $51.61 billion, up 16.3% year over year, and EPS of $3.04, compared to $2.75 in the third quarter of 2021.

In May, the company increased its quarterly dividend from $0.79 to $0.90 a share, the 19th consecutive year it has increased its dividend. The yield is 0.66% and the cash dividend payout ratio is 34.12%, very safe. The company also rewards with investors with special dividends when it is having a strong year. In 2020, it gave investors a special dividend of $10 per share.

Like Dollar General, Costco has the type of scale that helps it with inflation because it has pricing power with suppliers. Another big advantage is the company already pays its workers strong wages, so it hasn't been hit as hard by labor shortages as other retailers.

Hedging your bet

The best thing about these two retail stocks is their steadiness. You're not going to see huge growth spurts, but both companies have consistently grown revenue, year after year, regardless of whether that year has a downturn. They're built to do well in recessions because they have products that can save people money, but even if the current downturn doesn't continue long, the companies will fare well anyway.