(Reuters) - Amazon.com Inc AMZN.O forecast strong fall sales and posted a profit that was double Wall Street targets on Thursday thanks to the retailer's younger, higher-earning businesses, including cloud computing and advertising.

Shares rose more than 3 percent in after-hours trade. The report was a relief to investors in the U.S. technology sector, still reeling from a profit warning by Facebook Inc FB.O Wednesday that plunged its stock 19 percent.

Amazon’s report shows how the world’s largest online retailer has increasingly learned to compensate for the high costs of fast package delivery and video streaming by controlling expenses and building up higher-profit businesses. It was the first mover in the business of selling data storage and computing power in the cloud, a bet that continues to reap rewards and give it the leeway to invest in grand projects.

For instance, the company is working to ship food from Whole Foods Market stores across the United States, in an ambitious attempt to bring groceries into the age of online retail.

Amazon’s spending typically climbs in the summer quarter, pressuring profits as the company prepares for Christmas and the winter holidays, its peak sales period each year.

Yet the company said it expects an operating profit between $1.4 billion and $2.4 billion, up from $347 million a year earlier. The company also reported a second-quarter profit of $2.5 billion, its largest ever.

“A big contributor to the quarter and the last few quarters obviously has been strong growth in our highest profitability businesses and also advertising,” Brian Olsavsky, Amazon’s chief financial officer, said on a call with media. “We’ve seen a greater-than-expected efficiency in a lot of our spend in things like warehouses, data centers, marketing.”

The Seattle-based company cut hundreds of consumer jobs in its headquarters earlier this year, in a move that may have lowered costs and freed up resources for fast-growing areas like Amazon’s voice aide Alexa.

Wedbush Securities analyst Michael Pachter called the expansion in the company’s gross profit margin “remarkable,” citing impressive results from cloud sales and services to third-party merchants on Amazon.

“That drove the bulk of the earnings beat,” he said.

CFO Olsavsky noted that third-party sales were changing the profit equation for Amazon, too. The company for years was notorious for roller-coaster results.

FILE PHOTO: Amazon boxes are seen stacked for delivery in the Manhattan borough of New York City,U.S., January 29, 2016. REUTERS/Mike Segar/File Photo

More lucrative than sales of goods that Amazon owns, third-party transactions offer the company a commission that increases significantly when merchants choose to hand over fulfillment and advertising to Amazon, as many do. Half of all units sold through Amazon are from these sellers, and these sales keep growing.

ANGLING FOR ADVERTISERS

Highly profitable ad sales were a bright spot last quarter. The company said revenue from the category and some other items grew 132 percent to $2.2 billion. Analysts were expecting $2.1 billion, according to Thomson Reuters I/B/E/S.

The company is working to automate tasks for advertisers and to help media buyers measure the results, Olsavsky said.

Key to its allure has been that advertisers' placements result directly in sales, reaching customers on Amazon with an intent to shop. That contrasts with ads reaching users who are on industry leaders Facebook and Alphabet Inc's GOOGL.O Google for a range of purposes.

Amazon Web Services (AWS), the company’s must lucrative unit, saw its operating profit margin expand from a year earlier. Sales picked up speed from the year prior, too, rising 49 percent to $6.1 billion and beating the average estimate of $6 billion.

The unit’s success has helped make Amazon Chief Executive Jeff Bezos the richest person in the world.

“It’s really becoming the crown jewel of Bezos’ empire,” said GBH Insights analyst Daniel Ives. “They’ve invested significant dollars in building out the infrastructure, sales force and AWS partner ecosystem worldwide that I think now is starting to pay just massive dividends.”

While cloud rivals are gaining ground, AWS remains far in front with 31 percent of the fast-expanding market, versus 18 percent for Microsoft Corp MSFT.O and 8 percent for Google in the second quarter, research firm Canalys said Thursday. Amazon shares, up 55 percent this year versus 6 percent for the S&P 500 .SPX as of Thursday's close, trade at a premium, too. The stock's price-to-earnings ratio is more than 10 times that of Microsoft. Cloud and ad sales, along with a July event that Amazon created to drum up revenue during the summer shopping lulls, are helping the company overcome high costs in the third quarter.

During the event, called Prime Day, Amazon sold more than 100 million products and signed up more people to its Prime loyalty club than on any other previous day in its history.

Prime members spend above average on Amazon. The company said it now expects third-quarter sales of between $54 billion and $57.5 billion, up from $43.7 billion a year earlier.

Hiking the annual U.S. price of Prime 20 percent in the second quarter appeared to have few negative consequences: Amazon said subscription revenue increased 57 percent to $3.4 billion.

Total net sales for the second quarter rose 39 percent to $52.9 billion, missing the average analyst estimate of $53.4 billion.