Skip to main content

A sharding app to include in your project so that you can shard your data.

Project description

# [Django Sharding](https://github.com/JBKahn/django-sharding)

Django Sharding is a library and part-framework for sharding Django applications.

It helps you to scale your applications by sharding your data across multiple databases in a consistent way.

[![Circle CI](https://circleci.com/gh/JBKahn/django-sharding.svg?style=svg)](https://circleci.com/gh/JBKahn/django-sharding)
[![PyPI version](https://badge.fury.io/py/django-sharding.svg)](https://badge.fury.io/py/django-sharding)
[![PyPi downloads](https://img.shields.io/pypi/dm/django-sharding.svg)](https://crate.io/packages/django-sharding/)
[![Coverage Status](https://coveralls.io/repos/JBKahn/django-sharding/badge.svg?branch=master&service=github)](https://coveralls.io/github/JBKahn/django-sharding?branch=master)

### What is Sharding?

Sharding is a way of horizontally partitioning your data by storing different rows of the same table in multiple tables across multiple databases. This helps to increase the number of connections to a given resource as well as improves read performance of your application.

### Developer Experience

I wrote this library after working on this problem for [Wave](https://www.waveapps.com) and not being able to find a library that suited our needs. What we were looking for was something that was powerful, extensible and customizable. This library was created for just that purpose and includes at least one implimentation of each part of the pipeline with room to replace any individual components.

### Influences

The package was influenced by my experiences at Wave as well as the help and code of my co-workers.

### Installation

Check out the [installation section](http://josephkahn.io/django-sharding/docs/installation/Settings.html) of the docs for basic package setup.

### Basis Setup & Usage

#### Sharding by User

Select a model to shard by and open up the models.py file. Here we'll use the user model:

```python
from django.contrib.auth.models import AbstractUser

from django_sharding_library.models import ShardedByMixin


class User(AbstractUser, ShardedByMixin):
pass
```

Add that custom User to your settings file using the string class path:

```python
AUTH_USER_MODEL = '<app_with_user_model>.User'
```

#### Create Your First Sharded Model

Define your new model, eg:

```python
from django.db import models

from django_sharding_library.decorators import model_config
from django_sharding_library.fields import TableShardedIDField
from django_sharding_library.models import TableStrategyModel


@model_config(database='default')
class ShardedCarIDs(TableStrategyModel):
pass


@model_config(sharded=True)
class Car(models.Model):
id = TableShardedIDField(primary_key=True, source_table=ShardeCarIDs)
ignition_type = models.CharField(max_length=120)
company = models.ForeignKey('companies.Company')

def get_shard(self):
return self.company.user.shard
```

### Running migrations

Run them as normal, for example:

```
./managy.py makemigrations <app_name>

# To let django run the migrations in all the right places.
./manage.py migrate <app>

# To specify the database to run it on
./manage.py migrate <app> --database=<database_alias>
```

### Acccessing sharded data

```python
# TODO: Update this with methods.
shard = User.shard
Car.objects.using(shard).get(id=123)
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_sharding-0.0.2.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sharding-0.0.2-py2-none-any.whl (30.8 kB view details)

Uploaded Python 2

File details

Details for the file django_sharding-0.0.2.tar.gz.

File metadata

File hashes

Hashes for django_sharding-0.0.2.tar.gz
Algorithm Hash digest
SHA256 36ce72c0368bc95ae2d254ceb27c4374deff19ac607d1b4c267cd58b74715310
MD5 404bc4b4287cb7022c7d867d9be60e41
BLAKE2b-256 72b9388b4c66ae4efc1baa6819c9a36022a0d6a218e8e91128adbe82c81915d1

See more details on using hashes here.

File details

Details for the file django_sharding-0.0.2-py2-none-any.whl.

File metadata

File hashes

Hashes for django_sharding-0.0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 a0033275d8e457f71e8653d345d006c521e1b3d3bd442606ca7c4502601dff54
MD5 98ce6797501f003c53d6c2fa8e786fe1
BLAKE2b-256 1ff1f429f5a987439a6a3a02054549d6f2ba0d3ec5bf2b533aa9d05fd4981963

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page