0

I would like to implement a click counter using MongoDB (e.g. user clicks a link, count the total clicks).

My intuitive approach is to have an in-memory low priority thread pool that will handle a blocking queue of click messages and persist it to MongoDB in the background asynchronously.

So my question is - does MongoDB's native Java Driver have some async capabilities that do just that?

If it doesn't, is there an alternative Mongo driver that might has benefits over rolling my own async code?

Eran Medan
  • 44,555
  • 61
  • 184
  • 276

1 Answers1

1

Well, not really async but if you use a WriteConcern of NONE it is sort of async in that you only get the data into the socket's buffer and the insert returns. The down side is that you won't know if the insert worked or not. In the face of a failure you could silently drop a lot of clicks.

There is an Asynchronous Java Driver that allows you to use futures or callbacks to get the results of the insert. Going that approach there should be no need to roll your own queue or have a background thread (the driver has its own receive threads).

HTH - Rob.

P.S. Full disclosure - I work on the Asynchronous Java Driver.

Rob Moore
  • 3,343
  • 17
  • 18