Junior — Middle
Have you had experience with database partitioning (sharding)?
sobes.tech AI
Answer from AI
Yes, I have experience working with database sharding. In one of the projects, we divided user data based on geographic location to reduce the load on individual servers and improve performance. For example, user data from Europe was stored in one shard, and from Asia — in another. This allowed us to scale the system and provide faster data access.
A simple example of sharding in Python:
# Function to select shard based on user_id
def get_shard(user_id):
shard_number = user_id % 2 # Divide into 2 shards
if shard_number == 0:
return "db_shard_1"
else:
return "db_shard_2"
# Usage
user_id = 12345
shard = get_shard(user_id)
print(f"Connecting to database: {shard}")
This approach helps distribute load and simplifies scaling.