- The system should be able to calculate the cost for every click and compare total cost with the budget at run time
- The remaining budget is sent back to main system responsible showing ads. The amount would decide whether to show more ads or stop for that day
- The system should store daily spending in DB as well.
- The user should be able to see how much money is remaining in his budget
- Enforcing the latest budget. The change in budget should be seamless
- Bringing current budget/System should have an ability to absorb change in budget
Non functional requirements
- High latency since it may result in loss of revenue
- The system should scale and able to take users up to 100 M
- High availability
- The system constantly receiving click cost from the main system
- The system should also receive daily budget along with Advertiser ID
- The click cost is sent from main system to the system we are designing by means of Kafka stream. The orderly execution and high reliability are our main requirements hence we prefer Kafka.
- We also maintain a DB which would constantly reading daily budget and respective advertiser ID from main system. We use Kafka here as well to constantly add if there are changes to budget for individual users. There may be newly added users as well which should be fed to queue as well.
- The Cost per click is fed to Apache storm which keeps picking budget for that advertiser from Redis cluster. We don’t want to read budget from DB directly since too many DB calls would significantly affect the speed.
- The Apache storm would do the live calculation and push that to another Redis cluster
- When the main system wants to push the ad from advertiser, it can keep pulling the remaining budget for each user and keep posting the ads.
I have not shown many things here but during interview, you can talk about the improvements which you can do to further enhance the system. Some of which are
- The remaining budget to be stored in permanent storage as well with time stamps. That would help us to generate reports and create some intelligent analytics
- The DB can be sharded to have better performance. We can leverage consistent hashing to distribute the load evenly
- Every server should be clustered to have good fault tolerance
- We can generate multiple reports out the data we have. The users can be given the recommend budget as well.
- Redis and Kafka are clustered and we can rely on them