Vending Machine: High Level System Design
Last Updated :
04 Jan, 2024
Vending machines which are a common thing to be seen in shopping malls or metro stations, kind of operate by themselves. It’s different from other systems since it is not a distributed system used by millions of people. It is concurrently used by one person at a time and may be used by a few thousand people a day. So we won’t need features like load balancing and caching that we normally use in a system design.
Important Topics for High-Level Design of Vending Machine
Vending machine’s requirements:
- Ability to select an item.
- Ability to pay for an item(Ask the interviewer which mode of payment they prefer. In this example, we will stick with cash payment.
- Dispense the item once payment is made.
- Notify the owner (serving agent) about inventory data. in case the vending machine runs low on any of the items, the owner can refill that item in the vending machine.
Lifecycle of a Vending Machine
Lifecycle of Vending Machine
Below is the step-by-step Lifecycle of the Vending Machine:
- The first thing that we want to make sure of is that the vending machine is in the ready state.
- After that, the customer needs to select the item that they want to get and make the required payment for that item, only then will the vending machine dispense the item through a tunnel.
- In case of transaction failure, the system will revert to its ready state. In terms of payment, since this is a cash-only system, it would be beneficial if the vending machine were able to provide change to the customer.
- In addition to this, we can add the cancel button to the vending machine to cancel the transaction.
Design of Vending Machine
So in the system design diagram, on one side, we have our user. On the other hand, we have our vending machine which consists of two to three key components. We will split these components:
Item Selection
This component has a mapping of an item to a number so that when we select an item, it knows which item to dispense based on that mapping. We can use a a hashmap for this purpose so that the selected button is mapped to the correct item. We can also store the items in a matrix format with a cards row number suggesting the button the user needs to press to get the corresponding item in that column.
Payment System
Payment Gateway
Since as we know we are dealing with cash here the vending machine should be capable of calculating and generating change. We can also include some authentication mechanisms in our payment system to check if the user is using counterfeit currencies.
Dispense order
So when a customer selects a particular item, the system should be able to make those physical or mechanical movements to dispense that item to the customer. So the user kind of interacts with many components of the vending machine as a whole and not just a single component. The user interacts with the vending machine using a keypad, which is used to take in all the inputs.
Follow-Ups
Now there could be follow-up questions like the workflow of the system when the user selects something that is in stock and the workflow of the system when the user selects something that is out of stock, below is the explanation of both the cases:
In Stock
When the user selects something that is in stock
- The item selection knows what item the vending machine has to dispense.
- After that, it goes to the payment section, where the price of the item is calculated.
- Then in case the money provided by the user is surplus than the actual price, the change is also calculated by the payments system, and the change is dispensed.
- After the successful calculation of the payment system, it triggers a notification or a the message of the successful transaction to the “dispensing of the item” which finally dispenses the item to the user.
Out of Stock
The user asks for some item that is out of stock
- So an out-of-stock message needs to be displayed to the customer.
- Our item selection kind of deals with these things, it has an inventory counter which stores the count of each item of the vending machine.
- So it checks if the user asked for an item that is in stock or else sends an error message.
Another follow-up Scenarios:
Consider the case if the user does not pay the right amount of money that is the user pays less than the money that the item asks for. So our payment system has to deal with this scenario too. In case that condition is violated, a transaction failed message should be displayed along with reverting the entered money.
Also, there is another condition where the system runs low in change. So in that case, the system can either cancel the transaction and give the money back to the user without dispensing the item due to low change or ask the user to try and provide the exact amount for the item.
Scalability
If we want to add more features to our vending machine like credit card payment we can create a payment gateway for credit cards in the vending machine thus making our system easily scalable.