Database – Keeping data between two databases in sync

databasesynchronizationweb-development

Let's say I have a forum application running, and I start building a separate web application with a Python web framework. Both applications use their own MySQL databases, which contain their own respective user account tables. What would be the best approach to keeping the users between the databases in sync? I want to make it so that I can login with the same information on both applications.

Best Answer

Direct answer to your question might not do you much good but it boils down to using triggers if your databases are on the same server, or using your account creation logic to remotely trigger creation on the other application as well. However, I'd like to stress that syncing data can lead to likelier data losses unless some robustness mechanisms are in place (scheduled synchronization, repeated attempts at triggering the creation, etc).

Almost always a better approach would be to maintain a single source of truth. However, this does not avoid complexity either.

Here are some approaches you could go for:

Assumptions

Given the formulation of your question, I'm going to assume all of the user accounts should be replicated on both applications. E.g. there are no app-specific user accounts.

Approach #1: same database server, separate database

A straightforward approach would be creating a third database that contains shared data for both applications, in this case the user accounts. You could reference the database from your usual queries, since it's all on the same server. (If we assume you're using Django, on your User model you'll need to specify the database. To do this, you'll have to look into replacing the user model for the default auth system which is more of a hassle than I'd like to admit)

Approach #2: separate authentication service

You could maintain a separate application (a service, to be precise) which would be queried over a secured connection to prove the authentication claims. This would introduce significant complexities, as you'd have to authenticate your sessions manually, which is somewhat tedious as you might be used to it being automated by popular Python frameworks.

A more worthwhile option might be looking into third party authentication services such as https://auth0.com. You can find Python documentation here

Disclaimer: I am not affiliated nor experienced with Auth0 services, they're just given as an example.

Related Topic