As buyers and renters have been priced out of large cities, smaller exurban cities like Ontario, Calif., have boomed.

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, Calif., 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.”

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.

“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, he 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.

Last year’s population growth leaders were Phoenix, San Antonio, Fort Worth, Seattle and Charlotte, but they slowed down when compared to prior 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.”