Skip to main content

Django Sharding

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

Note: Does not support Django 1.10.3 due to a bug in the release.

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

Build Status PyPI version PyPi downloads Coverage Status

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.

Read The Documentation

For information about how to setup sharding in your application, read the documentation.

Developer Experience

I wrote this library after working on this problem for Wave 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 implementation 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 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:

from django.contrib.auth.models import AbstractUser

from django_sharding_library.decorators import shard_storage_config
from django_sharding_library.models import ShardedByMixin


@shard_storage_config()
class User(AbstractUser, ShardedByMixin):
    pass

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

AUTH_USER_MODEL = '<app_with_user_model>.User'

Create Your First Sharded Model

Define your new model, eg:

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_name='app.ShardedCarIDs')
    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:

./manage.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

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

Metadata

Release files for django-sharding 5.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-sharding 5.2.0
File Size Uploaded Provenance Actions
django_sharding-5.2.0.tar.gz 20.9 kB Without provenance Details for django_sharding-5.2.0.tar.gz

Release files / django_sharding-5.2.0.tar.gz

Download URL django_sharding-5.2.0.tar.gz
Size 20.9 kB
Tags Source
SHA-256 checksum
How to use checksums
133bae49def17db5f7cf5b6b0172f0cea9f345300eb10896feb7bc26f83f34ff
BLAKE2b-256 checksum
How to use checksums
4a179419e601cec868cc8a36b6e9ef4236670ebad88c7e324f980ff621e91ffb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.8.0 tqdm/4.36.1 CPython/3.7.6

Security for django-sharding 5.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page