The fire department in Menlo Park, Calif., home to Facebook Inc., has an emergency on its hands: how to prevent its firefighters from living hours away.

In an example of a wider trend that is making it harder for Silicon Valley communities to retain critical nontech workers, 15 of the Menlo Park Fire Protection District’s firefighters live at least 100 miles away, compared with one in the 1980s.

As...