LuaScripts

These Lua scripts are meant for redis EVAL method. As Redis is single threaded, the EVAL method is Atomic. This property is important if we want to ensure concistency in operations that involve check-and-set behaviour

Source:

Members

(static) buySecurity

Buy Security Script Source (.lua)

buying security does the following

  1. calculate a tradeId from a monotonically increasing key tradeId.{user}
  2. update the totalBuyPrice,shares and average buy price in the users portfolio
  3. uses msgpk to store the trade for update calculations
Source:

(static) createUser

Create User Script Source (.lua)

The createuser adds 2 keys to redis

  • user.{username} = {password} , this is used to ensure unique usernames and update passwords
  • auth.{username}:{password} = 1 , this is used as a fast way to authenticate users to find username password matches
Source:

(static) getAllTrades

Source:

(static) sellSecurity

Sell Security Script Source (.lua)

selling security does the following

  1. calculate a tradeId from a monotonically increasing key tradeId.{user}
  2. Decrease the quantity of stocks and check value
    1. if value is less than 0 then roll back and return error
    2. else goto 3
  3. update the totalBuyPrice the users portfolio
  4. uses msgpk to store the trade for update calculations
Source:

(static) updateSecurity

Update Trade Script Source (.lua)

The Update Script is used for UPDATE/DELETE operations It's strongly consistent for all operations as it only does changes to the db if the entire sequence of operations is valid, else backs off. It considers 2 cases

  1. Trades where tickerSymbol is not updated
    1. Get a list of all the trades
    2. Initialize the state to 0 shares, 0 price
    3. Start performing the trades one by one
    4. If the ID of the trade matches
      1. If operation is delete, ignore the trade
      2. Else, perform the updated trade
    5. Use the newState to calculate values of the portfolio
  2. Trades Where the tickerSymbol is updated
    1. This will follow the above method for 2 stocks
    2. It will delete the trade from the old Symbol
    3. Calculate the newState for the old Symbol
    4. Insert the trade in the new Symbol
    5. Calculate the newState for the new Symbol
    6. Update the portfolio for both the symbols
Source: