Friday, May 22, 2015

Database Sharding with Liferay

Liferay portal allows hosting completely unrelated portals using a feature called Portal Instances. Using Liferay control panel we can create multiple portal instances. Each portal instance will have its own users, sites, organizations, user groups and so on. Internally portal instance is also referred as company. Liferay keeps company id in almost all tables to distinguish data based on portal instance. If many portal instances are created and each portal instance has huge amount of data then at certain point performance of database will deteriorate because of huge amount of data. In this situation it will be a good idea to partition the data based on instance id or company id. This technique to distribute rows of table in multiple databases is called Database Sharding. Liferay portal supports this feature. This blog will explain how to use Database Sharding feature with Liferay 6.1.

Lets learn how to configure database sharding with an example. In our example scenario we want to create three portal instances. We want to store data of each portal instance to separate database. It is assumed that MySQL is installed on local environment and MySQL Driver jar is copied in tomcat lib directory. This steps refer Liferay 6.1 CE GA2 version.
Here are the steps to configure database sharding on local Liferay environment.
Step 1 Database sharding feature in Liferay works with portal instances as explained above. So on Local environment we will need to create multiple portal instances. To create multiple portal instance, we need to have multiple domains. On local environment we can add multiple domains by editing hosts file. So add following entries in hosts file.

127.0.0.1       domain1.com
127.0.0.1       domain2.com
Above step maps local host ip to two domains. By default liferay creates one portal instance and it is mapped to localhost. So we will create two more instances and map them with above domains respectively.
Step 2 Add following properties in portal-ext.properties file.

#SHARD 1 Configuration
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/shard1?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root
#SHARD 2 Configuration
jdbc.one.driverClassName=com.mysql.jdbc.Driver
jdbc.one.url=jdbc:mysql://localhost/shard2?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.one.username=root
jdbc.one.password=root
#SHARD 3 Configuration
jdbc.two.driverClassName=com.mysql.jdbc.Driver
jdbc.two.url=jdbc:mysql://localhost/shard3?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.two.username=root
jdbc.two.password=root
#Spring configuration files to be loadded. By adding shard-data-source-spring.xml in the list database sharding feature
#can be enabled
spring.configs=\
        META-INF/base-spring.xml,\
        \
        META-INF/hibernate-spring.xml,\
        META-INF/infrastructure-spring.xml,\
        META-INF/management-spring.xml,\
        \
        META-INF/util-spring.xml,\
        \
        META-INF/jpa-spring.xml,\
        \
        META-INF/executor-spring.xml,\
        \
        META-INF/audit-spring.xml,\
        META-INF/cluster-spring.xml,\
        META-INF/editor-spring.xml,\
        META-INF/jcr-spring.xml,\
        META-INF/ldap-spring.xml,\
        META-INF/messaging-core-spring.xml,\
        META-INF/messaging-misc-spring.xml,\
        META-INF/mobile-device-spring.xml,\
        META-INF/notifications-spring.xml,\
        META-INF/poller-spring.xml,\
        META-INF/rules-spring.xml,\
        META-INF/scheduler-spring.xml,\
        META-INF/scripting-spring.xml,\
        META-INF/search-spring.xml,\
        META-INF/workflow-spring.xml,\
        \
        META-INF/counter-spring.xml,\
        META-INF/mail-spring.xml,\
        META-INF/portal-spring.xml,\
        META-INF/portlet-container-spring.xml,\
        META-INF/staging-spring.xml,\
        META-INF/virtual-layouts-spring.xml,\
        META-INF/shard-data-source-spring.xml,\
        META-INF/ext-spring.xml

Step 3 Now start the Lifeary portal server and access using http://localhost:8080. Liferay will show setup wizard to configure the portal. After you signed in to the portal if you check user_ table in shard1 database you will find the test user. Which means 1st portal instance is mapped to default shard.

Step 4 Now add two portal instances from Server Administration | Portal Instances section of control panel. While adding the instances make sure you use domain1.com and domain2.com as web id. Here are the sample screenshots for adding portal instance.


No comments:

Post a Comment