FORTUNE — It all seems so simple: Hit the “place order” button online and get a head of lettuce, a dozen eggs, or a gallon of milk delivered to your doorstep within 24 hours. But behind the scenes, the burgeoning market of online grocery delivery involves a surprisingly delicate dance to ensure that perishable food gets from producer to consumer in just enough time to avoid (quite literally) a spoiled delivery.
With demand on the rise, giants such as Wal-Mart
are jumping into the fray as investors bet millions on a crop of startups with names like Good Eggs, Relay Foods, and Farmigo. Yet actually turning a profit and managing the logistics of food delivery is tricky, says Leslie Hand, a director at IDC Retail Insights, a market research firm. Unlike shipping books or durable products via UPS, fresh food delivery requires specific short routing, planning, technology, and a race against the clock.
“We’re sprinting hard behind the scenes,” says Mike Brennan, chief operating officer of Peapod, an online grocer in Stokie, Ill. that offers next-day deliveries in 24 East Coast and Midwest markets. “It’s a race every day. The faster you go, the fresher it is.”
MORE: At Wal-Mart, moving the needle on e-commerce
Challenge No. 1 is temperature. A mixed bag of groceries usually includes food that needs different temperatures to stay fresh. Bananas turn brown when they’re cold, yet frozen food mustn’t melt. Similarly, milk can risk spoiling if a trek to a customer’s home takes too long.
Wal-Mart, which is testing online food delivery in San Jose and Denver, ships food directly from local superstores to customers using special Wal-Mart To Go trucks. They have three separate sections: one area for frozen food; one refrigerated area for fresh produce, meat, and dairy; and a third area for durable goods. If the retail giant pursues online deliveries nationwide, it has a unique advantage: Its 4,000 stores are located within five miles of two-thirds of the nation’s population.
Peapod.com invested in high-tech warehouses with eight separate climate zones set to specific temperatures and humidity levels so that produce ripens more slowly and stays fresh longer. Strawberries, grapes, and melons go together; tomatoes hang in warmer temperatures; bananas sit with bread; and peppers and green beans get their own 45-degree room. Workers pack orders in bags, crush-proof containers, and in temperature-controlled totes.
Task No. 2 is logistics. Delivery routes must include enough customers to ensure a delivery is worth the cost of gas, driver time, and vehicle costs. Food must stay fresh even when a truck takes four hours to stop at 25 homes.
MORE: The race for same-day delivery
FreshDirect, based in Queens, has 2,500 employees working each day to ensure that fresh local food gets to customers in New York, New Jersey, Connecticut, Pennsylvania, and Delaware. Workers rely on real-time dashboards to monitor on-time deliveries; in-field scanning devices to track every event along the delivery; and customized mapping software models to handle traffic patterns, weather, and street closures to guide hundreds of trucks on the road. “We’re maniacal about getting fresh food through to customers fast,” says FreshDirect cofounder David McInerney.
On the other coast, a similar high-tech twist on food delivery is playing out at the San Francisco startup Good Eggs. Engineers there spent more than 18 months developing the back-end software to manage the deliveries of everything from local bread and eggs to yogurt and steaks from 400 local producers in San Francisco, Brooklyn, Los Angeles, and New Orleans.
Rather than buying food wholesale, stocking a warehouse, and hoping product will sell, Good Eggs uses just-in-time inventory, where local bakers only bake the amount of bread ordered that day or fisheries only pull the number of fish purchased. The company, backed by $13 million from investors, designed a separate website for local food producers like the Prather Ranch Meat Co.
Each Tuesday, Prather Ranch chief executive Doug Stonebreaker receives new cuts of pork and beef from the company’s northern California ranch. On his Good Eggs website, he updates and manages inventory, uploads product photos, sets prices, and views product trend data, like what’s selling best.
MORE: Why Amazon is goosing Prime memberships
Each afternoon, internal software at Good Eggs analyzes the day’s orders to determine next-day staffing, delivery routes, and product needed. It sends an automated e-mail to producers like Stonebreaker, telling him which cuts of meat customers ordered.
The next morning, Stonebreaker and other food providers pack up their pre-sold goods and drive to Good Eggs’ downtown San Francisco food hub. There, a dozen Good Eggs workers wait with tablet computers in hand to check in and sort food in various temperature-controlled storage areas.
Tablet applications help organize orders into waves of deliveries that go out in four-hour windows, as workers pack food in paper bags and cold sleeves for each customer. Those same employees then jump in their personal vehicles and use GPS routing software on their smartphones to make drop-offs. Before an order arrives, a customer receives a text message alert.
Within 36 hours of clicking the “buy” button online, customers like Judy Shaper, a retired mortgage broker in Sausalito, Calif., receive a bag of food. Although Shaper is frustrated by the delivery fee — which at times has hit $8, because there are not yet enough customers in her neighborhood — she is a big fan of the delivery service since she can’t always get to the farmer’s market.
“The spinach I’ve gotten is the best I’ve had in my life,” she says, uttering the words that every food delivery service executive is dying to hear. “It’s so fresh.”