---
title: "LLD for refer or referal code service"  
description: "Below is LLD (Low Level Design) for Referral / Refer Code Service suitable for .NET / Java / Node / Microservice / MVC architecture."  
author: "Ravi Vishwakarma"  
published: 2026-03-18  
updated: 2026-03-19  
canonical: https://answers.mindstick.com/blog/101/lld-for-refer-or-referal-code-service  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 3 minutes  

---

# LLD for refer or referal code service

Below is **LLD (Low Level Design) for Referral / Refer Code Service** suitable for **.NET / Java / Node / [Microservice](https://www.mindstick.com/blog/302264/ways-to-deploy-microservices) / MVC [architecture](https://www.mindstick.com/articles/249193/top-5-reasons-to-pursue-architecture)**.\
This design is [production](https://www.mindstick.com/news/2276/by-the-end-of-2023-tesla-cybertruck-mass-production-will-begin)-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](https://www.mindstick.com/blog/300788/why-is-heart-attack-increasing-in-the-younger-generation)
- No duplicate reward
- Secure [validation](https://www.mindstick.com/articles/43/validation-controls-in-asp-dot-net)
- Scalable

## 2. High Level Flow

```plaintext
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

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

## 3.2 ReferralCodes

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

## 3.3 Referrals

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

## 3.4 Rewards

```plaintext
Rewards
---------
Id
UserId
Points
Type
CreatedDate
```

## 4. Class Design (LLD)

## 4.1 ReferralService

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

## 4.2 CodeGenerator

```plaintext
CodeGenerator
-------------
GenerateUniqueCode()
```

Example logic

```plaintext
REF + UserId + Random
```

Example

```plaintext
REF1023AB12
```

## 4.3 ReferralRepository

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

## 4.4 RewardService

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

## 5. Sequence Flow (LLD)

### Signup with referral

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

## 6. API Design

## Generate Code

```plaintext
POST /api/referral/generate
```

Response

```plaintext
{
  code: "REF123ABC"
}
```

## Apply Referral

```plaintext
POST /api/referral/apply
```

Request

```plaintext
{
  userId: 10,
  code: "REF123ABC"
}
```

## Get History

```plaintext
GET /api/referral/history/{userId}
```

## 7. Code Example (.NET style)

### Interface

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

### Implementation

```cs
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](https://www.mindstick.com/interview/23464/what-is-concurrency) handling
- [Transaction](https://www.mindstick.com/blog/125/sql-server-transaction) required
- Cache for validation
- Rate limit
- Expiry support
- Fraud [prevention](https://www.mindstick.com/articles/167600/how-to-test-for-legionella-and-implement-prevention-plans)

## 9. Advanced Design (Senior Level)

Add:

- [Redis cache](https://answers.mindstick.com/blog/189/implement-redis-cache-in-asp-dot-net-core-api) for code
- [Message Queue](https://www.mindstick.com/forum/159260/how-do-correct-message-queue-in-c) for reward
- Event driven reward

Microservice

```plaintext
ReferralService → Event → RewardService
```

---

Original Source: https://answers.mindstick.com/blog/101/lld-for-refer-or-referal-code-service

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
