Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Redis Lua Script With Red Envelope & Message Queue

Redis Lua Script With Red Envelope & Message Queue

Golang Taiepi Meetup #41

Yun Chen

April 30, 2019
Tweet

More Decks by Yun Chen

Other Decks in Programming

Transcript

  1. REDIS LUA SCRIPT WITH RED ENVELOPE & MESSAGE QUEUE GOLANG

    TAIPEI MEETUP #41 https://github.com/chenyunchen/K8S-Meetup
  2. WHO AM I Chen Yun Chen (Alex) > [email protected] >

    blog.yunchen.tw Experience > Backend Engineer at JKOPay > Software Engineer at Linker Networks
  3. LUA: GOLANG options := redis.Options{ Network: "tcp4", Addr: "127.0.0.1:6379", DB:

    0, PoolSize: 10, } client := redis.NewClient(&options) fileBytes, err := ioutil.ReadFile("path") if err != nil { return } sha, err := client.ScriptLoad(string(fileBytes)).Result() if err != nil { return } keys := []string{"redenvelopeID"} args := []string{"get_redenvelope", "userID"} res, err := client.EvalSha(sha, keys, args).Result() 9
  4. LUA: DEBUG ./redis-cli --ldb --eval script.lua mykey somekey , arg1

    arg2 More Detail: https://redis.io/topics/ldb 10
  5. 毆ض叨ኞ夺۱ > 究ਧᰂ氃ጱ獤蟴秚ګ (ex: 1000ز 獤蟴 10۱) 50% 60% 70%

    80% 90% 99% [30 41.11 52.22 63.33 74.44 125.56 136.67 147.78 158.89 170] > 矎ଧ(褰秚Ի矦) or 藶穩瞲窔 [136.67 158.89 41.11 170 74.44 147.78 125.56 30 52.22 63.33] 12
  6. ח扖藯஑অ: ᓒ桽አၶ讨牧螛෱ᤩՈ瞇 var money1 float32 = 9.9 fmt.Println(money1 * 100)

    // 989.99994 var money2 float64 = 1.1 fmt.Println(money2 * 100) // 110.00000000000001 13
  7. LUA: 砋夺۱ local function getRedenvelope(rID, userID) if redis.call('hexists', rID, userID)

    ~= 0 then return nil; else local redenvelope = redis.call('rpop', 'redenvelope'); if redenvelope then local r = cjson.decode(redenvelope); r['userID'] = tonumber(userID); local re = cjson.encode(r); redis.call('hset', rID, userID, userID); redis.call('lpush', 'consume_' .. rID, re); return re; end end return nil; end 16
  8. BUT... RETRY & ACK 1. Worker 篷ဩ狒挨玲አRedis虻碘ጱWorker 100%౮ۑ ݢ胼襑ᥝᛔᤈ蒂ቘӞ犚奞℄: ๚蒂ቘ牏蒂ቘӾ牏૪蒂ቘ

    2. አ䜛ᒒ 苭Ӟአ䜛ᒒ䷱硩ک夺۱蝢Ꭳ䨝ெ讕䰬? 3. ䷱硩ک夺۱蝢Ꭳ吚簁疰100%砋犋ک 4. 虋๚䋿匍ᰂ氃蜴ࢧ咳硯ጱአ䜛癱蒈 18
  9. 犥䋿匍懱௳褧ڜ傶ֺ List > 膏夺۱砺֢Ӟ膌牧墋㻌ᓕቘ > 㰍ᔱ୚㬵瞲ਧ牧篷messageID > Value: String Data

    Bit Manipulation > 㻌ӞKEY:Value珀ಅ襑橕狌獉਻ > ᛔᤈ姘虁虻碘bytes裾ଶ/֖ᑏ > Value: Binary Data 21
  10. LUA: MESSAGE HEADER local function getHeader(name) return redis.call('getrange', name, 0,

    7); end local function parseHeader(header) return struct.unpack('I4HH', header); end local function setHeader(name, lastID, head, tail) local header = struct.pack('I4HH', lastID, head, tail); redis.call('setrange', name, 0, header); end 22