Example of Preventing Race Conditions in Redis using Locks
import redis
import time
# Connect to Redis
r = redis.Redis(host='localhost', port=6379, db=0)
# Key for the lock
lock_key = 'my_lock'
# Acquire lock
while not r.set(lock_key, 'locked', nx=True, ex=10):
time.sleep(0.1) # Retry after a short delay if lock is not acquired
try:
# Critical section - perform operations that need to be atomic
current_count = r.get('my_counter')
if current_count is None:
current_count = 0
else:
current_count = int(current_count)
new_count = current_count + 1
r.set('my_counter', new_count)
finally:
# Release lock
r.delete(lock_key)
In this Python example, a lock is implemented using Redis to prevent race conditions. The code attempts to acquire a lock by setting a key with a specific expiration time. If the lock is not acquired, it retries after a short delay. Within the critical section, operations that need to be atomic are performed, such as incrementing a counter safely. Finally, the lock is released by deleting the key. This approach ensures that only one thread can execute the critical section at a time, preventing race conditions in Redis.