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.
Join the DZone community and get the full member experience.Join For Free
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_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.
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
Published at DZone with permission of Craig Kerstiens, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.