Search code examples
mysqlsharding

Sharding logic for mysql Database


Usually sharding logic are placed in appserver. Query will be passed to the specific shard by querying the meta. Apart from appserver, where else sharding logic can be placed so that load in appserver can be reduced.


Solution

  • A fairly popular solution is to use the Ambassador pattern.

    It is understood that an additional application will be launched next to the main one. Shard distribution logic will be placed in the ambassador. Thus, the main service will be unloaded.

    enter image description here

    1. In first step client sends request with proper payload.
    2. In second step appserver makes decision that this payload has to be saved in database and sends request with data to ambassador app.
    3. The ambassador, in turn, has an algorithm for distributing data among shards(By meta information or hash functions).
    4. In the last step information is sent to the desired shard.

    With this approach, the distribution service and unnecessary load are removed from the main service.

    It is worth noting that it is important to place the ambassador in one physical host to reduce network latency.

    This may not be the best approach, but just one option.