Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Digital_Wallet.pdf

 Digital_Wallet.pdf

This slide cover on topic how cross wallet amount transfer happens using different possible solutions
- Have gone through each solution pros and cons
- Which one is preferable for monolith and microservices and also based on the key pointers.

Vrushali Raut

July 29, 2023
Tweet

More Decks by Vrushali Raut

Other Decks in Technology

Transcript

  1. 1 2 5 Problem statement Summary 2PC, Try-Confirm-Cancel solution In-memory,

    Database sharding solution 3 Agenda Digital Wallet Saga 4
  2. Digital Wallet One thing worth mentioning is data type of

    the “amount” field is “string” Open Question :- Why string and why not doubles?
  3. Digital Wallet Why string and why not doubles? Different protocols,

    software and hardware may support different numeric precisions in serialization and deserializatio, this may cause unIntended rounding errors? The number could be extremely big or extremely small Many choose float or double as a datatype. It is still a proper choice as long as individuals understand the potential risk of losing precision.
  4. Digital Wallet Possible solutions to implement cross wallet money transfer

    In-Memory sharding aka low-level solution Distributed Transactions solutions - high-level solution Redis Sharding Database Sharding 2PC/Long living transactions Try Confirm Cancel TC/C SAGA
  5. Digital Wallet - In memory solution Let’s Assume Wallet application

    maintains an account balance for each user account. For In-memory stores we use Redis String accountId = “A” Int partitionNumber = 7 Int myPartition = accountID.hashCode() % partitionNumber; Zookeeper
  6. Digital Wallet - In memory solution Let’s Assume Wallet application

    maintains an account balance for each user account. The final component is a service that handles transfer commands. - Let’s call it as a wallet service. Key responsibilities of wallet service will be - - Receive transfer command - Validate - If valid - Update account balance for two users involved in the transfer In real life user accounts are likely to be present in different nodes.
  7. Digital Wallet - In memory solution Each Users account balances

    are evenly spread across these 3 redis nodes
  8. Digital Wallet - In memory solution What are the disadvantage

    of using In-Memory solution? Does not meet our correctness requirement Wallet service updates two redis nodes on each balance transfer request -> there is no guarantee that both updates would succeed. Example- Wallet service node crashes after the first update has gone through but before the second update is done. -> result in an incomplete transfer. Open Question -> For this situation if you think What is needed?
  9. Digital Wallet - In memory solution What are the disadvantage

    of using In-Memory solution? Does not meet our correctness requirement Wallet service updates two redis nodes on each balance transfer request -> there is no guarantee that both updates would succeed. Example- Wallet service node crashes after the first update has gone through but before the second update is done. -> result in an incomplete transfer. For this situation if you think What is needed? The two updates need to be in a single atomic transaction.
  10. Digital Wallet - Relational Database solution Replace each redis node

    with a transactional relational database node.
  11. Digital Wallet - Distributed transaction Key moment in 2pc model

    protocol - When database nodes replies to coordinator we are willing to commit the transaction, then coordinator nodes decision is to commit or abort the transaction. What if database nodes crash in this process -> What happens when Coordinator crashes? - Coordinator writes its decision to disk - When it recovers, read the decision from disk and send it to replicas. (Or abort if no decision was made before crash) - Problem : If coordinator crashes after prepare, but before broadcasting decision, other nodes do not know how it has decided - Replicas participating in transaction cannot commit or abort after responding “ok” to the prepare request (Otherwise we risk violating atomicity) - Algorithm is blocked until coordinator recovers Then the coordinator will timeout and says we can abort the transaction for everyone.
  12. Digital Wallet Distributed transaction - Try-Confirm/ Cancel (TC/C) TC/C is

    an alternative to 2PC transactions, designed to handle long live transactions that involve multiple services. TC/C is a type of compensating transaction that has two types. 1. In the first phase, the coordinator asks all databases to reserve the resources for the transaction 2. In the second phase, the coordinator collects replies from all databases : If database replies with yes -> coordinator will ask all database to confirm the operation If any database replies with no. coordinator will ask all the databases to cancel the operation, Which is the Try-Cancel process.
  13. Digital Wallet - Distributed transaction The problems could appear with

    this pattern is - l What if First Try phase fails What if the first TRy phase succeeds but confirms fails? - which can result in $100 missing -> The sum of account balance will be 0, which is less than the beginning of TC/C. It violates the fundamental rule of accounting that sum should remain the same after a transaction
  14. Digital Wallet - Distributed transaction - SAGA The idea of

    saga is simple All operations are ordered in a sequence. Each operation is an independent transaction on it’s own database Operations are executed from the first to the last. When one operation has finished, the next operation is triggered a. b. c.
  15. Digital Wallet - Distributed transaction - SAGA When an operation

    has failed, the entire process starts to roll back from the current operation to the first operation in reverse order called backward recovery. Another way to handle this is you retry i.e you try to execute that step again and you continue your transaction to make cross wallet transfer successful that is called as a forward recovery.
  16. Digital Wallet The choice of which coordination model to use

    is determined by the business needs and goals. Challenge of the choreography solution is := services communicate in a fully asynchronous way, so each service has to maintain an internal state machine on order to understand what to do when other services emit an event. It can become hard to manage when there are many services. Orchestration Solution handles the complexity well. So it is usually preferred solution in digital wallet system SAGA Design Pattern -Which solution should implement when?
  17. Digital Wallet Which one should we use in practice? ::

    The answer depends upon the latency requirement. Operations in saga have to be executed in linear order, but it is possible to execute them in parallel in TC/C If there is no latency requirement, or there are very few services such as our money transfer example we can choose either of them If we want to go with the microservice architecture choose Saga If the system is latency-sensitive and contains many services operations TC/C might be a better option. So decision depends upon few factors :-
  18. Digital Wallet - Distributed transaction References :: Alex-xu book volume-2

    [Martin kleppmann] https://www.youtube.com/watch?v=-_rdWB9hN1c [sudocode] https://www.youtube.com/watch?v=vGOEO6mO674&t=604s