Multitenant Database using Hibernate Shards

In my previous post “How I Introduced Multitenant Architecture In my Web Application” I discussed database isolation as one of main requirements in a Multitenant SaaS application. So what are the reasons for data isolation? They are obvious and to lay them down.

  1. Security – The user does not want to mingle their data with their competitors.
  2. Scalability – There are many users, for example billion users in my webtop application :). So to deal with gazzilion bytes of data in a table and also to keep database sanity intact you need to chop down the table rows in smaller chunks.
  3. Combination of other two – Most of the users don’t mind their data coexisting with other users however there still are some fussy customers. 

If you feel I left out points then please leave a comment or two. I will update my post.

Whatever be your reason once you decide to separate user data data you have to think about the ways by which you can achieve this. Once again I will lay down the approaches (Ok. I have again stolen the points from google, big deal.)

  1. Separate database instance – Each tenant has his own database instance.
  2. Separate schema but same database instance – Each tenant has his own schema or sets of table in a single database instance.
  3. Separate Rows – The database instance is same same however rows of tenant data is partitioned using some kind of discriminator (tenant_id for example).

Each approach has their own pros and cons. Kindly check the table compiled in postSaaS – Multi-Tenant Database Design Options. It compares the various approaches under different user requirements.

If you had to make your decision then you could easily choose The separate database instances for each tenant approach, but you know that it could prove costly in long run and also add to maintenance and administrative headache. So what to do now? Don’t worry there is a middle path which will help you counter all these. Can you tell me? No? Not to worry I am here for the rescue. Here is my fourth Multitenant database approach. 

  1. Sharding – http://en.wikipedia.org/wiki/Sharding

Sharding is a mechanism by which data of a table is horizontally partitioned across multiple database instances. Each of the independent database partition is called a Shard. Concept of Sharding is different than the Horizontal Partitioning. In horizontal partitioning the data in a single table in a single database instance is partitioned across rows. Meanwhile in sharding the horizontal partitioning is achieved across multiple database servers. The shards are not aware of each other and work independently with each other. They can be present in any database server in any data-center anywhere in the world. So now look at advantages of using this approach.

  1. The total number of rows in each table are lesser now. Hence the size of index hence the performance of queries improve.
  2. Due to sharding not only the problematic table is spread across multiple database other table are also replicated across multiple database instances hence overall load on a database is reduced hence improving performance. In other words we have a database cluster each serving its own bunch of clients and each of which can be scaled in it own way.
  3. We can also get one shard for each tenant using this approach.

Ok, since we have the best approach (I know nosql cassandra etc lovers will be gunning for my head, but let me warn you I am very strong, I got super powers) now let us put our heads down and try to figure out how to implement sharding in your own cool application. But first let me pen down the approach once more

Multitenant database approach by horizontally partitioning data across multiple database instance each having identical schema.

 I had some portion my webtop already written and I had used Hibernate as my ORM tool. It took a long time to get my database layer designed in a way I liked so I decided to keep it. Hence  I decided to give a try to hibernate shards. Hibernate shards is not in active development however it is still a very robust piece of code. I thought it was better to use it rather than writing a utility right from scratch. I did a proof of concept by splitting data across three shards. Each installed in three separate database servers in my office, I really loved what I saw so I will share it with my fans ;).

Just FYI – Hibernate 4 will be released in next few months with full Multitenant support embedded with its core API. https://hibernate.onjira.com/browse/HHH-5697 . I am happy as I am sure that I can easily re-factor my shards code using latest API.

Print Friendly, PDF & Email

About Niraj Singh

I am CEO and CoFounder of a startup "Aranin Software Private Limited, Bangalore. I completed my graduation in 2002 as an Aerospace Engineer from IIT Kharagpur. I love working on new ideas and projects and recently released my first open source project JaiomServer "http://jaiomserver.org". I have 9 years of experience in IT industries most of which I have spent in developing community applications for various clients using java. Some of the sites in which I have actively involved with are hgtv.com, food.com, foodnetwork.com, pickle.com, diynetwork.com etc.
This entry was posted in Cloud Computing, HIbernate, Hibernate Shards, Multitenancy, SaaS and tagged , , , , , . Bookmark the permalink.

