DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Horizontally Scale Your Postgres Database Using Citus

How to Horizontally Scale Your Postgres Database Using Citus

Citus 6.1 hopes to bring scalability to the reliability of Postgres. You can share data across tenants, resource allocation, and view support for distributed tables.

Craig Kerstiens user avatar by
Craig Kerstiens
·
Mar. 20, 17 · Tutorial
Like (4)
Save
Tweet
Share
10.58K Views

Join the DZone community and get the full member experience.

Join For Free

Microservices and NoSQL get a lot of hype, but in many cases what you really want is a relational database that simply works and can easily scale as your application data grows. Microservices can help you split up areas of concern, but also introduce complexity and often heavy engineering work to migrate to them. Yet, there are a lot of monolithic apps out that do need to scale. If you don’t want the added complexity of microservices, but do need to continue scaling your relational database then you can with Citus. With Citus 6.1, we’re continuing to make scaling out your database even easier with all the benefits of Postgres (SQL, JSONB, PostGIS, indexes, etc.) still packed in there.

With this new release, users are able to scale from single node Postgres to horizontal linear scale. Citus 6.1 brings several improvements, making scaling your multi-tenant app even easier. These include:

  • Integrated reference table support
  • Tenant Isolation
  • View support on distributed tables
  • Distributed Vaccum / Analyze

All of this with the same language bindings, clients, drivers, libraries (like ActiveRecord) that Postgres already works with.

Give Citus 6.1 a try today on Citus Cloud, our fully managed database-as-a-service on top of AWS, or read on to learn more about all that’s included in this release.

Reference Table Support: Sharing Data Across Tenants

Applications that are B2B fit smoothly into a model of sharding by customers. This means your customer only interacts with their own data, and all that data can be automatically co-located together — giving you all the power of SQL while still maintaining flexibility. Still, in some cases, you may have smaller lookup or reference tables that don’t make sense to distribute. This could be something like a list of countries or an order status table. These type of tables don’t have the same large write volume as tables you’ll want to shard, but may have a relationship to them still. As of today, you now have cleanly defined APIs to create these tables. To create a reference table, you’d first create your table then run the function to distribute it:

-- a reference table
CREATE TABLE states (
  code char(2) PRIMARY KEY,
  full_name text NOT NULL,
  general_sales_tax numeric(4,3)
);

-- distribute it to all workers
SELECT create_reference_table('states');


Under the covers, when interacting with this table, we perform a two-phase commit (2PC) to guarantee all nodes are in a consistent state when you write/update/delete from it. This allows you to have the same data consistency guarantees as you would on a single node Postgres, without having to build extra logic into your application.

Tenant Isolation: More Resources for Bigger Tenants

When you choose to distribute your tables, multiple customers or tenants will live in the same table. Each shard is essentially just a Postgres table, under the hood. This means that if you were to shard something like a pageviews table by customer, you would have 32 tables: pageviews_001, pageviews_002, etc. When you then insert data in your application, you would insert into a normal pageviews table, and based on a hash of the customer_id, it would be placed in the appropriate table.

For most data distributions, this works well. Data at a large enough scale that you need to shard it tends to smooth out across your distributed tables —you’ll have some customers with larger datasets and some with smaller ones contained within one table. But in some cases, you may want even more isolation to improve performance. That's now trivial — you can isolate a specific tenant. To isolate your very large tenant, you’d run the following, which includes the table you want to isolate as well as the id of the tenant:

SELECT isolate_tenant_to_new_shard('table_name', tenant_id);


Once run, the above will split the shard it was in into three shards. One table will contain all the lower range hash values, this tenant will live in its own table, and another table will be for the higher range hash values. The new shard is created on the same node as the shard from which the tenant was removed. For true hardware isolation, you can then move that shard to a separate node if you see fit. And you can do this while keeping your database up and running.

View Support on Distributed Tables

With Citus 6.0, we saw a number of customers start to leverage Citus for distributed roll-ups of data to help power real-time analytics. Once using this, we heard requests to make it easy to create various views across the entire dataset to make it easier to query within your application. Now when you create a view across a distributed table, that view is automatically propagated to all of your distributed nodes.

Distributed Vaccum/Analyze

Being able to scale out all the resources your database needs — not only storage but also memory and compute — is at the core of how Citus is able to give significant performance gains to applications over single node Postgres. On the compute side, there are a lot of tasks that may not happen every transaction but do happen routinely, such as creating indexes or vacuum. Vacuum is one operation that can be quite intensive within Postgres. In Citus 6.1, Vacuum is now distributed and run in parallel automatically across your cluster

Database Relational database PostgreSQL

Published at DZone with permission of Craig Kerstiens, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Importance of Delegation in Management Teams
  • Taming Cloud Costs With Infracost
  • Remote Debugging Dangers and Pitfalls
  • Event Driven 2.0

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: