On a recent trip to Hawaii, my friends and I stopped at the local Costco (Nasdaq: COST) to pick up some supplies and fill up the tank of our minivan. My first thought was, "Hawaii has a Costco?" and that was quickly followed by, "Costco has gas?" Growing up, the Costco near me didn't sell gas, and the closest Costco to where I live now doesn't sell gas, either. I had no idea.

As my vacation progressed, I couldn't wrap my head around Costco selling gas. How could it sell it cheaper than ExxonMobil? Gas prices are so volatile, and margins are so thin -- why bother getting into the business? I took long walks on the beach, I stared perplexedly into the eyes of wild Spinner dolphins, and still, nothing came to me.

But now I get it.

Pump your own gas
Two-thirds of Costco's stores (none of them near me) sell gas. Most times, it's at or below neighboring gas station prices. The store is able to reduce overhead and keep gas prices low by employing fewer gas pump attendees and taking other cost-cutting measures.

Once customers notice that gas is cheaper at Costco, they can't just drive up to the pump; a membership is required to purchase gas. Customers who want cheaper gas year-round will grin and bear the annual membership fee, knowing in the long run they will save money. Costco reports that membership fees in the third quarter rose from $395 million to $435 million year over year. Membership = revenue for Costco.

Of course, once customers are in the parking lot getting gas, they are more likely to go inside to buy items. Why bother driving somewhere specifically for cheap gas, only to turn around and go home? Less driving = revenue for Costco.

Gas hits the bottom line
Now that the rising costs of cotton and meat are crunching retail and wholesale margins, it helps to have another revenue stream pick up the slack. Costco is expecting gasoline sales of $8 billion this fiscal year, likely making up 9% of total revenue. Gas sales in the current quarter are up 30% on the year.

Costco isn't the only wholesaler taking advantage of selling gas, either. I've never been to a BJ's Wholesale (NYSE: BJ) or a Wal-Mart (NYSE: WMT)-owned Sam's Club, but these stores also sell gas. BJ's recently reported that May revenue was up 7.4% and that gasoline sales made up 4.4 percentage points of the increase. Wal-Mart is also using a gas promotion to get gas-conscious consumers into its stores.

Foolish takeaway
Gas can be the gateway drug to wholesale shopping when prices are high. As an investor, it's nice to know that when times are tough, Costco has a game plan for driving traffic to its stores.

Interested in the future of Costco? Click here for a Motley Fool special free report, "The Death of Wal-Mart—The Real Cash Kings Changing the Face of Retail."