With inflation at a 40-year high and interest rates on the rise, many investors are feeling pessimistic about the near-term future of the economy. As a result, the growth-heavy Nasdaq Composite has fallen 17% from its high, and a number of economists believe the risk of a recession is rising.

What should you do? Famed investor Warren Buffett once offered this advice: "Be fearful when others are greedy and greedy when others are fearful." That doesn't mean you should pour all of your money into the market today. It simply means you should keep investing on a regular basis, even if the economy does indeed slip into a recession. After all, every past market downturn has been a buying opportunity.

With that in mind, here are two stocks to buy and hold through any recession.

Person reviewing financial papers and charts on a tablet.

Image source: Getty Images.

1. Roku: Reshaping the entertainment industry

Roku (ROKU 0.15%) has become a powerhouse in the entertainment industry. It ranks as the most popular streaming platform in the U.S., Canada, and Mexico by viewing time, and it powered 31.8% of all streaming hours worldwide in the fourth quarter. The next closest competitor was Amazon Fire TV, which accounted for just 16.5% of streaming hours.

One factor behind Roku's success is Roku OS, the industry's only operating system purpose-built for connected TVs (CTVs). Whereas rivals like Amazon have taken mobile operating systems and adapted them to TV, Roku designed a new system from the ground up, creating a better experience for viewers.

The company's ad-supported streaming service, The Roku Channel, is quickly becoming another source of differentiation. Roku expanded its lineup of linear channels last year, and it debuted more than 50 original titles, including its first feature-length film. Thanks to those efforts, streaming hours on The Roku Channel more than doubled in 2021, and the service ranked among the top five channels on the platform in the U.S. in the third and fourth quarters.

That helped drive another strong financial performance. Roku grew its user base by 17% to 60.1 million active accounts in 2021, and total streaming hours rose 25% to 73.2 billion. Not surprisingly, that uptick in engagement brought more marketing dollars to the platform, as monetized video ad impressions nearly doubled. In turn, revenue jumped 55% to $2.8 billion and free cash flow skyrocketed 186% to $188 million.

Going forward, Roku is well-positioned to maintain that momentum. While many viewers have cut the cord, linear TV ad spend was nearly fivefold greater than CTV ad spend in the U.S. last year, according to eMarketer. But ad budgets should continue to shift in the years ahead. In fact, BMO Capital Markets believes CTV ad spend will hit $100 billion by 2030, and while a recession may delay that trend, it wouldn't change the long-term trajectory of the industry. That's why this stock is a smart buy.

2. Paycom Software: Simplifying human capital management 

Paycom (PAYC -0.10%) specializes in human capital management (HCM) software. Its platform is built around payroll, but it also includes applications for talent acquisition, time and attendance, and training, as well as tools for human resources tasks like analytics and benefits administration. In short, Paycom provides everything a business needs to manage the employee lifecycle from beginning to end. That broad utility gives Paycom an edge.

Many businesses rely on multiple point solutions to meet their HCM needs, which makes things complicated for administrators. But Paycom built its HCM suite on a single system of record, eliminating the need to maintain employee data across multiple databases. Its software also leans on self-service functionality to further simplify workflow for clients. That value proposition has translated into strong financial results.

The company grew its customer base by 9% to 33,875 in 2021. In turn, revenue climbed 25% to $1.1 billion and earnings surged 37% to $3.37 per diluted share. More importantly, shareholders have good reason to believe Paycom can maintain that momentum.

According to Fortune Business Insights, HCM spend totaled $24 billion last year. That means Paycom has captured about 5% of its addressable market. To capitalize on that opportunity, the company is expanding its sales force and enhancing its HCM suite. Last year, Paycom debuted the industry's first self-service payroll application, and management noted "very strong adoption" in the latest earnings call. It also opened five new sales offices in the last few months.

Here's the bottom line: Paycom may not be the trendiest tech company, but it has a sizable market opportunity and its HCM suite creates real value for clients, as evidenced by its 94% retention rate last year. More importantly, no recession will make the HCM industry any less important in the long run. That's why this growth stock is a smart investment.