LLD for refer or referal code service

By Ravi Vishwakarma — Published: 18-Mar-2026 • Last updated: 19-Mar-2026 23

Below is LLD (Low Level Design) for Referral / Refer Code Service suitable for .NET / Java / Node / Microservice / MVC architecture.
This design is production-level and can be used in interview + real project.

1. Requirements (Understand first)

Functional Requirements

  • User can generate referral code
  • User can share referral code
  • New user can signup using referral code
  • Referrer gets reward / points / discount
  • Referral code should be unique
  • One user can use only one referral code
  • Referral code expiry support (optional)
  • Track referral history

Non-Functional Requirements

  • High performance
  • Unique code generation
  • No duplicate reward
  • Secure validation
  • Scalable

2. High Level Flow

User A registers
   ↓
Referral Code generated
   ↓
User B signup using code
   ↓
Code validated
   ↓
Referral record created
   ↓
Reward added

3. Database Design (Tables)

3.1 Users

Users
------
Id (PK)
Name
Email
Password
ReferralCode
CreatedDate

3.2 ReferralCodes

ReferralCodes
--------------
Id (PK)
UserId (FK)
Code
IsActive
ExpiryDate
CreatedDate

3.3 Referrals

Referrals
-----------
Id (PK)
ReferrerUserId
ReferredUserId
ReferralCode
RewardGiven
CreatedDate

3.4 Rewards

Rewards
---------
Id
UserId
Points
Type
CreatedDate

4. Class Design (LLD)

4.1 ReferralService

ReferralService
---------------
GenerateCode(userId)
ValidateCode(code)
ApplyReferral(code, newUserId)
GiveReward(referrerId)
GetReferralHistory(userId)

4.2 CodeGenerator

CodeGenerator
-------------
GenerateUniqueCode()

Example logic

REF + UserId + Random

Example

REF1023AB12

4.3 ReferralRepository

ReferralRepository
-------------------
CreateCode()
GetByCode()
SaveReferral()
GetUserReferrals()

4.4 RewardService

RewardService
--------------
AddPoints(userId)
AddCoupon(userId)
AddCashback(userId)

5. Sequence Flow (LLD)

Signup with referral

Controller
   ↓
ReferralService.ValidateCode()
   ↓
ReferralRepository.GetCode()
   ↓
ReferralService.ApplyReferral()
   ↓
RewardService.AddPoints()
   ↓
Save Referral

6. API Design

Generate Code

POST /api/referral/generate

Response

{
  code: "REF123ABC"
}

Apply Referral

POST /api/referral/apply

Request

{
  userId: 10,
  code: "REF123ABC"
}

Get History

GET /api/referral/history/{userId}

7. Code Example (.NET style)

Interface

public interface IReferralService
{
    string GenerateCode(int userId);
    bool ApplyReferral(string code, int newUserId);
}

Implementation

public class ReferralService : IReferralService
{
    public string GenerateCode(int userId)
    {
        return "REF" + userId + Guid.NewGuid().ToString("N").Substring(0,4);
    }

    public bool ApplyReferral(string code, int newUserId)
    {
        var referrer = repo.GetByCode(code);

        if(referrer == null)
            return false;

        repo.SaveReferral(referrer.UserId, newUserId);

        rewardService.AddPoints(referrer.UserId);

        return true;
    }
}

8. Important Interview Points

  • Idempotent reward
  • Unique code
  • Concurrency handling
  • Transaction required
  • Cache for validation
  • Rate limit
  • Expiry support
  • Fraud prevention

9. Advanced Design (Senior Level)

Add:

  • Redis cache for code
  • Message Queue for reward
  • Event driven reward

Microservice

ReferralService → Event → RewardService
Ravi Vishwakarma
Ravi Vishwakarma
IT-Hardware & Networking

Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.