Redis Lua Script

Redis with Lua script can achieve:

  1. Atomicity: All commands in a Lua script execute atomically, which prevents problems caused by concurrent modifications However: Lua scripts are not inherently atomic. Redis executes commands on a single thread, and it runs the entire Lua script as one command. While the script runs, Redis handles no other requests, so no other command can interrupt it. That is why the script appears to be atomic.
  2. Fewer network round trips: Because the script runs on the server, there are fewer round trips between client and server, which improves performance.
  3. Complex operations: A Lua script can run complex logic, such as batch updates and conditional updates, that a single Redis command can’t do.

For how to use the redis with lua script in go code you can refer to: https://redis.uptrace.dev/guide/lua-scripting.html

This is my token bucket rate limiting lua script implementation: https://github.com/Fulim13/nearbuy/blob/main/pkg/ratelimit/token_bucket.lua