Using Redis to implement login?

2019-06-14 06:33发布

问题:

We're using Redis to implement login mechanism between 2 servers over a vpn.

So basically it's :

How we did it ?

At server1 side we do :

string RandomNum = Guid.NewGuid().Tostring("N"); 
sub.Subscribe("DO_LOGIN"+RandomNum,(channel,message)=>{ //listen to specific (!) response
 ...
});
sub.Publish("Do_LOGIN",{ ...data  , RandomNum});

While server2 is already listening via :

sub.Subscribe("DO_LOGIN",(channel,message) =>
{
    //read message values + extract RandomNum from the message
    //check DB if user OK
    sub.Publish("DO_LOGIN"+RandomNum, ...data);
});

So as you can see- for every login I generate a number and command and then listen to a new channel which is concatenation of "command"+RandomNum

But we feel that this is a wrong path to go. Because all of those temp subscriptions.

Question:

Is it possible to implement login mechanism without temp subscriptions ? (unless this is how it should be)

NB We're using StackExchange.Redis

回答1:

Your can use two Lists for this type of distributed pattern instead. I'm suggesting this mainly because of PubSub's nature of "shoot and forget" without guaranteeing delivery. I would assume that in your case, your do not want that.

Here's the suggested flow - first you'll need to set up two blocking "listeners":

  1. Server1: BRPOP completed
  2. Server2: BRPOP requests

Then, when a user logs in:

  1. Server1: LPUSH requests data
  2. Server2: unblocks, processes data, LPUSH completed data and go back to blocking pop
  3. Server1: unblocks, does whatever, go back to blocking pop