Coupon Engine

vijay
Airtel Digital
Published in
4 min readMay 8, 2018

What is Coupon Engine ?

Coupon engine is distributed system through which we can distribute coupons to the users and handle its complete coupon journey. Coupon engine gives flexibility to the business to release new exciting offer for the users which will attract the users toward the company.

Technical Details

Coupon Engine Flow Diagram

Components of coupon engine

Segmentation Engine : — We record all the activities about every Airtel user in this system. Activites are recorded in form of events which could have been performed on MyAirtel App, other digital platofrms and in offline mode. The key role of this engine is to divide the users into different segments based on different user events in real time. This engine pushes user’s data along with its segment into solace queue which in turn is being consumed by Coupon Engine.

Coupon Engine : — Segment of the user is being stored in AerospikeDB and offers corresponding to a segment in MongoDB. Then we run various kind of filters on the coupons based on the user coupon history. Finally, the system generates the user coupons with proper information.

Complex parts : -

  1. To maintain the count of user coupons that are remaining in the mongo and to show the correct count to the user.
  2. Maintain all user coupon states.

Payment Systems :- We have 2 payment systems — APB(Airtel Payments Bank) and PCIDSS stack which in turn talks to different Payment Gateways. Through PCIDSS stack user can make payment through credit card, debit card, netbanking, UPI and other payment modes.

Whenever user starts the transaction we notify our payment system that transaction has been started, and internally we lock the user coupon. On transaction callback, an action (unlock/availed) is taken based on the transaction state (success/failure). The user coupon is unlocked if transaction fails else the user coupon is marked as availed. In case, transaction goes in pending state, coupon remains in blocked state until unless Coupon Engine Service does not receive final sucess/failure status of transaction.

Segment/Transaction Message Consumer :- It consumes message from solace queue at real time. These messages are then processed and stored in the AerospikeDB. These segments are then used further for many different kind of exiting offers.

Why Solace queue — Solace is enterprise grade hardware based queue. It provides the fastest, most robust, and most easily manageable messaging backbone around. It supports all kinds of messaging, along with popular protocols and APIs.

Notification Service :- Whenever we consume messages from solace queue, an event is send to the notification engine. It is then used to send push notification and notify the users regarding the exiting offers.

Database Choice

What is stored in MongoDB?

  1. Segment to offer/coupon mapping
  2. Coupon meta data
  3. User to coupon mapping
  4. User coupon status history
  5. User transaction states

Why we used MongoDB for this?

MongoDB is a highly consistent storage. It is a CP system where C stands for Consistency and P stands for Partition Tolerance. It also supports complex queries to read data.

We wanted to always get consistent view of user coupon details even if there is any sort of system failures. Hence we stored all this information in Mongo.

What is stored in AerospikeDB?

  1. User to segments mapping

Why is AerospikeDB used for segmentation storage?

Aerospikes provides high write throughput performance and high availability. It prioritises availability over consistency. It means that during network brain split or failure scenerios, Aerospike does not stop writing to the database. It is referred to as AP systems where A stands for Availability and P stands for Partition Tolerance.

We get new segment from Segmentation engine at high rate. We also needed to store the segment of 400 million Airtel customers. This required a high through put system. During failure, if we do not get to view the segment of few users, it would be fine with us. Hence we went ahead with Aerospike.

Interaction between different microservices

Coupon engine interacts with APB service, payment system, segmentation engine through events. Events are pushed to solace queue. Then event consumer reads them in order and marks the coupon status accordingly.

Our Coupon Engine service is eventually consistent. for example, there could be an case where we receive failure from payment system but payment goes through during reconcilliation. The user coupon is being blocked until we receive an update regarding payment success or failure. If we receive payment success, the coupon is marked as availed else in case of payment failure, the coupon code is marked as unblock.

--

--