0

I’m asked these two interview questions in my interview and nowhere in web I could get the answer, as I’m sort of new to programming.

Can someone please help me with some Answers for these.

  1. How to attain synchronisation or concurrency management.

Imagine it’s a e-commerce website that users can redeem points. When a user opens another browser tab and redeems the available points two times, how to prevent this? It’s a rest Api using web api or graphql.

  1. How to redeem an available point say 20$ when user purchasing through our partner website, so basically item cost 50$, so redeem 20$ and pay only 30$ and remaining points or redeem amount should show as 0$

I don’t know answers to both the questions. Can someone give me some good insights for these and tips and also some links to study as I don’t know what techniques applies here too

Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • 1
    `how to prevent this?` There are many ways to do this. The simplest is some kind of shared locking system (using say Redis). For most SQL based shopfronts, the solution would be to run the SQL with a guard clause (something like `UPDATE PointsBalance Set Balance = Balance - 200 WHERE Balance >= 200`) and then check whether the update did anything. If it didn't, don't apply the discount. Getting this working perfectly is hard though - handling 'what if the payment failed' (which should give you back your points) etc etc. – mjwills Jun 03 '19 at 00:43
  • Hi mjwills, thank you, true I spoke about locking. I even told about isolation levels. He asked me specifics in the api level. He asked me which object I would lock. He said the total points available are calculated by few other functions. So which function or object I will lock he asked. And yes it’s amazon cloud – Jasmine Jun 03 '19 at 00:55
  • 1
    There is no one way to do this (and if the interviewer says there is, reject the job - you don't want to work with someone who does 'guess what I am thinking' style interviews). Any form of distributed lock on the coupon code / id would work. Redis distributed lock as an example - https://redis.io/topics/distlock . It depends very much on the data store though - if SQL based then my SQL suggestion is where I would start. This question is hard to answer without *way* more context. – mjwills Jun 03 '19 at 01:21
  • 1
    Mjwills, thank you so much for helping me. I’ll read about redis dist lock you shared. Well yes they use sql but they also use aws lambda angular etc. all on aws. I am invited for final stage of interview tomorrow lol. I passed previous round. But they have coding test for me next round lol which I doubt I’ll be able to perform wihout using google unless it’s easy one. Anyway it will all be learning opportunity even if i don’t clear interview tomorrow. Hope for the best. I’ll read about redis now. Thank you so much again for your help. – Jasmine Jun 03 '19 at 07:57

0 Answers0