After Hours Academic

Question 13: Accurate count

Marie has built app for garage parking spaces. Whenever a car enters the garage, a process running on a server at the garage gate puts an ENTER message in a queue. Similarly, whene a car exits the garage, the process puts an EXIT message in the queue. A consumer of the queue reads messages from the queue and maintains a count of the number of cars in the garage.

Unfortunately, the queue only provides an at-least once guarantee. That is, it guarantees that any message sent will not be lost, but it does not guarantee that there will only be a single copy of the message in the queue.

She wants to ensure that she does not double count (or double subtract) any of the cars entry (or exit, respectively). Can you suggest a mechanism to ensure this?

Solution

Marie needs to implement some idempotency mechanism to avoid double additions (or subtractions). A common solution is to have the sender (in this case, the server at the garage entry and exit) add an idempotency number to each message. The receiver should log (say in a database) the idempotency number for each of the request it processes. Before processing a request, the receiver should check whether it has already processed a request with the same idempotency number. If so, the receiver can infer that it is a duplicated message and ignore it.

A common choice of idempotency numbers is a randomly generated UUID. In this particular case, Marie can even use the car's license plate number as a unique idempotency identifier for a car entering or exiting the garage. The only additional requirement is that once a car exits the garage, the receiver would need to remove the idempotency identifier for the car (i.e., its license plate number) from the list of its processed messages for the cars that entered the garage. Otherwise a re-entry, following an exit, would also appear as a duplicate message.

AWS has a good blog on idempotency mechanisms here.

#computer-science #idempotency #qna