Tech stocks have fallen out of favor in the past 18 months or so as macroeconomic headwinds caused investors to become more risk-averse. But big valuation pullbacks across the sector also presented opportunities to build positions in top companies at prices that leave room for long-term returns that could elevate the overall performance of your portfolio. 

The potential for continued market volatility and economic challenges in the near term means that investors should take a judicious approach to buying tech stocks. With that in mind, read on for a look at two top technology stocks that are worth buying right now and holding on to forever.  

A person looking at data on a display.

Image source: Getty Images.

1. Microsoft

It's almost hard to believe now, but there was a time not too long ago when Microsoft's (MSFT 1.65%) business was in a rut. The tech giant had largely missed out on the mobile computing revolution, and the evolution of smartphone and tablet platforms and new web technologies saw rivals including Apple and Alphabet rapidly making gains in markets that had traditionally been Microsoft strongholds like operating systems, browsers, and software. It looked like one of the prime originators of the computing revolution was rapidly losing relevance. 

But things started to shift when Satya Nadella, the company's former executive vice president of Microsoft's cloud and enterprise group, become the company's CEO in 2014. Nadella led a shift that saw Microsoft push into cloud infrastructure services and transition key software products to subscription-based models that proved to be much more profitable. 

Thanks to these moves, Microsoft has been serving up tremendous net income and free cash flow generation, and it could be in the early stages of its next big growth phase. Through internal initiatives and investment in OpenAI and its ChatGPT project, the company has established a leading position in the artificial intelligence (AI) space

With strong service offerings in categories including cloud services, productivity software, and operating systems, Microsoft has a huge well of data to draw from for AI and machine learning and existing products that stand to benefit from the integration of these technologies. Nadella says that AI is the biggest thing to happen at the company since he's been CEO.

While Microsoft stock has regained some ground early in 2023's trading, the company's share price is still down roughly 27% from its high. Its positions in crucial service categories and early leadership in the AI space position the tech giant for continued success in the software arena, and the stock offers big upside potential for long-term investors. 

2. Cloudflare

Cloudflare's (NET 3.77%) software for protection against distributed denial-of-service (DDoS) attacks plays a key role in keeping websites and applications online. The company's content delivery network (CDN) software also makes it possible for data to be sent and received more rapidly across the internet. Through these technologies and other web services, the secure platform specialist is playing an essential role in the modern communications space, and its software is in high demand.  

With 42% year-over-year sales growth bringing the company's revenue to $274.7 million in the fourth quarter, Cloudflare is now on track to generate over $1 billion in annualized revenue. As impressive as its rapid growth has been thus far, it's likely just getting started. With the company reporting $975.2 million in sales in 2022, the business captured less than 1% of its estimated $115 billion total addressable market. 

Five years from now, Cloudflare expects to have reached roughly $5 billion in annualized revenue, and there's a good chance that the company can meet that target and continue posting strong growth from there. Because of its network's scale, ease of use, high-level security, and performance, Cloudflare has powerful competitive advantages and stands as a clear leader in key service categories. 

NET PS Ratio (Forward) Chart

NET PS Ratio (Forward) data by YCharts

While Cloudflare continues to trade at a growth-dependent valuation, the company is now posting regular profits on a non-GAAP (adjusted) basis and has an incredible runway for growth over the long term. With category-leading service offerings and the shares trading down roughly 72% from their peak, I think that taking a buy-and-hold approach to Cloudflare stock at today's prices is a move that will work out very well for long-term investors.