vendredi 24 avril 2020

System design interview question related with concurrent access in distributed systems

I have a question related with system design.

Suppose we have a service which accepts the points from a merchants on the basis of the payment done using some wallet.Like uber customer uses the paypal for the payment;Then in that case uber calls this api (exposed by the paypal) to give some points to the user as he spent some money using paypal(example is, if the user spent 500 Rs then uber calls this api with 5 points for that user).

This api accepts the following fields as a input { merchant id, request id, userid, number of points allotted }

So as the scale increases,We can have lot of micro services which can accept the requests.

Now in the DB we maintain the table called points which has the following fields

points ( merchant id, request id , userid, no of points allotted, points remaining , updated at, Created on )

Please note that the request id is generated by the merchant and there are chances that the merchant merchant request is already been used then in that case we need to tell the merchant that please use some other request. You can assume that merchant for its book keeping keep a table where primary key is the request id.

So here in the points table we can assume that merchant id and request id can be considered as the primary key.

1). Now if the merchant makes a request and that request took some time at the server side and the merchant wants to know what happened to that request.This request can be anywhere in the system.It can be stuck somewhere in some thread.Waiting in the queue for its term to be executed.How can we accomplish this.

I thought of it we can use some sort of key-value store.As soon as the request comes in for updating the points table,This key-value cache is updated;Key being merchantid@requestid with ttl as the request time out time.so this can be referred.

2). Now what happens when a user u1 has some entries like

merchant id,request id, user id, number of points,points remaining

m1 , r1 , u1 , 10 ,0

m1 , r2 , u1 , 15 ,0

m2 , r1 , u1 , 25 ,0

m3 , r1 , u1 , 10 ,0

Now user u1 has 60 points.u1 wants to use these points. Now 2 requests hit the system at the same point in time. one request for u1 want to use the 40 points and the other request wants to use the 30 points.They hit on different services.So when they query the database each query can see that they have sufficient values and on the top of that there will be fields where you have the value remaining fields which will be updated.How can we handle the point remaining case.

How to handle such case?

And how can we handle the cases like where 2 requests hit the services in parellel.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire