Design a system to enforce a daily budget for cost-per-click based ads
 

Functional requirements

  1.  The system should be able to calculate the cost for every click and compare total cost with the budget at run time
  2. 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
  3. The system should store daily spending in DB as well.
  4. The user should be able to see how much money is remaining in his budget
  5. Enforcing the latest budget. The change in budget should be seamless 
  6.  Bringing current budget/System should have an ability to absorb change in budget

Non functional requirements

  1. High latency since it may result in loss of revenue
  2. The system should scale and able to take users up to 100 M
  3. High availability

Assumptions

  1. The system constantly receiving click cost from the main system
  2. The system should also receive daily budget along with Advertiser ID
click based ads system design problem

 

Flow

  1. 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.
  2. 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.
  3. 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.
  4. The Apache storm would do the live calculation and push that to another Redis cluster
  5. 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.

Improvements

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

  1. 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
  2. The DB can be sharded to have better performance. We can leverage consistent hashing to distribute the load evenly
  3. Every server should be clustered to have good fault tolerance
  4. We can generate multiple reports out the data we have. The users can be given the recommend budget as well.
  5. Redis and Kafka are clustered and we can rely on them