Back to Home

Semaphore / Rate Limiting

Control access to a limited pool of resources

🚧 What is a Semaphore?

A semaphore is a counter that controls access to a limited pool of resources. Think of it like a parking lot with N spaces - when all spaces are taken, new cars must wait.

✓ Use Cases

  • • Connection pool limits
  • • API rate limiting
  • • Bounded parallelism
  • • Resource quotas

⚙️ Operations

  • acquire(): Decrement, block if 0
  • release(): Increment, wake waiters
  • • Counter can be > 1 (unlike mutex)

🎬 What to Watch

See how only N permits can be held at once. When all are taken, new requests wait in the queue until a permit is released!

Max Permits:3
Available Permits
3/3
Processing
0
Waiting
0
Completed
0
Rejected
0

🚗 Resource Access (Tollbooth Analogy)

Waiting Queue
No waiting requests
Gates (3 slots)
Empty
Empty
Empty
Exit
0
done

Semaphore State

3 permits available, 0 in use

Event Log

Start demo to see events...

How It Works

A semaphore is a counter that controls access to shared resources.

  • acquire(): Decrement counter (blocks if 0)
  • release(): Increment counter (wake waiters)

Use Cases

  • • Connection pool limits
  • • API rate limiting
  • • Worker thread limits
  • • Resource quotas
  • • Bounded parallelism