Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

In-memory sharding solution

Sumit Rawal answered on May 21, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • Is Sharding Database partitioning

  • In-memory sharding solution

    0

    The wallet application maintains an account balance for every user account. A good data structure to represent this relationship is a map, which is also called a hash table (map) or key-value store.

    For in-memory stores, one popular choice is Redis. One Redis node is not enough to handle 1 million TPS. We need to set up a cluster of Redis nodes and evenly distribute user accounts among them. This process is called partitioning or sharding.

    To distribute the key-value data among N partitions, we could calculate the hash value of the key and divide it by N. The remainder is the destination of the partition. The pseudocode below shows the sharding process:

    String accountID = "A";

    Int partitionNumber = 7;

    Int myPartition = accountID.hashCode() % partitionNumber;

    The number of partitions and addresses of all Redis nodes can be stored in a centralized place. We could use Zookeeper [4] as a highly-available configuration storage solution.

    The final component of this solution is a service that handles the transfer commands. We call it the wallet service and it has several key responsibilities.

    Receives the transfer command

    Validates the transfer command

    If the command is valid, it updates the account balances for the two users involved in the transfer. In a cluster, the account balances are likely to be in different Redis nodes

    The wallet service is stateless. It is easy to scale horizontally. Figure 3 shows the in-memory solution.

    1fromclientAtoclientB,itissuestwocommandstotwoRedisnodes.FortheRedisnodethatcontainsclientA’saccount,thewalletservicededucts1 from the account. For client B, the wallet service adds $1 to the account.

    Candidate: In this design, account balances are spread across multiple Redis nodes. Zookeeper is used to maintain the sharding information. The stateless wallet service uses the sharding information to locate the Redis nodes for the clients and updates the account balances accordingly.

    Interviewer: This design works, but it does not meet our correctness requirement. The wallet service updates two Redis nodes for each transfer. There is no guarantee that both updates would succeed. If, for example, the wallet service node crashes after the first update has gone through but before the second update is done, it would result in an incomplete transfer. The two updates need to be in a single atomic transaction.

    Popularity 1/10 Helpfulness 1/10 Language whatever
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on May 21 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.