I would like to know which pattern is recommended to work with counter representing the number of message processed, with an application that should be stateless.
For example, in an architecture where an application is deployed on several servers, a database is used to store the persistent information (session, etc...). However, such information are not exposed to concurrent updates like a message counter would be. In a mono-instance application we could use singleton, but this is not the case here.
What would be your suggestion to implement a such counter ? Is using a counter a bad design ?
I do not think there is some pattern to do this very abstracted task. Also:
Using a singleton DOES NOT guarantee data safety in a multithreaded system. You need some synchronisation primitives. The very simple is a critical section.
If all of your applications should update some counter, it looks reasonable to refactor this... "counter" management into a microservice and use critical section inside microservice to update resource. In this case in each given moment in time you are guaranteed to have only one thread updating counter.