WHEN Jonathan Moreno’s company was looking for a location for a new factory in 2009 to make its medical devices, it ruled out much of the world. Europe and the Americas were too expensive, India was too complex and intellectual-property rights in China too patchy. In the end, Vietnam was the one candidate left standing. It still seemed risky as the country was just emerging as a destination for foreign investors. Seven years on, Mr Moreno surveys the scene—employees assemble delicate diagnostic probes in a room that resembles a laboratory—and has no doubt about where his company, Diversatek, will expand next. “To the back, there and there,” he says, pointing to either side.

It is far from alone. Foreign direct investment in Vietnam hit a record in 2015 and has surged again this year. Deals reached $11.3 billion in the first half of 2016, up by 105% from the same period last year, despite a sluggish global economy. Big free-trade agreements explain some of the appeal. But something deeper is happening. Like South Korea, Taiwan and China before it, Vietnam is piecing together the right mix of ingredients for rapid, sustained growth.

Vietnam already has a strong, often underappreciated, record. Since 1990 its growth has averaged nearly 6% a year per person, second only to China. That has lifted it from among the world’s poorest countries to middle-income status. If Vietnam can deliver 7% growth for another decade, its trajectory would be similar to those of China and the Asian tigers (see chart). But that is no sure thing. Should growth fall back to 4%, it would end up in the same underwhelming orbit as Thailand and Brazil.

Perhaps the biggest factor in Vietnam’s favour is geography. Its border with China, a military flashpoint in the past, is now a competitive advantage. No other country is closer to the manufacturing heartland of southern China, with connections by land and sea. As Chinese wages rise, that makes Vietnam the obvious substitute for firms moving to lower-cost production hubs, especially if they want to maintain links back to China’s well-stocked supply chains. A relatively young population adds to Vietnam’s appeal. Whereas China’s median age is 36, Vietnam’s is 30.7. Soon enough, it will start ageing more rapidly but its urban workforce has much scope to grow. Seven in ten Vietnamese live in the countryside, about the same as in India—and compared with only 44% in China. The reservoir of rural workers should help dampen wage pressures, giving Vietnam time to build labour-intensive industries, a necessity for a nation of nearly 100m people. Many other countries also boast young workforces. But few have had as effective policies as Vietnam. Since the early 1990s the government has been very open to international trade and investment. This has given foreign companies the confidence to build factories. Foreign investors are responsible for a quarter of annual capital spending. Trade accounts for roughly 150% of national output, more than any other country at its level of per-person GDP. Investors have also taken heart from the stability of Vietnam’s long-term planning. Like China, it has used five-year plans as rough blueprints for development. But also like China, its governance allows scope for innovation: its 63 provinces compete with each other to attract investors. A model of developing industrial parks with foreign money and managers began in Ho Chi Minh City in 1991 and has since been replicated elsewhere.

And Vietnam’s workforce is not just young but skilled. Public spending on education is about 6.3% of GDP, two percentage points more than the average for low- and middle-income countries. Although some governments spend even more, Vietnam’s expenditures have been well focused, aiming to boost enrolment levels and ensure minimum standards. In global rankings, 15-year-olds in Vietnam beat those in America and Britain in maths and science. That pays dividends in its factories. At Saitex, a high-end denim manufacturer, workers must handle complex machinery—from lasers to nanobubble washers—all to produce the worn jeans so popular in the West.

On top of this solid foundation, Vietnam is reaping benefits from trade deals. It is set to be the biggest beneficiary of the Trans-Pacific Partnership (TPP), a 12-country deal that includes America and Japan. With American politics turning hostile to trade, there is a risk that the TPP will fail. But even if that happens, Vietnam will do well. The TPP has already helped to advertise its capabilities. And there are other major agreements: a free-trade pact with the EU is in the works, and one with South Korea went into force in December.

Yet Vietnam also faces a series of challenges, any of which could impede its rise. Speculative excesses in the past helped fuel a property bubble. It burst in 2011, saddling banks with bad debts. Vietnam created a “bad bank” to house the failed loans and has started cleaning up its banks. However, it has been slow to inject new capital into its banks and hesitant about modernising their operations.

In one crucial area it compares poorly with China: getting the most out of the private sector. Private Chinese companies generate about 1.7 yuan of revenue per yuan of assets, more than double the 0.7 ratio for state-owned enterprises (SOEs). In Vietnam private-sector productivity has slumped over the past decade to the 0.7 level, the same as SOEs, says the World Bank. One reason is that large groups in Vietnam sprawl across 6.4 separate industries on average; those in China operate in just 2.3, according to the OECD.

Furthermore, although Vietnam has benefited from foreign investment, only 36% of its firms are integrated into export industries, compared with nearly 60% in Malaysia and Thailand, according to the Asian Development Bank (ADB). In some cases Vietnam has gone too high-end. Much has been made of Samsung’s plans to invest $3 billion in mobile-phone production in Vietnam, but domestic suppliers provide it with little except plastic wrapping. Vu Thanh Tu Anh, director of the Fulbright Economics Teaching Program in Ho Chi Minh City, says the government needs to help build up supply chains—for example, training companies in textile production to support the apparel sector.

There are grounds for cautious optimism. The Ministry of Planning and Investment teamed up with the World Bank to lay out a strategy for change earlier this year. Their joint report, “Vietnam 2035”, details how the country can make SOEs more commercial and reinvigorate the private sector. Weakened public finances—the fiscal deficit is set to be more than 6% of GDP for the fifth straight year in 2016—are putting pressure on the government. To bolster its revenues, it sold shares in more than 200 SOEs last year, the biggest annual tally ever. These were mostly small deals but in July it took a bolder step, scrapping a foreign-ownership limit (previously 49%) on Vinamilk, the country’s main dairy company. Investors are hopeful this will serve as a template for more such reforms.

After years of solid growth, Vietnam has nearly reached a milestone. Now it is classified as a middle-income country, it is about to lose access to preferential financing from development banks. In 2017 the World Bank will start to phase out concessional lending. For Vietnam it is a moment to reflect on how far it has come and also on the trickier path ahead. It has a chance to be Asia’s next great success story. It will take courage to get there.