Mongo db and java data versioning between code updates

I have an online service with multiple application servers with multiple collections stored in MongoDB. I'm working in continuous deployment mode, which basically means that code updates run automated tests followed by a production update if all goes well (this complicates things a bit, but the question matters for CD-less deployments as well, I suppose).

This works most of the time, but sometimes one (or more) of my main data models change, in which case the update can mess up the data in memory and after that in the db.

I'll give an example:

Let's say I have a simple data object:

public class User {
    private String id;
    private String name;
    private String[] friendsNames;
}

      

and now I decided to change User:

public class User {
    private String id;
    private String name;
}

      

and add friends as a separate collection that holds a simple object like:

public class Friend {
    private String name;
    private String friendUserId;
}

      

This leads to a problem. I cannot update my service before I change the data structure to fit the new data model, and I cannot change the data before I deselect the service, otherwise the old version will read the new version of the data and get messed up.

So the only solution is to bring it all down, run some update process on the db to change everything, and then put the service back in with new code running.

So finally the question: I was wondering if there was a best practice solution for these versions (specifically with Mongo, if relevant) so that old version apps can continue to work with old data, and newer apps "see" new data. I thought of something like "UserV1.1" and "UserV1.2" as versions of the classes that would look for a suitable version of the class in mongo, but don't want to "reinvent the wheel" if someone already thought of that and came up with clever decision.

To be clear, I don't need object history, I just want to be able to update versions of apps smoothly.

+3


source to share


1 answer


Welcome to the joy of being "sketchy". In my application, I ended up coding the objects so they could go through a "transition" period. That is, any release that changes the model must support both the old and the new "schema". I push the beat towards production and then start a long process that transforms everything. In the next installment, we postpone transitional logic. Its a pain in the ass, but it works. Changing the schema requires two releases.



+2


source







All Articles