1

Whats the best way to check whether user paid the subscription or not?. I want to check whether the user paid for monthly subscription, if not, I'll redirect him to payment page.

My solutions :

Using JWT : whenever user login, I write next_payment_date (checking from databse) to jwt and sign it. so every time users makes request to protected routes (routes which can be accessed only for paid users), i decode the token and check next_payment_date and act accordingly

Problem with this method : Lets say user is using phone and computer.In computer he gets payment expired notification and he makes payment successfully. BUT later when he uses the phone his phone will not have updated **jsonwebtoken** which will make him to redirect to payment page (which is not a good sign)

Solution 2:

Always check payment status from Database.

Problem with this : Every time request comes, database should be queried , which is the useless read operation.

If you know any better/efficient way to deal with this issue please suggest me

Mr.Bhat
  • 397
  • 5
  • 15

1 Answers1

2

Third solution would be to use some in-memory service, for example Redis.
In-memory service would store data as is (in memory) and CRUD operations would be much faster than database ones.

So, solution is:

Always check payment status

But store it in redis.

In this case you need to update user's payment status on action.
You can just save user payment status as boolean value and update it on payment made.
To remove payment status you can use EXPIRE key (set them on payment step) and redis will remove user payment from memory.
If your application will be really big and you'd like to become really memory efficient, read more about EXPIRE here and here and choose whether it's useful for you.

Note: Also, in case of JWT, it's possible for user to change it, so if you'll trust JWT completely, user can cheat you. There is more possible problems with JWT you can read here

[UPDATE]:
About MongoDB mentioned in comment.
As you said in solution 2 "...which is the useless read operation.", every stateful service, in your case database, is stateful.
Which means it has to save data somewhere. For this task databases use disk.
Doesn't matter is it SQL or NoSQL database, it has to read/write data from/on disk. Means it needs additional time and resources.
On the other hand, in-memory services store data in memory, which means that code doesn't need to go to database, ask to find data on disk, wait for db to find data on disk and then return to user.
In-memory storage just find data in memory, which is super fast. More info you can read here.

Bottom line, in-memory storage is faster than any database.
But, as always, solutions depends on your tasks.
If you need to create test service or you're gonna serve about 100 requests per second, stateful database could be enough.
But if you'll need to speed up your code or to handle more load, it's better to use in-memory storage.

Grynets
  • 2,477
  • 1
  • 17
  • 41