Search code examples
javaspring-bootapirestmicroservices

Using a GET request to persist items in database


I currently have a scenario with two java microservices, lets say StudentService and UserService. Both services persist data in respective nosql databases.

The client makes a GET api/v1/students/{id} to retrieve a particular student from the StudentService. In some cases, some entries for students in the Student table do not contain a field age. I currently retrieve age for this student from the UserService and return it to my client.

Although this seems quite straightforward and seems to work, I am not really a big fan of this solution because we could end up with a nested request for each GET request from the client(worst case).

The alternative I am thinking of is, when the StudentService cannot find age for a student, it retrieves age from UserTable and also adds this field in my UserTable. By doing this, next time the client calls GET api/v1/students/{id} for the same user, the UserService has everything it needs and I do not have to go to UserService again.

On the contrary, modifying contents of a Table with a GET request is not ideal.

Making major modifications to the structure of my Tables and services is unfortunately not possible because of legacy reasons.

How would you guys suggest I proceed?


Solution

  • Go ahead and cache the data you need in your table.

    HTTP does not attempt to require the results of a GET to be safe. What it does is require that the semantics of the operation be safe, and therefore it is a fault of the implementation, not the interface or the user of that interface, if anything happens as a result that causes loss of property (money, BTW, is considered property for the sake of this definition). -- Fielding, 2002

    Safe, here, means "essentially read-only". It means that general purpose components (like a web crawler) can submit the request on their own, without needing to worry about underlying cost. It means that a user agent can "pre-fetch" the resource before the user decides to click on the link.

    Your server's making changes to its underlying data store is an implementation detail, it doesn't change the semantics of the request.