## High concurrency counters without sharding

Sharded Counters are a well known technique for keeping counters with high update rates on App Engine. Less well known, however, are some of the alternatives, particularly in areas where you want to keep a reasonably accurate counter, but absolute accuracy isn't required. I discussed one option in this cookbook post - be sure to check the comments for an improved version - and today we'll discuss another option, which also makes use of memcache and the task queue.

The basic assumption is this: We want to keep as accurate a count as possible, but we're willing to accept that it may, in some cases, under-count. A good example of where this is true is counting downloads, or hits, or other such metrics.

Our solution has three major components:

1. A 'permanent' count, stored in the datastore.
2. A 'current' count, stored in memcache.

In order to implement this, we'll take advantage of the task queue's task name functionality, and 'tombstoned tasks' - the restriction that two tasks with the same name cannot be enqueued within a reasonable period (at least a week) of each other. Each time we want to update the counter, we increment the current value in memcache, and attempt to queue a task that will store the memcached total to the datastore at the end of the interval. The tradeoff, here, is the length of time we will cache the data for: The longer it is, the less load we put on the datastore, but the higher the risk of losing data from memcache before it's stored.

Let's start by defining the counter itself, and the code to get its current value:

```class Counter(db.Model):
count = db.IntegerProperty(required=True, default=0)

@classmethod
def get_value(cls, name):
"""Returns the value of the counter with the specified name."""
counter = cls.get_by_key_name(name)
if counter:
count = counter.count
else:
count = 0
count += memcache.get(name, cls.kind())
return count```

The model itself has only one property - the count - and the method we've defined to get the current value retrieves the relevant instance - if it exists - and adds its value to the value stored in Memcache. One possible optimisation here would be to cache the counter entity in local memory - we know it's safe to do so at least until the end of the current time interval, however long that may be.

To define the update part of the code, we need a couple of prerequisites: We need a way to generate task names, such that there is one unique one for every interval of a given length, and we need to write the actual function that will copy the count from memcache to the datastore. First things first:

```def get_interval_number(ts, duration):
"""Returns the number of the current interval.

Args:
ts: The timestamp to convert
duration: The length of the interval
Returns:
int: Interval number.
"""
return int(time.mktime(ts.timetuple()) / duration)```

A lot of verbiage for not much code! All we're doing here is converting a timestamp to a unix time - which is the number of seconds since the Unix Epoch - and then dividing it by the duration, giving us the number of 'intervals' since the epoch. Now for the second prerequisite: The 'flush' function:

```  @classmethod
def flush_counter(cls, name):
counter = cls.get_by_key_name(name)
if not counter:
counter = cls()

# Get the current value
value = memcache.get(name, cls.kind())

# Subtract it from the memcached value
memcache.decr(name, value, cls.kind())

# Store it to the counter
counter.count += value
counter.put()```

The observant amongst you may notice a couple of potential issues here. First up, we're not doing the update in a transaction. We're relying on the fact that this record is only updated in the task queue, and thus is unlikely to get clobbered by another concurrent update. It's also possible that the memcache.decr could succeed, but the counter.put() could fail, leading to under-counting. We've already stated we're willing to accept occasional under-counting, though, and this is a rare circumstance.

Finally, we can define the counter's increment method:

```  @classmethod
def incr(cls, name, interval=5, value=1):
"""Increments the named counter.

Args:
name: The name of the counter.
interval: How frequently to flush the counter to disk.
value: The value to increment by.
"""
memcache.incr(name, value, cls.kind())
interval_num = get_interval_number(datetime.datetime.now(), interval)
task_name = '-'.join([cls.kind(), name, interval, interval_num])
try:
{"ft_lang_label":"__label__en","ft_lang_prob":0.867508,"math_prob":0.8861264,"size":5125,"snap":"2020-10-2020-16","text_gpt3_token_len":1127,"char_repetition_ratio":0.15055653,"word_repetition_ratio":0.0,"special_character_ratio":0.22321951,"punctuation_ratio":0.14874372,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.961711,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-04-03T16:07:45Z\",\"WARC-Record-ID\":\"<urn:uuid:d530cc03-ea09-4350-9d8e-d6fbe77a8cc9>\",\"Content-Length\":\"11395\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ba123b65-b9a1-4223-8ec5-2f09af614d85>\",\"WARC-Concurrent-To\":\"<urn:uuid:1032b530-cb6d-4b09-93d1-786df044c072>\",\"WARC-IP-Address\":\"172.217.13.83\",\"WARC-Target-URI\":\"http://blog.notdot.net/2010/04/High-concurrency-counters-without-sharding\",\"WARC-Payload-Digest\":\"sha1:KZ7GX5AZMAMBDRPGAIESHHBOI54JM3FT\",\"WARC-Block-Digest\":\"sha1:IYUIDKL5CLIXOMJHCHMIFY4WHRPBLS6G\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370515113.54_warc_CC-MAIN-20200403154746-20200403184746-00210.warc.gz\"}"}