Skip to main content

Simple database sharding (horizontal partitioning) library for Django applications.

Project description

Django Horizon

https://travis-ci.org/uncovertruth/django-horizon.svg?branch=master https://api.codacy.com/project/badge/Grade/6f4ba73576904beaa41d68f40970bda9 https://codebeat.co/badges/74f07702-68ed-47e7-91e6-9088b0532342 https://www.codefactor.io/repository/github/uncovertruth/django-horizon/badge https://codecov.io/gh/uncovertruth/django-horizon/branch/master/graph/badge.svg Documentation Status Updates Python 3 https://img.shields.io/pypi/v/django-horizon.svg

Purpose

Simple database sharding (horizontal partitioning) library for Django applications.

Logo

Features

  • Shard (horizontal partitioning) by some ForeignKey field like user account.

Installation

To install Django Horizon, run this command in your terminal:

$ pip install django-horizon

This is the preferred method to install Django Horizon, as it will always install the most recent stable release.

If you don’t have pip installed, this Python installation guide can guide you through the process.

Usage

Setup

Add database router configuration in your settings.py:

Horizontal database groups and a metadata store
HORIZONTAL_CONFIG = {
    'GROUPS': {
        'group1': {  # The name of database horizontal partitioning group
            'DATABASES': {
                1: {
                    'write': 'member1-primary',
                    'read': ['member1-replica-1', 'member1-replica-2'],  # Pick randomly by router
                },
                2: {
                    'write': 'member2-primary',
                    'read': ['member2-replica'],
                },
                3: {
                    'write': 'a3',  # Used by 'read' too
                },
            },
            'PICKABLES': [2, 3],  # Group member keys to pick new database
        },
    },
    'METADATA_MODEL': 'app.HorizontalMetadata',  # Metadata store for horizontal partition key and there database
}
Database router
DATABASE_ROUTERS = (
    'horizon.routers.HorizontalRouter',
    ...
)

Example models

Horizontal partitioning by user

Metadata store
from horizon.models import AbstractHorizontalMetadata

class HorizontalMetadata(AbstractHorizontalMetadata):
    pass

In the example, metadata store save followings.

  • group: Group name for horizontal partitioning.

  • key: Determines the distribution of the table’s records among the horizontal partitioning group.

  • index: Choosed database index in horizontal partitioning groups.

Sharded model
from django.conf import settings

from horizon.manager import HorizontalManager  # For Django<1.10
from horizon.models import AbstractHorizontalModel


class SomeLargeModel(AbstractHorizontalModel):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.DO_NOTHING,
        db_constraint=False,  # May be using anothor database
    )
    ...

    objects = HorizontalManager()  # For Django<1.10

    class Meta(object):
        horizontal_group = 'group1'  # Group name
        horizontal_key = 'user'  # Field name to use group key

In many cases use UUIDField field for id. The AbstractHorizontalModel uses UUIDField as a them id field in default.

Using a model
from django.contrib.auth import get_user_model


user_model = get_user_model()
user = user_model.objects.get(pk=1)

# Get by foreign instance
SomeLargeModel.objects.filter(uses=user)

# Get by foreign id
SomeLargeModel.objects.filter(uses_id=user.id)
Model limitations
  • django.db.utils.IntegrityError occured when not specify horizontal key field to filter

    SomeLargeModel.objects.all()
  • Cannot lookup by foreign key field, cause there are other (like default) database

    list(user.somelargemodel_set.all())

History

1.1.0 (2018-03-30)_

  • Drop support for Django 1.9, 1.10

1.0.0 (2018-02-02)

  • Add support for Django 2.0

  • Drop support for Django 1.8

0.0.1 (2017-05-22)

  • First release on PyPI.

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-horizon-1.1.0.tar.gz (239.5 kB view details)

Uploaded Source

Built Distribution

django_horizon-1.1.0-py2.py3-none-any.whl (13.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-horizon-1.1.0.tar.gz.

File metadata

File hashes

Hashes for django-horizon-1.1.0.tar.gz
Algorithm Hash digest
SHA256 609d61c3766f3838eab78386c9973694704341ffd2a66a4a0e8ccfc9e3094edb
MD5 787511ea1b2087731648da7e29fcd42f
BLAKE2b-256 fa7cd0a75d17af8ff03e6aea65c90f1d20a0ceb89c0e045ca17cb198e04e4891

See more details on using hashes here.

File details

Details for the file django_horizon-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_horizon-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8eb12f89216cdebd75cb85f2f7f70ad218d4a8ff6d1e1fad5d5b4ca4e02dc913
MD5 35c82ac4cf94042d4bf594655b0f6877
BLAKE2b-256 464e5a267c80767baada0261b94e4e63067970b8728fb4b829168c8ae85c3673

See more details on using hashes here.

Supported by

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