dot Stop testing, start deploying your AI apps. See how with MIT Technology Review’s latest research.

Download now

11.2 Rewriting locks and semaphores with Lua

back to home

11.2 Rewriting locks and semaphores with Lua

When I introduced locks and semaphores in chapter 6, I showed how locks can reduce
contention compared to WATCH/MULTI/EXEC transactions by being pessimistic in heavy
traffic scenarios. But locks themselves require two to three round trips to acquire or
release a lock in the best case, and can suffer from contention in some situations.

In this section, we’ll revisit our lock from section 6.2 and rewrite it in Lua in order
to further improve performance. We’ll then revisit our semaphore example from section
6.3 to implement a completely fair lock while also improving performance there.

Let’s first take a look at locks with Lua, and why we’d want to continue using locks
at all.