List functional requirements for the system (Ask the chat bot for hints if stuck.)...
Users can choose the product and its number in the vending machine.
Users can pay for the product with a bank card, cash, Paypal, the cryptocurrency and get the choosen product.
If users choose a product number that isn't available, the vending machine shows an error.
If users choose the product and there is a blackout, the vending machine shows an error.
If users choose the product and there is an internet connection issue, the vending machine shows an error.
Estimate the scale of the system you are going to design...
10 million vending machines need about 100 GBytes.
Define what APIs are expected from the system...
Endpoint: POST /pay
Description: This API starts paying for the selected products in a vending machine.
Payload: { "vendingMachineId": "string", "paymentMethod": "string" // Values: {"cash", "card"},
"paymentKey": "string"//encrypted card information,
"productType":"string",
"price":"double", "productAmount":"int"
{"oriderId":"string", "order":{ ["productId": "string", "productName": "string", "expirationDate" :"timestamp",
"price":"double","amount":"int"]}
}
}
Response:
{ "success": true, "message": "Status updated successfully." }
{"success":false,"message":"Not enough money","Card information is invalid","Connection broken"}
2. Get the product list from the vending machine
Endpoint: GET /products/<vendingMachineId>
Description: This API returns the list of products the user can buy in the given vending machine.
Response:
{
["productId": "string", "productName": "string", "expDate" :"timestamp", "updateDate":"timestamp",
"price": "double", "amount": "int"]
}
Admin API:
Endpoint: POST /vendingMachine
Description: This API creates a new vending machine.
Payload:
{"namee":"string", "location": GeoData, "createdAt":timestamp}
Response:
{"vendingMachineId":"string", "location": GeoData,"createdAt": timestamp }
Endpoint: GET /vendingMachines
Description: This API returns the list of vending machines.
Response:
{
[{"vendingMachineId":"string","location": GeoData,"createdAt":timestamp }]
}
Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
Below is a detailed schema using PostgresSQL.
VendingMachineProduct Table
VendingMachine Table
This table has information about the vending machines that are installed.
This table has information about the vending machine orders.
OrderDetail Table
This table has information about the specific vending machine orders.
VendingMachine ||--o{ VendingMachineProduct : has
Order ||--o{ OrderDetail : has
API Gateway
Payment service
Order service
Product List service
We'll examine the scenario:
Selecting and paying for the product from the vending machine.
This flow describes what happens when a user selects the product.
Sequence of Events:
Purpose: handles all incoming API requests for selecting and paying for the products in the vending machines.
Key Responsibilities:
Scalability:
Technologies:
Purpose: a facade for the third-party payment services.
Key Responsibilities:
Scalability:
Technologies:
Purpose: Manages the product list and the vending machine list.
Key Responsibilities:
Scalability:
Technologies:
Cache is used to offload the The Product List service.
Scalability:
Horizontal Scaling: cache is scaled out by using productId as the sharding key.
Technologies:
Redis: Ideal for this role due to its performance and features like publish/subscribe, which are useful for invalidating cache entries when data changes.
Trade-off:
Technology Choice: WS DynamoDB for real-time operations due to its superior performance in handling large volumes of data with high write and read throughput. PostgreSQL was chosen for detailed logs and analytics where complex queries are more common.
Trade-off:
Technology Choice: Using stateless architecture facilitated by a load balancer (like NGINX or AWS ELB) to ensure requests can be served by any server, enhancing reliability and ease of scaling.
Trade-off:
Technology Choice: Redis, known for its quick data access speeds and robustness, fitting the requirement for a high-performance, in-memory data store that supports rapid read and write operations.
Trade-off:
Technology Choice: Kafka offers reliable messaging with strong delivery guarantees and is widely used in systems requiring high levels of decoupling and scalability.
Database Failure
Cache Failure
Order service, Payment service and Product List service Downtime
Network Issues
Cache Hotspots
Database Write Throughput