Search code examples
mongodbmongohqdatabase

Mongodb - Database hits


I want to have a guide section with a view counter. At the moment I worry about the performance. I would like to have it in real time so if someone views the page i want to increase the counter.

I worry because , what if i would have 1000000 views per day. So I would have to hit my db 1000000 times, to increase the counter. (only on one guide!)

I have problems to calculate the performance hit. Is this a big performance hit or a small performance hit to update an integer?

My workaround would be to cache the views and submit them only every x views. This would reduce the db hits by a factor of x. (for example x=100)

I am using mongohq.com at the moment and it seems that they have a flat pricing model, so I only pay for the storage and not for read/writes.

I am asking because I don't want to abuse their pricing model.

Edit:

Here is my code of incrementing the views count.

public static void increaseViews(String title) throws UnknownHostException,
            MongoException, DbAuthException {
        Datastore ds = DatabaseConnect.getInstance().getDatastore();
        Query<UserGuides> q = ds.createQuery(UserGuides.class).field("title")
                .equal(title);
        UpdateOperations<UserGuides> ops = ds.createUpdateOperations(
                UserGuides.class).inc("views");
        ds.update(q, ops);
    }

Solution

  • Disclaimer: I am a founder at MongoHQ

    It is a small performance hit to do this using the $inc operator. MongoDB has a nice way of doing just this.

    For example your query might look like:

    db.pages.update({page: "index"}, {$inc: {hits: 1}})
    

    Mongo can handle these very fast and will work well for what you are doing. If you get to where you are abusing a plan we will politely let you know ;)