By using PostgreSQL on EC2 save 40% to 60% cost over AWS RDS PostgreSQL

Rupesh Desai
Amazon Web Services
6 min readOct 17, 2021

--

AWS RDS PostgreSQL vs. EC2 Instance for Postgre

In this article, we are going to explore the pros and cons of setting up a PostgreSQL Server on the RDS instance vs. the EC2 instance.

RDS provides a robust platform that is highly scalable and completely managed by AWS. You will not need to worry about scaling quickly, based on your demand, or backing up your data.

On the other hand, the EC2 instance is completely unmanaged by AWS and gives complete control of the server and the packages such as PostgreSQL in the hands of the user.

These are the following common reasons for someone to setup Postgre on EC2 instance vs. using the managed AWS RDS services:

  • The cost of managed instances of RDS is significantly higher than running self-managed PostgreSQL on EC2. Additionally, you can use the server to do multi-tasking too, while in RDS you don’t have access to the instance itself. You can use the AWS calculator to estimate the latest cost at https://calculator.aws/
  • You do not have the expertise to self-manage and maintain the database instance, or don’t have an army of experts that can troubleshoot critical issues when occur, hence you use the RDS instance.

Here are the top 10 reasons why you should install and use PostgreSQL on EC2 vs RDS

  1. Cost: The cost of hosting an EC2 instance and installing PostgreSQL is at least 40% to 60% lower than the cost of running an RDS instance. Additionally, you can turn off the EC2 instance and you won’t incur the cost of the same. In the case of the RDS instance, if the instance of RDS is turned off, it will automatically turn on after 7 days. Hence if you are not watching the instance closely, you may get a bill at the end of the month. If you want to quickly spin up an EC2 instance with PostgreSQL, check out my post on Setting up Postgresql on Ubuntu EC2 Server instead of using RDS
  2. Backup: For a long time, the RDS instance has provided a beautiful feature called Snapshots that automatically backs up the data. Don’t worry, while this can be automated too on the EC2 instance, and you can store the backup in S3. In fact, older backups can be moved to Glacier. You can also create incremental backups for optimizing the same, or do a full backup of the DB as needed. So if backup, is holding you back to use EC2 for PostgreSQL, you can follow the step-by-step instructions for setting the backup on EC2. Creating backup and restoring PostgreSQL on EC2 Server. Just note, that creating and saving backups becomes an added responsibility that you need to self-manage.
  3. Availability: The RDS instance is highly available as they create automatic replicas of the DB instance, this feature doesn’t add to your overall cost on RDS. Additionally, there is multiple A-Z deployment where it is highly scalable and available across multiple regions. This feature is difficult to replicate on EC2 instances. Honestly, if you have a peak season traffic spike like Black Friday or Cyber Monday, you can create a replicated EC2 instance and keep them in sync and use load balancers to manage the traffic, but if you want extremely high availability and mission-critical DB access like banking service, you are better off using RDS.
  4. Security: Whether it is RDS or EC2, you still get state-of-the-art AWS security with IAM that you can leverage to ensure that your data is safe and secured. The loopholes in the security are vulnerabilities that are mostly caused by developers who don’t know how to manage security, with very little opportunity for hackers to actually hack the database. The data itself on EC2 is encrypted on the EBS volume as well as during transit to EC2. Understanding the fundamentals of AWS security is key to setup a safe and secured environment for your development.
  5. Ease of Setup: RDS is simply going to win this battle when it comes to the ease of setting up the database, as it is completely managed by AWS. On the other hand, EC2 for PostgreSQL is definitely not rocket science. For small projects, I would strongly recommend setting up DB on the EC2 instance. Use this step-by-step guide Setting up Postgresql on Ubuntu EC2 Server instead of using RDS
  6. Scalability: This is another battle that RDS will simply swoop and win. With multiple A-Z deployment, read replicas, and multi-tenant setup, scaling RDS DBs are a breeze. On the other hand, you will have to do sharding, and scaling yourself on EC2. This task of managing DB at scale should be left to DB Admins. If you don’t want to invest in DB Admins, simply choose RDS and pay the cost.
  7. Storage: Both EC2 and RDS provide a flavor of fast read/write storage. For more details, you can simply go to storage details for EBS vs. RDS to get higher IOPS (Input/Output per second). I have not tested, which one is faster or better. In fact, the only time you need to really think about IOPs speed is when you are building a very heavy transactional DB with at least 10K+ transactions / per second. For standard DB application and support, I wouldn’t want to bring a cart before the horse. More importantly, you will have challenges on a number of simultaneous connections over reading/write issues as you grow and continue to scale.
  8. Performance: While RDS offers PIOPs (Provisioned IOPs), for small to medium size applications, both RDS and PostgreSQL on EC2 provide fairly good and comparable performance. In fact, if you connect the EC2 instance over localhost within the network, or via VPC subnet, you will see at least 30% to 50% faster performance when loading data and doing any complex CRUD operations. The smarter way to increase performance is to write queries directly into a memory DB like Redis for all common UI components and access through Redis instead of hitting the DB every time for each user.
  9. Control: It is debatable, while EC2 is the barebone control of your server and seems more open, for the purpose of DB management, you have almost similar tools to manage RDS instances too. For example, you can use pg_dumpall DB, but that impacts your current performance of the DB, on the other hand, Snapshots doesn’t have any impact on the RDS instance and literally has no downtime.
  10. Monitoring: RDS provides out-of-the-box CloudWatch monitoring that can be enabled for an additional price. However, there are many tools assuming you are willing to pay for monitoring the PostgreSQL.

Summary

The decision of using the RDS instance vs. the EC2 instance for PostgreSQL is based on whether you are ready to invest the time and effort to set up and manage the DB yourself.

For all developers that are setting up the environment, I strongly recommend using the EC2 instance to save cost.

Unless, you are willing to learn or have a team of Database Administrators, who are already proficient in managing and scaling DBs, simply use RDS instead of taking a chance on the live application as you will increase failure points.

Hope this article is useful for you!

--

--

Rupesh Desai
Amazon Web Services

Blogger, Entrepreneur, Data Analyst, Developer, Designer, Linear Creative, FitnessFan