0

I put a load balancer in front of example.com and I believe all traffic for *.example.com goes to the load balancer

My database is named db.example.com and listening at 5432

Application Load balancer has port setting for 80 and 443 only..

How do I direct db.example.com traffic to the ec2 instance which listens to 5432?

  • EDIT

I have single DB, I don't need load-balance db, just need to reach it.

eugene
  • 139
  • 1
  • 9

2 Answers2

0

You typically can't load balance a database with a simple connection based load balancer and no additional design. If you try to do this your writes will go to a random database, and they will go out of sync. Maybe it could work if your databases have synchronisation set up, but it needs proper thought and design rather than just throwing a load balancer in front of a database. You haven't described any design in this area so I assume it's not been considered.

Your best options that I know of (and I'm not a database expert) are:

  • Relational Database Service with read replicas. Your writes all go to the master, your reads can be manually distributed to the slaves.

  • Use database native features. I'm not sure if you can use RDS for this. Oracle has good features in this area, at a price.

Note that RDS multi-AZ is not for scale, it's for reliability.

I did a quick search and found that you can use HAProxy or ProxySQL for MySQL.

Tim
  • 30,383
  • 6
  • 47
  • 77
-2

I presume the DB must be accessed by the app servers only and is a behind the scene thing. App servers can connect directly to db server and it does not need to go through load balancer. How many db servers you have - single or multiple ?

  • I have just 1 db – eugene Nov 28 '19 at 18:02
  • If its single DB, why you want to point it to the load balancer. Your app servers must have connection strings and there you can directly provide internal IP for the db to be accessed. – Abhishek Mishra Nov 28 '19 at 18:13
  • well, it might deserve a separate question, but here 's why, webservers which need to connect to the db is managed by ecs, (so I can't control their ips) and I don't want to open DB for public, just want to allow ecs-ec2 instances. only way I know is have DB's security group allow webserver's security group. And this doesn't work if webserver connects to DB directly.. – eugene Nov 28 '19 at 18:22
  • Then you can create a separate TCP Load Balancer in front of your DB server and allow only app server instances to connect to this. – Abhishek Mishra Nov 28 '19 at 18:51
  • 1
    I can't use ALB because it only supports http/https and postgresql needs tcp right? – eugene Nov 28 '19 at 19:43
  • correct. TCP will work – Abhishek Mishra Nov 29 '19 at 06:46
  • You could improve this answer by making it so that people don't have to read the comments to find what they need – Miles Jul 07 '20 at 21:46