A recession can bring a lot of pain to many people. You may lose your job or fear that you will as the economy goes through a rough patch. While a recession on the near horizon isn't inevitable, some economists have become cautious, given the Federal Reserve's aggressive stance on fighting inflation.

Fortunately, you don't have to abandon stocks. These two companies do well even when the economy hits the skids. It's time to uncover their secret formulas.

People pointing to a piggy bank.

Image source: Getty Images.

1. Walmart

Walmart (WMT -0.65%) has proven that it can thrive in any market, especially when people grow more cautious about spending money. That's because the retailer, which opened its first discount store six decades ago, has perfected squeezing costs out of the business and passing these savings along to customers.

For instance, its fiscal 2021 adjusted revenue rose by 7.5%. This period, which ended on Jan. 31, 2021, encompassed the start of the pandemic and the challenges that entailed. Last year was strong, too. While revenue, excluding foreign currency translations, rose 1.6%, the figure was impacted by sold businesses since last year. Adjusted operating income grew by nearly 14%.

Walmart generates plenty of free cash flow, including $11.1 billion last year. This allowed it to handily pay $6.2 billion of dividends. The company has a proven ability to not only pay dividends throughout the economic cycle but increase them. Walmart has boosted payments annually since 1974. Most recently, the board of directors announced it would hike April's payment by a penny, to $0.56.

Already a Dividend Aristocrat, it's set to join the even more elite Dividend Kings in a couple of years when it reaches a half-century of higher dividends.

2. Procter & Gamble

Procter & Gamble (PG 0.86%) sells items people use in their everyday life, and they generally won't buy less if their personal financial circumstances worsen. These products include shampoo, deodorant, toothbrushes and toothpaste, laundry detergent, diapers, and paper towels. Better still, Procter & Gamble owns popular, well-established brands such as Head & Shoulders, Gillette, Luvs, and Pampers.

As a testament to the product strength, while many other companies struggled when the pandemic struck, Procter & Gamble's fiscal fourth-quarter 2021 (ended June 30) adjusted revenue increased by 4%. The company continued growing its top-and-bottom lines this year. In the fiscal third quarter (ended March 31), adjusted sales grew by 10% and earnings per share rose by 6%. Procter & Gamble was able to pass along some of its cost increase to customers, with higher prices responsible for 5 percentage points of the sales increase. Increased volume accounted for 3 percentage points and product mix added 2 percentage points.

In the first nine months of this fiscal year, covering the period that ended on March 31, Procter & Gamble generated free cash flow of $10.5 billion. This easily covered the $6.5 billion of dividends.

With the ability to generate steady free cash flow, the company has built an impressive dividend track record. In April, Procter & Gamble raised the quarterly payment by 5% to $0.91, marking 66 straight years of growing dividends.

Consider these recession-proof stocks

Walmart and Procter & Gamble have made a strong commitment to reward shareholders with higher dividends backed by strong free cash flow generation. This includes periods of recessions, stagflation, war, and a global pandemic. That's because their businesses -- low prices in the case of Walmart, and the popular, everyday necessities that Procter & Gamble sells -- lend themselves to consumers continuing to buy their merchandise.

When things aren't going well, it's nice to know that you can rely on these two to continue paying dividends.