e-Book - Redis in Action

This book covers the use of Redis, an in-memory database/data structure server.
  • Foreword
  • Preface
  • Acknowledgments
  • About this Book
  • About the Cover Illustration
  • Part 1: Getting Started
  • Part 2: Core concepts
  • Part 3: Next steps
  • Appendix A
  • Appendix B
  • Buy the paperback

    Chapter 11: Scripting Redis with Lua

    This chapter covers

    • Adding functionality without writing C
    • Rewriting locks and semaphores with Lua
    • Doing away with WATCH/MULTI/EXEC
    • Sharding LISTs with Lua

    Over the last several chapters, you’ve built up a collection of tools that you can use
    in existing applications, while also encountering techniques you can use to solve a
    variety of problems. This chapter does much of the same, but will turn some of
    your expectations on their heads. As of Redis 2.6, Redis includes server-side scripting
    with the Lua programming language. This lets you perform a variety of operations
    inside Redis, which can both simplify your code and increase performance.

    In this chapter, we’ll start by discussing some of the advantages of Lua over performing
    operations on the client, showing an example from the social network in
    chapter 8. We’ll then go through two problems from chapters 4 and 6 to show examples
    where using Lua can remove the need for WATCH/MULTI/EXEC transactions.
    Later, we’ll revisit our locks and semaphores from chapter 6 to show how they can be
    implemented using Lua for fair multiple client access and higher performance.
    Finally, we’ll build a sharded LIST using Lua that supports many (but not all) standard
    LIST command equivalents.

    Let’s get started by learning about some of the things that we can do with Lua
    scripting.