Imagine a web backend where N amount of accumulators (numbers) are kept. The backend handles requests that contain increments over some variable number of accumulators.
Eg:
STARTUP------> A1=0 A2=0 A3=0 A4=0
REQ1: A1,A3 -> A1=1 A2=0 A3=1 A4=0
REQ2: A2,A3 -> A1=1 A2=1 A3=2 A4=0
REQ3: A1,A3 -> A1=2 A2=1 A3=3 A4=0
Rules:
- There are N number of accumulators
- A request contains M number of increments. Each increment corresponds to a different accumulator. With M > 0 and M < 10.
- Updates per request are atomic. All or none apply. For simplicity, let's assume that the increment can fail randomly.
- It has to support multiple increments of the same accumulator in parallel. That is, several requests may be attended to that wish to update the same accumulator(s) at the same time.
Let's ignore everything related to the web part and its configuration.
How would you write the algorithm reactively in this context that requires managing resource concurrency and request atomicity?.
NOTE: I solved it synchronously without major difficulties.
EDIT: The technology I tried for the reactive attempt: Spring boot reactive (project-reactor)
The reactive strategy, very briefly, was to use a concurrent map where the updates of each accumulator were chained in the form of promises (Sinks.One). Although this did not work finally