Buying stocks during a recession can be the most emotionally difficult thing to do in investing. Unless you time the market perfectly -- which is highly unlikely -- the companies you buy will continue to sink, and watching the value of your portfolio shrink is painful. 

So why would you buy stocks during a recession? If history is any indicator, it can also be one of the best times to be putting money in the market. For example, if you bought the tech-heavy Nasdaq Composite index in April 2009, near the depths of the Great Recession, and held it until April 7, 2022, your investment would have grown 774%. This is equivalent to an average annual return of 18%, which is almost double what the market traditionally offers.

For long-term investors, then, investing when stocks are struggling can be one of the smartest decisions you make. Datadog (DDOG -1.43%) and Zscaler (ZS -1.49%) are high on my watch list in such a situation. If these stocks saw steep declines in a weak economy, they would still have the potential to trounce the market long term because of their dominance, competitive advantages, and the tailwinds pushing them forward.

Person looking at a phone with a digital face looking back at them.

Image source: Getty Images.

1. Datadog

More companies are becoming cloud native, meaning they are expanding their use of tech-based applications and infrastructure. This is where Datadog comes in by providing services for businesses to ensure all of their cloud applications are running smoothly, and their network is operational and secure. The company acts as an eye in the sky for a company's cloud-based operations, and it is one of the leading platforms according to Gartner's Magic Quadrant research.

One of its main advantages over competitors is switching costs: The company has products for nearly everything a business may need to oversee its cloud, from network performance monitoring to user monitoring to scanning data logs for sensitive information. This creates a thriving ecosystem where once a customer gets integrated into multiple services, it is incredibly difficult to shift away. This might be why Datadog's churn is in the mid to low single digits.

One of the most impressive things about Datadog is its ability to expand its relationship with existing customers. Its net retention rate in the fourth quarter remained above 130% for the 18th consecutive quarter. This means customers from Q4 2020 are now spending 30% more on average as of the most recent report, thanks mostly to their adoption of more products. And 33% of customers used four or more features at the end of last year, up from 22% in the year-ago period. A similar trend can be seen in the number of users with six or more platforms, which grew from 3% to 10% over the same period. All of this signals that Datadog is loved by its customers, and as they become more integrated into the Datadog ecosystem, it will be harder to leave.

Many fast-growing tech companies are unprofitable, but Datadog stands out: It is teetering on the edge of profitability. The company lost $21 million in 2021, which decreased year over year and now represents just 2% of revenue. Datadog is not perfect, however. It trades at 40 times sales -- a very high valuation. That being said, its leadership and strengthening competitive advantages are so impressive they might be worth paying up for. If a recession bumps the share price down, Datadog would be a screaming buy.

2. Zscaler

Zscaler is in a similar situation as Datadog: It is a leader with a strong advantage over the competition and commands a high valuation of 37 times sales. But Zscaler specializes in "zero-trust" cybersecurity, which means the company acts as a bouncer for a business's cloud, data, and applications. Whenever an employee tries to access these parts of the business, Zscaler makes sure they actually are who they say they are.

This seems like a hyperfocused segment of the cybersecurity market as a whole, but it still leaves Zscaler with plenty of growth potential. The company's serviceable market is worth $72 billion, and considering the company reported just $860 million in revenue in the trailing 12 months, the opportunity is still tremendous.

The company has attracted over 5,600 customers, 1,480 of which spend over $100,000 annually. While the main risk is competition, the company's leadership position should deliver continued success. After all, no business wants to skimp on security as sophisticated cyberattacks make headlines on a frequent basis. As the world becomes more digital, there will be more of these threats, meaning demand for Zscaler's services is only going to rise in the future. This is why I would buy more Zscaler and hold it long term if a recession knocked the stock down to more attractive levels.