20 Responses to Multitenant Database using Hibernate Shards

  1. Earn up to $265 within next 12H Answering Simple Surveys
    Hi weblog4j.com admin just wanted to let you know

    There’s an incredible new opportunity
    that just grabbed my attention!

    It’s almost too good to be true. Big
    companies are paying people just for giving
    their opinions! I can’t believe this guy and others are cashing out money just for filling out simple surveys read here how they are doing this:

    http://get-surveys-for-money.net

    That’s right! I’m not kidding, all you’ve got
    to do is complete simple surveys and these
    companies will pay you fat cash for it and it doe not mater form which country you are, they need people from all over world!

    You’ve got to see this.

    http://get-surveys-for-money.net

    But the truth is… they’re not letting
    very many people join the program so
    you’ve got to hurry!

    See you there!
    Mike

  2. Pingback: googlezerlunited

  3. Danilo says:

    Congratulations !!!

    I liked alot this post.
    Hibernate Shards was something that i was looking for a good explanation and i was not finding it.
    Now i find it =)

  4. seo vps says:

    Check out my seo blog 🙂 http://goo.gl/AtwWB

  5. I did find some notions about sharding strategies, but it seems like you really need to find out what’s gonna your the best for your data set. In the week I spent with Shards, I was able to see many different ways of handling sharding data – it really comes down the data itself.For re-balancing, I think why Hibernate passes on it is that it’s not a trivial operation, and perhaps they felt it better to defer to either a DBA team or a future implementation. It also gets weird because hibernate needs to know the shard id entity identifier to load an object, so live re-sharding could get weird. They do have the notion of virtual shards, however, which should be able to alleviate some of the pain.

  6. Incredible story there. What happened after? Take care!

  7. johnny says:

    This blog is great. I realy love it!

  8. markos says:

    Thanks for this post, I am considering talking about the same in my blog.

  9. vps says:

    This post is amazing. I realy like it!

  10. It’s not an easy task to build relationships your possibilities crowd. But efficient approaches one can get shocked to find out many followers on-page. Little bit every day will communicate with the folks and will steer someone to view large leads to lover adhering to. Coming from more different options for instance profiles, images in addition to practical information about the business enterprise, it becomes easier expressing about the solution and also providers fo the organization.

    If you want to get started with a great web page in your case to test out for guides could be
    internet marketing courses
    internet marketing orange county
    internet marketing specialists
    internet marketing expert

    To save cash for your obtain, it can be recommended to purchase this enthusiasts in the company that gives non-targeted admirers. All these followers less complicated easier to find and also provide high quality final results at the lowest price ranges ever. This implies, if you are looking for the opportunity to increase your small business, you can do so by means of Facebook fans plus likes. Resources: Mark could be the author as soon as i’ve about .
    Discover details, in relation to in this article Buy Facebook Likes,Improve
    Facebook Such as, Acquire more Google+ Readers. Increase Facebook Likes having fblikesupply.

    By this manner, you can hold in intellect elements this sort of as funds and superior at the similar
    time. Now you can have your very own . By Simply click
    internet marketing company india
    internet marketing guru
    targeted internet marketing
    internet marketing plan

    Facebook has brought everyone suddenly along with from now on appears to have much
    more compared to 400 1,000 targeted traffic. Aloof from these sorts of mass of clients about 50% of which log-in
    just about every day time. The particular statics proves that customers having the ability to view Facebook 600 trillion mins month to month.
    This can be a very best online community internet site at the regional, across the country, & global sizing.

    Social advertising and marketing gets a huge factor all over the on the internet area
    of interest intended for companies. Each are bound to have witnessed or perhaps lose out-it’s as fundamental as in which. A lot better design and style makes more effective customer contribution, which actually creates inside increased gross sales.

    My blog internet marketing website
    internet marketing website recently posted…internet marketing websiteMy Profile

  11. prabakar says:

    Amazon stuffs are very sound to get my basics.. Thanks…

  12. Dan Martin says:

    Your blog is pretty cool check out mine! http://flik.us/blog

  13. site says:

    You actually make it seem so easy with your presentation but I find
    this topic to be really something that I think I would never understand.
    It seems too complex and extremely broad for me.
    I am looking forward for your next post, I’ll try to get the hang of it!
    site recently posted…siteMy Profile

  14. Vetement says:

    Hello, I enjoy reading all of your article post.
    I like to write a little comment to support you.
    Vetement recently posted…VetementMy Profile

  15. Dorie says:

    hello there and thank you for your information –
    I have certainly picked up something new from right here.
    I did however expertise some technical points using this website, as I experienced
    to reload the website lots of times previous to I could get it to
    load correctly. I had been wondering if your web host is OK?
    Not that I’m complaining, but slow loading instances times will very frequently affect your placement in google and can damage your high quality score if ads and marketing with Adwords. Well I am adding this RSS to my email and could look out for much more of your respective fascinating content. Ensure that you update this again soon.
    Dorie recently posted…DorieMy Profile

  16. vijayrahul says:

    hi,

    i found your blog very helpful.

    i have implemented shards in my project..the problem i was facing is that while fetching the data i am able to get data from all the different sharded databases.but i am unable to fetch from a particular database from the sharded.

    • Niraj Singh says:

      Hi Vijay,

      Thanks for your comment. Can you give some detail regarding your problem? For practical purposes your sharded database appears as a single database to your hibernate shard. It revolves the parameters of session factory depending on id of the object you want to pull. And the mapping between id and shard is defined by the ShardSelectionStrategy that you implement. Suppose you want that object with id f(n) should be stored in shard n then you have to define that mapping in ShardSelectionStrategy. Once this is done to your liking you can use the shards as one single database without the worry of connecting to a specific shard.

      We can discuss some more if you can tell the details of your problem?

      Regards
      Niraj

Comments are closed.