I have a matching-engine microservice requirement for my application, i need to implement a matching between depositors and withdrawers . So, all the details of depositors and withdrawers are stored in redis cache. There will be tons of requests coming into this microservice and are put into redis cache. i need to match on the basis of same amounts.
1) How to do the matching, do i need to fetch details from cache into two separate arrays and perform matching on these two arrays. Iterating with both the arrays, is it the right way to do it?
2) As and when the data fetching into arrays goes bigger , the matching gets complicated and will take lot of time too. What is the better approach to deal with this scenario?
Consider the following scenario:-
w/d |w/d amt|w/d-bonus|d/p | depositors
personA |10000 | 100 |person1| 70000
personB |50000 | 750 | |
personC |20000 | 100 | |
personD |1000 | 50 | |
personE |100000 | 1000 | |
here person1 will be matched first with personB (50K -50K), personB is completely matched hence he will give person1 750 amt as bonus to depositor.
now still 20k of depositor has to be matched. so, that will be matched with personE , since personE got only 20K (personE needed 100K) he will give only 200 amt (for 100K we have 1K bonus then for 20K bonus will be 200/- amt)as bonus to depositor.
Point to be noted:
1) person1 is first matched with personB then he is matched with personE.
2)person1 could have matched totally with personE but person1 would get less bonus that is 700/- amt.
3)but now since person1 is matched with personB and personE , person1 will receive total bonus of 950/- amt
Main aim is to provide depositor a maximum bonus.
above is an example , the perfect match would be D,B,A,E and D,B,E
we will consider D,B,A,E since we can satisfy 4 withdrawers and yet
give the depositor maximum bonus.
here:
w/d => withdrawer
d/p => depositor
amt => amount