Inspired by a tweet from a friend this morning, I’ve taken on the challenge to write a quick blog post about this.
I believe it’s relatively simple to do a lot of things in a serverless way (I have a collection of serverless blog posts so go and look there), and a referral system is one of them.
In fact, the tweeter suggested that all they need is a “code” (assume referral code) and a “count” against that code.
Using AWS the easiest scalable way of storing this is DynamoDB. The table would have a schema with a Partition Key of “code”, and an attribute (not a sort key) of “count”.
Then you need a way of adding to that data. So you would create an AWS Lambda function (again, highly scalable) to be given a referral code, and then increment the count (if there is no item, then a new item should be created, with a count of 1).
Let’s call that function IncrementReferralCount.
And the final thing we’d like to do is get a count of how many referrals a code has, so let’s create a function for that (if there is no item, then return zero).
Let’s call that function GetReferralCount.
To make it accessible via the web, you could then create an API Gateway on AWS that allows you to access these functions via HTTPS. You would require a variable to be passed in called “referralCode” (or something like that). This is a little more convoluted than just passing through the data, but it’s relatively simple to do.
So once you have that, you can literally store a referral code and a count against it.
To think about costs, there are no servers to maintain (there are servers running Lambda, API Gateway and DynamoDB of course) and the costs are the costs of running those AWS services and the bandwidth which should be very low to zero. If you were doing this the “normal” way, you’d need code, servers, instances, databases, backing up etc… so this is definitely an improvement on that.
Refining the system
This is of course, open to issues. If the API call was ever discovered, somebody could game the system and add referrals, so you want to either make sure it’s only available via an IAM role (relatively simple via API Gateway) or some form of authentication (maybe Cognito here).
You may also want to see referrals over time, which might require a second table, with a “code” and a timestamp.
You might also wish to know if the code is valid which would require access to the referral code tables wherever they are.
There are also situations where you might want to limit amount of referrals (maybe to 10 a day) or something, so you would need a daily count, or a way of finding it (maybe the referrals over time would help here).
So this does get quite complicated to make more production useful, but if like the original tweeter you only want a code and a count, then this can easily be done via AWS Lambda.
Quick Update: There are many ways of doing a serverless system like this and many providers. I’ve picked Amazon AWS because that’s what I know, but equally there are similar services on Azure, IBM Bluemix and on Google services worth a look as well as other companies in this serverless nano-function space.