I currently have two microservices. We'll call them A
and B
.
The database under microservice A
has the following table:
A
|-- users
The database under microservice B
has the following table:
B
|-- trackers
The requirements state that users
and trackers
have a many-to-many relationship.
I'm not sure how to properly handle this within a microservices architecture.
I could see this working one of three ways:
- A
user_trackers
table is added to microserviceA
. This acts similar to a join table containing "foreign keys" tousers
andtrackers
. - A
owners
table is added to microserviceB
. This table acts similar to a polymorphic join table. This would allow any service to create an association with a tracker. This may look somewhat like this:B |-- trackers |-- owners |-- owner_id |-- owner_type |-- tracker_id
- Keep records for
users
andtrackers
in each microservice. Keep them in sync with some sort of pubsub system.
I was originally going to go with option 2 because I liked that it preserved transaction boundaries. I can create a tracker and associate it with something atomically. However, it seems out of scope for microservice B
. Why should microservice B
care that microservice A
wants to create an association?
I feel like there's probably a good pattern here that I'm not aware of. Do any of the options I laid out make sense? Is there another option that may make more sense?