Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:37:53+00:00 2026-06-15T08:37:53+00:00

I have several servers running their own instance of a particular MySQL database which

  • 0

I have several servers running their own instance of a particular MySQL database which unfortunately cannot be setup in replication/cluster. Each server inserts data into several user-related tables which have foreign key constraints between them (e.g. user, user_vote). Here is how the process goes about:

  1. all the servers start with the same data
  2. each server grows its own set of data indepedently from the other servers
  3. periodically, the data from all the servers is merged manually together and applied back to each server (the process therefore repeats itself from step 1).

This is made possible because in addition to its primary key, the user table contains a unique email field which allows identifying which users are already existing in each database, and merging those who are new while changing the primary and foreign keys to avoid collisions and maintain the correct foreign key constraints. It works, but it’s quite some effort because primary and foreign keys have to be changed to avoid collision, hence my question:

Is there a way to have each server use primary keys that don’t collide with other servers to facilitate the merging?

I initially wanted to use a composite primary key (e.g. server_id, id) but I am using Doctrine which doesn’t support primary keys composed of multiple foreign keys so I would have problems with my foreign key constraints.

I thought about using a VARCHAR as an id and using part of the string as a prefix (SERVER1-1,SERVER1-2, SERVER2-1, SERVER2-2…) but I’m thinking it will make the DB slower as I will have to do some manipulations with the ids (e.g. on insert, I have to parse existing ids and extract highest, increment it, concatenate it with server id…).

PS: Another option would be to implement replication with read from slaves and write to master but this option was discarded because of issues such as replication lag and single point of failure on the master which can’t be solved for now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:37:55+00:00Added an answer on June 15, 2026 at 8:37 am

    You can make sure each server uses a different incrementation of autoincrement, and a different start offset:

    Change the step auto_increment fields increment by

    (assuming you are using auoincrements)

    I’ve only ever used this across two servers, so my set-up had one with even ids and one with odd.

    When they are merged back together nothing will collide, as long as you make sure all tables follow the above idea.

    in order to implement for 4 servers

    You would say, set-up the following offsets:

    • Server 1 = 1
    • Server 2 = 2
    • Server 3 = 3
    • Server 4 = 4

    You would set your incrementation as such (I’ve used 10 to leave space for extra servers):

    • Server 1 = 10
    • Server 2 = 10
    • Server 3 = 10
    • Server 4 = 10

    And then after you have merged, before copying back to each server, you would just need to update the autoinc value for each table to have the correct offset again. Imagine each server had created 100 rows, autoincs would be:

    • Server 1 = 1001
    • Server 2 = 1002
    • Server 3 = 1003
    • Server 4 = 1004

    This is where it does get tricky due to having four servers. For imagine certain tables may not have had any rows inserted from a particular server. So you could end up with some tables having their last autoinc id not being from server 4, but from being from server 2 instead. This would make it very tricky to work out what the next autoinc should be for any particular table.

    For this reason it is probably best to also include a column in each of your tables that records the server number when any rows are inserted.

    id | field1 | field2 | ... | server
    

    That way you can easily find out what the last autoinc value should be for a particular server by selecting the following on any of your tables:

    SELECT MAX(id) FROM `table` WHERE `server`=4 LIMIT 0,1
    

    Using this value you can reset the next autoinc value you need for each table on each server, before rolling the merged dataset out to the server in question.

    UPDATE information_schema.tables SET Auto_increment = (
      SELECT MAX(id) FROM `table` WHERE `server`=s LIMIT 0,1
    )+n WHERE table_name='table' AND table_schema = DATABASE();
    

    Where s is the server number and n is set to the offset, so in my example it would be 10.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several servers running the same code but each handling specific messages (they
Requirements We have several servers (20-50) - Solaris 10 and Linux (SLES) - running
We have several application servers, and a central monitoring server. We are currently running
Suppose I have an ASP.NET application running across several web servers behind a load
I have several instances of my project running on my server, like so: http://0.0.0.0/one
I have a server that has several virtual machines running on it. I'm trying
I have a php application that is installed on several servers and all of
Having to interact with several different email servers via IMAP (using javamail), I have
I have a singleton instance that is referenced throughout the project which works like
I have a project that requires us to maintain several MySQL databases on multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.