Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A subscription model #34

Open
harshraj22 opened this issue Nov 27, 2022 · 5 comments
Open

A subscription model #34

harshraj22 opened this issue Nov 27, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@harshraj22
Copy link
Owner

harshraj22 commented Nov 27, 2022

Add a subscription model. An existing user can have one of the following subscription tiers:

  • Free (30 requests per 24 hour)
  • Basic (150 requests per 24 hour)
  • Advanced (500 requests per 24 hour)

The limit on the number of API calls by a user should be limited by the type of subscription they own.

@harshraj22 harshraj22 added the enhancement New feature or request label Nov 27, 2022
@harshraj22
Copy link
Owner Author

harshraj22 commented Dec 12, 2022

Can use reddis to implement it from scratch[1], [2] or use a third party library.

Read More: nordicapis

# docker run -p 6379:6379 --name redis_server redis:4-alpine
import redis

r = redis.StrictRedis(host='localhost', port=6379, decode_responses=True, password="")
print('Ping:', r.ping())
print(r)
print('setting:', r.set("key", "value"))
print('getting:', r.get("key"))

import time

username = 'user1'

WINDOW_LENGTH = 2 # in Seconds
for _ in range(3):
    cur_time = time.time()
    r.zadd(username, {cur_time: cur_time})
    time.sleep(1) # Sleep for 1 Second

p = r.pipeline()
p.zremrangebyscore(username, 0, time.time() - WINDOW_LENGTH)
cur_time = time.time()
p.zadd(username, {cur_time: cur_time})
p.expire(username, WINDOW_LENGTH)
p.execute()

print(r.zrange(username, 0, -1))

@harshraj22
Copy link
Owner Author

harshraj22 commented Dec 12, 2022

A potential Architecutre:

image

Might have to deal with Distributed Commit 😨

The three Major components for this would be:

  • Consistency across DBs
  • Rate Limit On user request based on subscription tier they belong to
  • Integration of the payment gateway

@harshraj22
Copy link
Owner Author

harshraj22 commented Dec 28, 2022

Read about saga pattern and 2 phase commit

Another helpful stackoverflow thread.

@harshraj22 harshraj22 pinned this issue Jan 6, 2023
@harshraj22
Copy link
Owner Author

harshraj22 commented Jan 6, 2023

The subscription based Rate Limiting should be developed as a separate and completely independent identity, which is easy to plug in into any existing product. Is should not be tightly coupled with existing product.

A possible Architecture:
Subscription Rate Limiter is a library that accept details to connect to existing Redis and MySql Server. It has methods to update the MySql and Redis server and Query them. It should be made in such a way that Replacing MySql Server with any other database is easy.

Similarly Data Monitoring Service can also be separated out into an independent project.

@harshraj22
Copy link
Owner Author

Flow for Rate Limit On user request based on subscription tier they belong to:

  • Payment microservice exposes and endpoint to update the redis cache from the mysql database.
  • A short lived microservice updates the redis cache once the payment microservice is up
  • The Subscription microservice starts, and uses the cache to rate limit based on the number of requests and the subscription tier the user belongs to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant