Redis Mutex
This guide shows you how to implement a distributed mutex using Redis, Docker Compose, and Python. This lab demonstrates how to coordinate concurrent access to shared resources across multiple threads.
Prerequisites
Section titled “Prerequisites”- Docker installed and running.
- Dev Containers extension installed (optional).
How to execute
Section titled “How to execute”-
Setup Environment:
Open VS Code in the project folder and execute this command in the Command Palette:
Terminal window > Dev Containers: Reopen in ContainerTerminal window pip install uv && uv sync -
Start Infrastructure: Launch the Redis container:
Terminal window docker compose up -d -
Run the Example:
Terminal window python main.pyTerminal window python main_acquire.py
Validate results
Section titled “Validate results”Verify that each thread wrote its line in order by checking shared_file.txt:
cat shared_file.txtClean Up
Section titled “Clean Up”docker compose down -vrm shared_file.txt