Vending Machines (Karat)

Difficulty: EASYCategory: HLDFrequency: 1Asked On 2025-05-01

Problem Statement

You're building infrastructure for ~188,888 internet-connected vending machines across major cities.

•Each machine connects via cellular network to a central server.
•Every machine reports stock levels and maintenance issues once daily at midnight.
•The central database stores these reports.
•A batch job runs at 1 AM to analyze the data and schedule restocking and maintenance.


Question:

Are there scalability or reliability concerns with this design?

If yes, how would you address them?

Solution

To address these issues, the reporting should be staggered instead of synchronized. Machines can be assigned random reporting windows across a broader time range—say within an hour—so that the traffic load is more evenly distributed over time. This can be done by using machine metadata such as timezone or city, or by implementing a randomized reporting offset.


Next, instead of having devices directly hit a central API and database, the ingestion process should be decoupled using a message queue system like Apache Kafka or AWS Kinesis. This way, vending machines simply push their updates to the queue, which absorbs traffic bursts and allows backend processors to consume data at a manageable pace.


The APIs receiving these updates should be stateless and horizontally scalable. Using a load balancer and autoscaling groups ensures that the system can dynamically expand to handle higher request loads. Behind these APIs, the data can be stored in a distributed or partitioned data store, such as a time-series database or a NoSQL system like DynamoDB or Cassandra. These are optimized for high write throughput and can handle large-scale ingestion workloads efficiently.


Each vending machine should also have retry logic with exponential backoff in case of failure, and every update should include a timestamp to maintain data accuracy even if received slightly late. Finally, instead of relying on a rigid batch job at 1 AM, the system should use stream processing—triggering restocking and maintenance tasks in near real-time as new data comes in. This ensures that the system remains responsive and reliable even if some machines report later than expected.

Comments