All Projects → harikitech → django-horizon

harikitech / django-horizon

Licence: MIT license
Simple database sharding (horizontal partitioning) library for Django applications.

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to django-horizon

Mosaic Contracts
Mosaic-0: Gateways and anchors on top of Ethereum to scale DApps
Stars: ✭ 119 (+54.55%)
Mutual labels:  sharding
Kahip
KaHIP -- Karlsruhe HIGH Quality Partitioning.
Stars: ✭ 180 (+133.77%)
Mutual labels:  sharding
Octopus
Database Sharding for ActiveRecord
Stars: ✭ 2,496 (+3141.56%)
Mutual labels:  sharding
Wotrd Cloud
基于nacos包含网关、认证授权、服务注册、发现、断路降级、限流、配置中心、分库分表等基础组件
Stars: ✭ 135 (+75.32%)
Mutual labels:  sharding
Redis Resharding Proxy
Redis Resharding Proxy
Stars: ✭ 168 (+118.18%)
Mutual labels:  sharding
Star Zone
⭐星座空间App(社交类App)项目源码,包括Java后台、Android、管理后台的代码。自主研发了数据库分表、token权限认证
Stars: ✭ 189 (+145.45%)
Mutual labels:  sharding
Dble Docs Cn
Documents for dble in Chinese
Stars: ✭ 80 (+3.9%)
Mutual labels:  sharding
TONBUS
Telegram Open Network(TON)中文社区。Telegram 群组:https://t.me/ton_china
Stars: ✭ 15 (-80.52%)
Mutual labels:  sharding
Rekord
A javascript REST ORM that is offline and real-time capable
Stars: ✭ 171 (+122.08%)
Mutual labels:  sharding
Activerecord Turntable
ActiveRecord Sharding Plugin
Stars: ✭ 206 (+167.53%)
Mutual labels:  sharding
Ton
Telegram Open Network research group. Telegram: https://t.me/ton_research
Stars: ✭ 146 (+89.61%)
Mutual labels:  sharding
Mongo Cluster Docker
Docker compose config for mongodb cluster
Stars: ✭ 165 (+114.29%)
Mutual labels:  sharding
Sharding Method
分表分库的新思路——服务层Sharding框架,全SQL、全数据库兼容,ACID特性与原生数据库一致,能实现RR级别读写分离,无SQL解析性能更高
Stars: ✭ 188 (+144.16%)
Mutual labels:  sharding
Lealone
极具创新的面向微服务和 OLTP/OLAP 场景的单机与分布式关系数据库
Stars: ✭ 1,802 (+2240.26%)
Mutual labels:  sharding
Shardingsphere Elasticjob Cloud
Stars: ✭ 248 (+222.08%)
Mutual labels:  sharding
Redis Tools
my tools working with redis
Stars: ✭ 104 (+35.06%)
Mutual labels:  sharding
Django Sharding
A sharding library for Django
Stars: ✭ 184 (+138.96%)
Mutual labels:  sharding
eris-fleet
Cluster management for Discord bots using the Eris library.
Stars: ✭ 38 (-50.65%)
Mutual labels:  sharding
routing4db
Simple Database Routing and Shard Framework
Stars: ✭ 16 (-79.22%)
Mutual labels:  sharding
Pyquarkchain
Python implementation of QuarkChain
Stars: ✭ 194 (+151.95%)
Mutual labels:  sharding

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 Documentation Status Updates Python 3

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())
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].