4

I have multiple instances of a microservice A running that does nothing but dump data in a database and provide it from there to other services.

I am using spring's CrudRepository interface to automatically get a bean which has a couple of sql methods like save() implemented.

So a microservice B would send via a load balancer a serialized object to save to one of the instances of A which then saves it an oracle database (shared by all instances of service A). Do I have to be careful when 2 instances of A try to access the oracle db at the same time?

Also, what would happen if I have only 1 instance of A and 2 instances of B which try to send serialized objects to A at the same time? Would one of them have to wait automatically or do I have to control that somehow?

Thanks for the help

1
  • what would happen if I have only 1 instance of A and 2 instances of B which try to send serialized objects to A at the same time? - in this case if objects are different then can be save parallelly as insert operation does not lock any other row in Oracle. Commented Aug 28, 2018 at 17:24

1 Answer 1

3

Before digging into the details; I just want to make it very clear that enterprise databases like oracle are designed to do hundreds of things simultaneously... so doing just one thing from your app at a time is definitely not necessary. Similarly, micro-services are used to handle thousands of request a second in parallel in many places.

Do I have to be careful when 2 instances of A try to access the oracle db at the same time?

This completely depends on the operations you are running against the database and how you are executing them. If you use database transactions, even complex multi-statement workflows can be isolated/safe when done in parallel (its very normal actually).

But... if I sent two parallel requests against your micro-service(s) and it resulted in two threads (regardless of which app) trying to create the same object in the same table, then one of them would beat the other one and the other would fail (assuming your database has proper constraints).

This could even happen with one instance of your service A if you allow it to have multiple database connections (which is likely if you're using a library).

So... if you have multiple instances of the micro-service talking to the DB, you probably can't easily control making sure they don't try to do the same thing at the same time, so you must have defensive coding to detect and handle when duplicate data is sent to the DB, etc. Also, you must be aware that the DB state may change due to an app outside of this one and reload data if you cache anything periodically.

Also, what would happen if I have only 1 instance of A and 2 instances of B which try to send serialized objects to A at the same time?

Generally this would be normal/expected. The idea is you spawn up more copies of your micro-service as they are required due to load. Normally micro-services handle multiple requests at a time whether it is by REST, sockets, etc.

So, the requests wouldn't wait... unless you purposely coded your one instance of A to have a single database connection and to handle requests in a queue. But again, that's down to your personal implementation details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.