Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling Postgres horizontally

Let we say you are running your business on top of postgresql database. After some time you get so much traffic that it cannot be handled by single postgresql instance, so you want to add more instances (scale it horizontally) to be able to handle growth.

Your data is relational, so probably switching to some key/value solution is not an option.

How would you do it with postgresql?

PS. Postgresql version: 9.5

like image 900
user232343 Avatar asked Jan 16 '16 19:01

user232343


People also ask

Can PostgreSQL scale horizontally?

With Amazon Relational Database Service (Amazon RDS) for PostgreSQL, you can scale a database instance vertically or horizontally. You implement vertical scaling by changing the DB instance type or size (for example, from M to R or from xlarge to 2xlarge), and scale horizontally by creating read replicas.

How do you scale a database horizontally?

How do you scale a database? Databases are scaled either vertically (by adding more resources to existing machines) or horizontally (by adding more machines, distributing data, and processing across those machines).

Is horizontal scaling better?

Increased performance - If you are using horizontal scaling to manage your network traffic, it allows for more endpoints for connections, considering that the load will be delegated among multiple machines.

How do I scale in PostgreSQL?

There are two main ways to scale our database… Horizontal Scaling (scale-out): It's performed by adding more database nodes creating or increasing a database cluster. Vertical Scaling (scale-up): It's performed by adding more hardware resources (CPU, Memory, Disk) to an existing database node.


1 Answers

  1. If it is about read-heavy workload then you should just add replicas. Add as many replicas as you need to handle the whole workload. You can balance all the queries across the replicas in the round robin fashion.

  2. If it is about write-heavy workload then you should partition your database across many servers. You can put different tables on different machines or you can shard one table across many machines. In the latter case you can shard a table by a range of the primary key or by a hash of the primary key or even vertically by rows. In each of the cases above you may lose transactionality, so be careful and make sure that all the data changed and queried by a transaction be resided on the same server.

like image 164
Dennis Anikin Avatar answered Sep 21 '22 12:09

Dennis Anikin