I have a model Tweet that I'd like to shard horizontally based on the
tweet author's id.

class Tweet(models.Model):
    author_id = models.IntegreField()
    text = models.TextField()


Let's say I set up 3 databases: shard0, shard1, shard2

I'd like to take the tweet author's id, do a modulo 3 and use the
result to determine which shard a tweet should be located in.

If I understand the documentation correctly, I'd do something like:

class TweetRouter(object):
    N = 3
    def db_for_read(self, model, **hints):
        if model.__name__ == "Tweet" and hints['instance'].author_id %
N == 0:
            return 'shard0'
        if model.__name__ == "Tweet" and hints['instance'].author_id %
N == 1:
            return 'shard1'
        if model.__name__ == "Tweet" and hints['instance'].author_id %
N == 2:
            return 'shard2'
        return None

A couple of questions:

- Did I use the argument **hints correctly? I'm not entirely sure

- How is auto-increment PK being handled in a horizontal sharding
scenario like this?
Say I have 3 tweets with 3 author_id: 0, 1, 2
Those 3 tweets will live in 3 separate databases.
Each of them will get an auto-increment PK assigned by its database.
Since they all live in different databases, potentially they could all
get the same PK.
Does that mean if I shard a model horizontally I could get into a
situation where different model instances could have the same PK? How
is this being handled?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to