July 10, 2017

Counter-Badging Service Architecture

Most of the applications have some sort of badging functionality which is used to display certain counts to the users for CTA (Call To Action).

Once the user clicks or acknowledges the counter, the value associated with the counter will need to be reset.

Given a system with large number of users and a large data set, it's not always possible to compute the counter values in real time. Counts in the database world are aggregation operations and would need to perform a whole table scan.

An alternative approach to doing count operation is to keep track of the counts in separate fields/columns in the database. This means that application has to perform two separate operations, one to store the data related to an action and another to increment/decrement/clear the counter fields/columns.

Like explained above the three main actions associated with the counters are :

  • 1. Increment
  • 2. Decrement   - This usually happens when an action is undone or something is deleted.
  • 3. Reset       - When user acknowledges the counter.

All the above actions could happen concurrently and be associated with a user. For this reason, all the operations on the counter needs to be atomic otherwise the counter values would be off and not accurate.

A common example for the badging/counter feature is the Facebook notification system.

The counters associated with the user are based on the actions performed by other users.

 

A user can be associated with several other users and many of those other users could be performing the same or different actions at the same time.  Like, there could be 3 people sending the user messages or another 4 people sending friend requests.

 
Facebook notifications
Facebook Notifications

Given the above behavior of the system, the badging system can be designed as an event driven system. This would make the counter values eventually consistent but will free up the core application from performing any counter related operations. This also provides an opportunity to setup a separate database/store for tracking counters separately.

Individual services would generate events which would get listened to by an events processor service. Based on the data in the event, the processor will update the value of counters associated with the user.

Badging / Counter Service Architecture
Simple Badging/Counter Service Architecture


Here is a simple example of the REDIS HASH data structure which can be used to store the counters associated with the user. Each HASH is associated with a userId and all the counters are tied to the hash as key-value pairs.

Badging Redis Structures
Badging Redis Structure