Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Horizontally Scale Your Postgres Database Using Citus

DZone's Guide to

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.

· Database Zone
Free Resource

Download the Guide to Open Source Database Selection: MySQL vs. MariaDB and see how the side-by-side comparison of must-have features will ease the journey. Brought to you in partnership with MariaDB.

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

Interested in reducing database costs by moving from Oracle Enterprise to open source subscription?  Read the total cost of ownership (TCO) analysis. Brought to you in partnership with MariaDB.

Topics:
citus ,postgres ,horizontal scaling ,database ,tutorial

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}