lundi 20 avril 2015

Can I batch incrementing values in order to reduce database writes?

My web app uses Django on Heroku.

The app has two components: a banner serving API and a reporting API.

Banners served by the app contain an ajax call to the app's reporting API. This call is made once per impression. The purpose of this is to record how many impressions each banner gets each day.

Reporting API abridged source:

def report_v1(request):
    '''
    /report/v1/?cid=

    - cid= the ID of the banner
    '''

    creative = get_creative(creative_id = request.GET.get('cid'))
    if not creative:
        return HttpResponseNotFound("404 creative with that ID not found")

    day = timezone.now()

    fact, created = CreativeFact.objects.get_or_create(
        day = day,
        app = app,
        creative = creative,
        defaults = {
                'impression_count': 0,
            }
        )

    fact.impression_count += 1

    response = HttpResponse("200 OK")
    response["Access-Control-Allow-Origin"] = "*"

    return response

My problem: Is there any way to avoid writing to the database on every single impression? I'm currently tracking hundreds of thousands of impressions this way, and expect to track millions soon, and I think this is inefficient.

Aucun commentaire:

Enregistrer un commentaire