postgresql 水平缩放 Postgres
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34831086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scaling Postgres horizontally
提问by user232343
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.
假设您在 postgresql 数据库上运行您的业务。一段时间后,您会收到大量流量,单个 postgresql 实例无法处理,因此您希望添加更多实例(水平扩展)以应对增长。
Your data is relational, so probably switching to some key/value solution is not an option.
您的数据是相关的,因此可能无法切换到某些键/值解决方案。
How would you do it with postgresql?
你会如何用 postgresql 做到这一点?
PS. Postgresql version: 9.5
附注。PostgreSQL 版本:9.5
回答by Dennis Anikin
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.
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.
如果是关于读取繁重的工作负载,那么您应该只添加副本。添加尽可能多的副本来处理整个工作负载。您可以以循环方式平衡副本之间的所有查询。
如果它是关于写入繁重的工作负载,那么您应该将您的数据库分区到许多服务器上。您可以将不同的表放在不同的机器上,也可以将一张表分片到多台机器上。在后一种情况下,您可以按主键的范围或主键的散列或什至按行垂直对表进行分片。在上述每种情况下,您都可能会失去事务性,因此要小心并确保事务更改和查询的所有数据都驻留在同一服务器上。