Design a Grocery Subscription Service
🛒 What is a grocery subscription service? It is the part of a grocery app that says "send me two cartons of milk and a dozen eggs every week". The customer sets up the basket once. From then on, the system creates an order on each delivery date without anyone pressing a button. Customers can pause it while they are on holiday, resume it when they are back, or cancel it.
This problem looks like a CRUD exercise, but it tests three ideas that show up in many LLD rounds. A subscription has a lifecycle (active, paused, cancelled) with rules about which moves are legal. It has a schedule that has to keep working when the daily job is late or the customer pauses in the middle. And it creates orders that must not change when prices change later. Get those three right and the rest is bookkeeping.
Requirements
"Design a subscription service for a grocery app. Customers subscribe to items delivered on a regular schedule, and can pause, resume or cancel. Orders are created automatically."
Most of the design hides in what "regular", "pause" and "automatically" mean, so we ask.
Clarifying Questions
You: What is in one subscription?
Interviewer: A customer, a list of products with quantities, and how often: every N days, so weekly is 7. The customer also picks the day of the first delivery.
You: What creates the orders?
Interviewer: A job runs once a day and creates an order for every subscription that is due. Pass the day in as a number, so we can test it.
You: What if the job doesn't run for a few days, say after an outage?
Interviewer: The next run should catch up, but nobody wants three boxes of milk at once. One order per subscription, then carry on with the schedule.
You: When a customer resumes after a pause, when is the next delivery?
Interviewer: Keep their rhythm. If they got deliveries on Mondays, they still get them on Mondays: the next scheduled date on or after the day they resume.
You: Can a cancelled subscription come back?
Interviewer: No. Cancel is final, from active or paused. They can always create a new one.
You: Prices change. Which price does an order use?
Interviewer: The price on the day the order is created. Old orders keep the price they had.
You: Do we handle payment and stock?
Interviewer: No, those are other teams. We create the order and hand it over.
You: How many subscriptions?
Interviewer: Millions, but on any day only a small share are due.
The last answer rules out the simplest daily job (look at every subscription every day) as the final design, and the outage answer tells us the schedule can't assume the job runs every single day.
💡 Interview tip: Whenever a system does something "automatically" on a schedule, ask what happens when the scheduler is late. The answer ("catch up once" or "back-fill every missed date") changes the math, and interviewers love to see you ask it before they do.
Final Requirements
- Products have prices. A price can change at any time.
- A customer subscribes to one or more products with quantities, delivered every
intervaldays starting onstart_day. Invalid input (no products, unknown product, quantity or interval not positive) is rejected. - A subscription is active, paused or cancelled. Active can pause; paused can resume; either can cancel. Cancel is final. Anything else is rejected.
- A daily job, given the day, creates one order for each active subscription whose next delivery is on or before that day. A subscription that missed several dates gets one order. Then its next delivery moves to the first scheduled date after that day.
- On resume, the next delivery becomes the first scheduled date on or after the resume day.
- An order copies each product's price at the moment it is created. Later price changes don't affect it.
- With millions of subscriptions, the daily job's cost should depend on how many are due, not on how many exist.
Out of scope
- Payment, stock and delivery slots (owned by other teams).
- Editing a basket or frequency after subscribing (Extensibility).
- Calendar schedules like "the 1st of every month" (Extensibility).
- Notifications to the customer (Extensibility).