Monday, January 20, 2014

Load Balancing and Failover in JDBC Drivers


When you have to connect your java application to a database in a database cluster, using a JDBC driver, you can simply connect to one of the nodes of the cluster. (For example, if it's a MySQL cluster you can connect to one of its SQL nodes.) But what happens if that node goes down? You lose the connectivity to the cluster and high availability aspect too which is one of main intents of having a cluster. Connecting to a single node makes a single point of failure. How do we avoid this?

If we can achieve failover aspect, we can avoid above situation. And fortunately most of the JDBC drivers support failover (and load balancing too). Intent of this post is to list load balancing/failover supported JDBC URLs of some popular JDBC drivers.

MySQL


jdbc:mysql:loadbalance://<host_1>,<host_2>,...,<host_n>/<dbname>?loadBalanceBlacklistTimeout=5000

Here, it is load balanced among given hosts. If a node is down while you're trying to connect, JDBC driver retries to connect for the given time period  (loadBalanceBlacklistTimeout). If the node is still down it tries to connect to another node. That's how failover is achieved by this.

SQL SERVER (Microsoft JDBC driver)


jdbc:sqlserver://<host_1>:1433;DatabaseName=<dbname>;failoverPartner=<host_2_for_failover>:1433

Here it supports only failover. If the active node is down, it connects to the failover (passive) node.

POSTGRE SQL 


jdbc:postgresql://<host_1>:<port_1>,<host_2>:<port_2>,...,<host_n>:<port_n>/<dbname>

Please comment if you know URLs of any other JDBC Driver.

No comments:

Post a Comment