Search code examples
rubyperformancepostgresqlamazon-web-servicessinatra

How can I speed up my AWS RDS Postgres performance?


I just set up a db.t2.micro instance on Amazon's AWS. I am using sinatra to load a localhost webpage. I am using Active Record to do maybe about 30~ queries and it's taking 92 seconds to load. It's extremely slow. I tried doing custom parameters as listed here: http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_BestPractices.html#CHAP_BestPractices.PostgreSQL

This didn't help speed anything up. I'm not sure how I can speed up this instance. This is my first time hosting a database. Any help would be appreciated.

When I run my sinatra app it host locally(localhost). Here is where the 30~ queries are taking 92 seconds to load. When I run select * statements in Postgres they take only a couple seconds.


Solution

  • The problem is the latency between you and Amazon's data center.

    For example, when you are in New York and your RDS instance is in Amazon's data center on the West Coast, then the latency between you and the data center is about 80-100ms. That means when your local application sends a query to the database, then it takes about 100ms before the database receives the query. To return the answer, it takes again an additional 100ms.

    That said: Assume a roundtrip including computation takes 300ms, and you have ~30 queries, then your application loses about 10 seconds doing nothing – just waiting for data being sent through the wire. And there are other factors that might slow down this even more: Big packets or lost packets (the server has to ask again), bad internet connections, wireless connections, the distance between you and the database being longer than my example.

    Therefore, the database should be as near as possible to the application server in the same data center to minimize latency.