Search

Combining Many Customer Databases into One

Brandon Beacher

2 min read

Nov 7, 2011

Combining Many Customer Databases into One

Keys

Here at Highgroove we like a good challenge, and we love it when we can meet a challenge with a simple and elegant solution.

Recently we had a customer with a great problem to have – they had signed up lots of new customers and were experiencing some growing pains.

A typical Ruby on Rails app has one database. This app had one database per customer.

Routine tasks like launching servers, deploying code, or migrating databases were taking much longer than normal because they had to be performed per customer instead of just once.

What this app needed instead was Multitenancy – a fancy word for combining the customers into one database.

To combine safely, we’d need to ensure each customer’s unique keys no longer were in conflict with each other.

Here is an example of unique keys before the merge:

Customer A     Customer B

1

Bob

1

Frank

2

Alice

2

Eleanor

And after the merge:

Customer A & B

1

Bob

1

Frank

2

Alice

2

Eleanor

You can see the unique keys are no longer unique.

How do we fix this? Time to look at some code:

This bit of code will give us the maximum id across all tables in all customer databases.

Once we know the maximum id, we can reassign each customer’s unique keys with enough offset to ensure there are no conflicts:

This will give us the combined table:

Customer A & B

3

Bob

customer_a

4

Alice

customer_a

5

Frank

customer_b

6

Eleanor

customer_b

Now we’re off to the races – with a single database containing records for all of our customers – and no unique key conflicts!

Have you ever had to combine conflicting database records? How did you tackle the problem?

Steve Sparks

Reviewer Big Nerd Ranch

Steve Sparks has been a Nerd since 2011 and an electronics geek since age five. His primary focus is on iOS, watchOS, and tvOS development. He plays guitar and makes strange and terrifying contraptions. He lives in Atlanta with his family.

Speak with a Nerd

Schedule a call today! Our team of Nerds are ready to help

Let's Talk

Related Posts

We are ready to discuss your needs.

Not applicable? Click here to schedule a call.

Stay in Touch WITH Big Nerd Ranch News