We have a legacy system (app 1) that has millions of records of an object Project
. It has ProjectId
and OrganizationId
columns along with other properties. A recently added service (app 2) gets the same object created again in its database as part of some migration. The new object will be created with slightly different schema. Let's say it has ProjectKey
and TenantId
along with other properties. There are cases when project
is deleted in the older application but its counter-part in new application still exists. These dead records need to be cleaned up.
Sample Legacy App's Database - DB1 (SQL):
ProjectId | OrganizationId |
---|---|
1 | Org1 |
2 | Org1 |
3 | Org1 |
7 | Org2 |
8 | Org2 |
9 | Org2 |
Sample Modern App's Database - DB2 (PostgreSQL):
ProjectKey | TenantId |
---|---|
1 | Org1 |
2 | Org1 |
3 | Org1 |
4 | Org1 |
5 | Org1 |
6 | Org1 |
7 | Org2 |
8 | Org2 |
9 | Org2 |
10 | Org2 |
11 | Org2 |
12 | Org2 |
From DB2, records with ProjectKey
s 4, 5, 6, 10, 11, 12 need to be deleted.
I'm working on a cleanup tool that will be executed periodically. The two databases from two completely different applications are really huge with millions of records. And the stale records can also be in the millions.
I wanted some inputs on the best approach to handle this. The periodic task that cleans up the dead records shouldn't put too much load on the databases.
The newly added application has an API to get all projects of a given TenantId
. I've to do a similar all query on the legacy application and then do a diff of the two lists. But I'm worried of this diff operation when the two lists are very big.
Also, the second application that gets projects of a TenantId
does so in batches.
Here are some of the ways I've thought of:
- Get all records from modern app (list2) and legacy app (list1) and then do a list2 minus list1 to get the stale records and call delete API of the modern application to cleanup.
- Get records from modern app in batches and call a stored proc in DB1. Let DB1 do the diff and give out the
ProjectId
s that are missing. This stored proc will be called numerous times until all theProjectKey
s from app 2 are checked in DB1. My concern was why should DB1 intake a parameter from another DB and do a diff?
Should the identification of missing projectId
s happen in a stored procedure of the DB1 or would it be better to do it in the tool?
Or are there any other more efficient ways to design this?