简体   繁体   中英

Send user activities to external service from java spring microservice

I'm extending the backend (microservice) of a functioning webapp to add new functionalities. One change requested is the ability to log all user activities. I have already setup a logging facity leveraging slf4j, for this log however I need to send it to another service in json like
{activities : [{username: ..., activity_type: ...}, ...]}
The simplest way for me is to send one activity at a time after completing the API request that the front end calls.
My doubt is about performance since for every api call to my microservice I have to call the external one. Do you see problems in this solution?
Another idea I have is to batch all the activities of a single user per session (in memory) and send it to the service when the session is over, less calls, potentially less impact on the user but for this solution (if viable) I don't know where to start, can you give me pointers?
Also I've decided not to maintain these user activities information on the DB of this microservice since I have no use for them, is this a mistake? Any tips are appreciated.

I wouldn't complexify it too much. I would call the external API for logging asynchronously so that the callers of your API do not suffer from additional request time because you are calling another service in the meantime. This would be my initial approach for such a simple topic.

Adding an outgoing HTTP call for every such event can be cause performance issues, of course it also depends on the load of our service. You also need to think about how to handle errors (timeouts and non 2xx response) in a graceful manner.

Doing a batch update after a session could also be problematic. You will need to maintain a lis of events and make sure again to handle this list gracefully for all potential exception

To give some pointers to start with is to think of this as an audit logging pattern scenario, at least thats what I think of when I read the wanted feature:

One change requested is the ability to log all user activities.

Start with to understand how these logs should be used/read. Then try to find the simples solution. Eg you you could simple log all activities in a separate log file and configure your logging framework (eg log4j2 or logback) to use json as format.

Another approach could be to store the each activity in a separate database table. Then you can add an API to read this info or use some background process to synchronous with an external service.

As mentioned in another comment, you could also use a messaging system and send an event for every action.

Each approach has its own trade-offs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM