I have an application that can be reduced/simplify to this flow:
user sends request to app A
app A inserts info about the request and user into DB ( marked as "B in progress" and "C in progress")
app A pushes the data into queue and returns to user
app B retrieves data from queue and process it
app B finishes processing the data and marks record in DB as "B done"
app C retrieves data from queue and process it
app C finishes processing the data and marks record in DB as "C done"
In other words, user sends request to app, app saves the record to the database and send it to queue, app B and C takes request from queue and process it ( each app does different thing but requires data from request ) and when they are done i want to mark the request in db as done for both APP.
This can be achieved, if all apps share DB. However sharing the DB like this between microservices is considered anti-pattern.
What are some design patterns to solve this? Am i really left with only option - make app A expose rest API and call the endpoint from app B and C to update the row in DB?
Thanks for help!
This more sounds like choreography , event driven process. Instead of DB, did u consider using Kafka where status gets enriched at each publish.
Related
I am developing a Rest API using spring boot.
This rest api will be deployed on weblogic application server.
using Java 8
I am in the design phase so do not have any demonstratable code .
The API will receive a Request payload ( to approve Orders ) .
This Request payload can vary from 1 to 100000 OrderIds
For each of these orderIds I need to call a stored procedure in Oracle to approve the order.
The stored procedure has the business logic and it could take anywhere between a few seconds to minutes to respond back with a response.
To not keep the end user waiting am planning to implement this using "DeferredResult" from spring.
To achieve this I will be spawning off database call in a new thread using :
ForkJoinPool.commonPool().submit(() -> {
//do database call here
}
However I am not clear about how to control the number of threads spawned ?
Should I be worried about subsequent requests ( which I have no control over ) which could also lead to more threads being spawned ?
Евгений Кравцов:
I develop some tiny service with http backend and android app. And recently i felt the lack of knowledge in such a systems.
Case:
- Client makes order in app and send request to server
- Server successfuly recieved data and make a database row for order
- After database work completes, backend tries to respond to App with 200 success code.
- App user faced some internet connection problems and can not receive server response. App gets timeout exception and notify user, that order was not successful
- After some time, internet connection on user device restored and he send another request with same oreder.
- Backend recieves this again and create duplicate for previous order
So i got 2 orders in database, but user wanted to create only one.
Question: How can i prevent such a behavior?
You should add a key to your table in the db, for example defining the key as compound key of user_id, type - so specific user cannot make 2 orders with the same order type.
This will cause the second db insert to fail.
Is there an elegant way to subscribe to updates only after I trigger first update of my data using rabbitMQ?
Or.. Is there a way to know when a new consumer is added and trigger sending the data?
For example:
Service A is getting updates from Service B (using rabbitMQ, Service B pushes IPs that I need to send the data to).
Service A is also getting requests from Service C and sends each request to all the IPs from Service B.
My problem is when Service A is up, there might be 1-5 minutes until Service B pushes an update. Meanwhile, Service C can send 100 requests, and I'll have no ips to send these requests ...
If the queue could have known that a new consumer is added - we could trigger sending all the ips..
I hope it explains my problem.
Any help would be very appreciated.
I have four db servers with the same db structure but different data.
Currently when new data are inserted to database my application get this data, create template and send email.
I would like to separate sending email from my applications.
For example some thread which will start once per 10 minutes. It selects data from my four db servers, connect to mail server and send email to users.
It's possible with using JMS or something similar ?
Thanks for replys !
I did the same by creating a mail table (likely one per DB) and save the Template and data (or subject/body) in it. A separate process could be Quartz or your own pooling thread read that table and connect to mail server and send email and update email status.
In this way you can check status of any email at any given time and even you can resend any email. The table needs to purge/archive after some time may be after 1 day or 1 week depends on table size.
I have 2 application,
1 as A application and
2 as B application.
Now from A , I am navigating to b application, there I will spend some time. And In B I have a log off button, if user clicks on that, it should come back to application A, with session intact.
I am using J2EE and Weblogic server, Here servers of a and b are also different.
Can any one please help me, I need to complete this work by today eveining.
Thank you for your help in advance.
Here is one way of doing it
Assume that a user is on application A with valid session.
When you click a link (or post some data) to go on application B, pass some token in query string. (This token may encrypted (username+password+salt)).
Application B receives the query string data, decrypts it and authenticates the user.
When user clicks log off in application B, the log off handler in application B (it could be a servlet/JSP/Controller/Action etc), does s response.sendRedirect() to the application A.
which will still have its session intact (provided session has not timed out i.e. the time user spent on application B is less than the session timeout of application A).