Cities with fewer than 200,000 residents grew faster than larger metropolises between 2017 and 2018 as high housing prices chased many people away from big cities and their closest suburbs.

The biggest cities grew by a collective 326,000 people, less than half the number earlier in the decade, and less than the number for smaller cities – 421,000 for cities with populations between 10,000 and 50,000.

And small towns of fewer than 10,000 people grew more quickly than earlier in the decade, attracting more than 142,000 new people last year, according to a Stateline analysis of U.S. Census Bureau estimates, driven in part by retirees seeking affordable housing.

Larry Vollmer, a 37-year-old web development manager, felt lucky to find a home in 2017 in Vallejo, a city of about 122,000, despite a new two-hour commute to his job in San Francisco.

Tired of constant rent increases for small apartments in closer suburbs, he and his wife bought a $450,000 four-bedroom home with room for a family if they decide to have children. That price might have bought an apartment with less than half the space where they lived before, in Walnut Hills.

“It’s one of the last affordable places to commute to San Francisco. The house was on the market for two days and we were one of six people bidding on it,” said Vollmer, whose commute consists of a 10-minute drive to a ferry, 20 minutes in a line, an hour on the boat and then a half-hour walk to work.

It’s a particularly pressing problem in California, where well-paid tech workers have no trouble finding jobs but are hard-pressed to afford a home.

“Jobs need beds,” said Adam Fowler, research director at Beacon Economics, a Los Angeles consulting firm, who studied the gap between jobs and housing last year. “We’ve had an influx of well-paid information workers, but we haven’t built housing for them.”

California policymakers have been unable to overcome entrenched resistance to the construction of new apartment buildings. But housing shortages in booming cities is a national problem, said Lawrence Yun, chief economist at the National Association of Realtors.

Yun estimated that the nation needs another six million homes near the new jobs created in the past decade, and that the shortage will cause political turmoil in places where it is most acute, like San Francisco and New York.

San Francisco grew by about 4,100 people from 2017 to 2018, well down from a recent growth peak of more than 13,000 between 2011 and 2012.

New York, the nation’s largest city at about 8.4 million, lost almost 40,000 people, after gaining more than 82,000 earlier in the decade, between 2010 and 2011.

“There are repercussions,” said Yun, adding that fear of inflated housing costs and congestion helped doom Amazon’s plan to build a headquarters in New York City this year. The situation gives an economic boost to places that build both housing and jobs, Yun said.

One example might be Sierra Vista, a city in southeast Arizona of about 44,000 that lost 1,500 people between 2016 and 2017 but gained 1,400 last year, as increases in defense spending helped fuel growth at Fort Huachuca, said Robert Carreira, chief economist at Cochise College Center for Economic Research in Sierra Vista.

Nearby Sahuarita, a far suburb of Tucson, is more typical of places where people are seeking affordable housing, which Carreira called “drive till you qualify.” The small city of about 30,000 grew by more than 800 people, the largest bump in a decade.

Carreira said there are plans to build retirement homes in tiny nearby Benson, with less than 5,000 people, which has been losing population every year until gaining 31 last year. The city hopes to mimic the success of rural retirement areas such as the Villages in Florida, a development that contributed to a more than doubling of Fruitland Park’s population to about 10,000 between 2016 and 2018, said Richard Doty, a research demographer at the Bureau of Economic and Business Research at the University of Florida.

Retirees seeking affordable housing have helped boost population in small cities of less than 10,000, which collectively grew by more than 142,000 people, compared with about 8,000 between 2011 and 2012.

In California, efforts to build more urban housing have been frustrated despite Democratic Gov. Gavin Newsom’s vow to build 3.5 million homes by 2025 — about 500,000 a year, compared with the roughly 85,000 new homes that went up between 2017 and 2018. The state announced plans in April to build housing on state-owned land in cities including San Francisco.

State Sen. Scott Wiener, a Democrat representing the San Francisco area, sponsored a bill intended to ease construction of apartment buildings near mass transit that failed to pass this year. Wiener still hopes it will pass by next year.

“It’s politically divisive but it’s critical,” Wiener said. “We have to develop the political will and a culture change from Nimby attitudes. The people get it.”

In 2012, the cities with the most population growth were New York, Houston, Los Angeles, Phoenix and Austin. Last year’s leaders were Phoenix, San Antonio, Fort Worth, Seattle and Charlotte, and even those cities saw slowing growth compared with earlier years.

“It’s not that the biggest and brightest cities are any less attractive, but they are more expensive,” said Patrick Adler, a research associate at the University of Toronto’s School of Cities.

People who can’t afford housing, or maybe millennials looking for more space to raise children, might choose a less glamorous city nearby because of costs, Adler said. “If they’re in New York and they want a cheaper city with 70% of New York, maybe they’d go to Philadelphia or Jersey City. If they’re in Los Angeles or San Francisco, maybe they go to San Diego.”

Some of the smaller cities gaining population are exurbs that lost their appeal in the rush to live in cities. California’s “Inland Empire” has seen a resurgence in interest, with the cities of Ontario and Chino Hills east of Los Angeles seeing growth doubling since the previous year. Ontario now has about 181,000 people and Chino Hills has 83,000 — together they grew by about 8,900, compared with 4,400 the year before and less than a thousand five years ago.

That’s because affluent people priced out of Los Angeles have been buying new houses there. Light rail makes commuting possible, and home prices are half what they are in coastal suburbs like Orange County, said John Husing, an economist specializing in Inland Empire issues.

San Bernardino County, which includes Ontario and Chino Hills, opened to more extensive development about five years ago when dairy farmers decided to cash in and move their herds east, Husing said. It’s growing because it’s one of the few places in California where the typical family’s income is enough to buy the typical house.

At the same time, lower-income people are moving even further inland.

He added that legislation like Wiener’s, which prioritizes urban apartments, would hurt the development of single-family houses in the Inland Empire by subjecting it to more review.

“They call us sprawl. They’re trying to gut our kind of development,” Husing said. “I don’t see it happening. Over the last 50 years, people have repeatedly tried to get people out of single-family homes and the marketplace has said, ‘No way.’”