In Part 3, we followed one request through the network layers. This week is the operating systems chapter that produces the trickiest GATE questions: concurrency, race conditions and deadlocks. In the cloud, these aren't just exam problems. Thousands of requests hit the same data at the same moment, so AWS services are built around the exact solutions from our syllabus.
1. The race condition: the lost update
Every GATE aspirant has traced this one. Two processes read a shared counter (value 10), both add 1, both write back 11. One update is lost.
In the cloud it looks like this: two users book the last seat at the same instant. Both read "1 seat left", both write "0 left", and you've sold the seat twice.
Fix A: atomic operations (like a hardware test-and-set)
DynamoDB's UpdateItem can increment or decrement a number atomically on the server, for example ADD seats_left -1 . There's no separate read and write for another request to slip between, just like an atomic instruction in our OS notes.
Fix B: conditional writes (optimistic concurrency)
DynamoDB lets you attach a condition to a write: "only update if seats_left > 0 ", or "only update if version = 7 ". If another request changed the item first, the condition fails and you retry with fresh data.
That's optimistic concurrency control from the DBMS syllabus: don't lock, just check at write time that nobody else changed the data.
2. Critical sections across many items: transactions
Sometimes the critical section covers several items, like moving money between two accounts. DynamoDB's TransactWriteItems applies all the writes or none of them.
Here's the interesting part for deadlock theory: if two transactions conflict, DynamoDB rejects one instead of letting it wait for the other's locks. Your code just retries. A transaction that never waits while holding resources can't take part in a deadlock.
3. Deadlock: the four conditions
The textbook says deadlock needs all four Coffman conditions at once:
Condition | Meaning | How cloud services break it |
|---|
Mutual exclusion | A resource can be held by only one process | Usually unavoidable for writes |
Hold and wait | Hold one resource while waiting for another | DynamoDB transactions fail fast instead of waiting |
No preemption | Resources can't be forcibly taken back | Databases can abort a transaction to free its locks |
Circular wait | A cycle of processes each waiting on the next | Lock resources in a fixed order in your own code |
Relational databases still use locks, so deadlocks can happen. MySQL's InnoDB engine (which you can run on Amazon RDS) detects deadlocks and rolls back one of the transactions. That's the "detection and recovery" strategy from our notes, running in production. The application's job is to catch that error and retry.
4. Producer and consumer: Amazon SQS
The producer-consumer problem is the classic synchronisation question. In AWS, the shared buffer is often an Amazon SQS queue: producers send messages, consumers (for example Lambda functions) process them, and neither side has to wait for the other.
SQS adds its own concurrency rule, the visibility timeout. When a consumer receives a message, SQS hides it from other consumers for a set time. If the consumer finishes and deletes it, done. If the consumer crashes, the message reappears and another consumer picks it up.
The catch: a standard queue guarantees at-least-once delivery, so the same message can occasionally be processed twice. Your consumer must be idempotent, meaning processing a message twice has the same effect as once. A FIFO queue preserves order within a message group and removes duplicates, at lower throughput.
5. Semaphores: limiting concurrency
A counting semaphore lets at most n processes into a section at once. Lambda has a similar control called reserved concurrency: set it to 5, and at most 5 copies of that function run at the same time. It's useful when your function writes to a database that can't handle hundreds of connections at once.
The summary
Atomic updates and conditional writes stop race conditions. Transactions that fail fast break hold-and-wait. Deadlock detection in RDS rolls back a victim. SQS is the producer-consumer buffer, and idempotency handles duplicates.
Try this GATE-style question
Two Lambda functions read the same DynamoDB item (version = 3), modify it, and write it back with the condition "version = 3", setting version = 4. What happens to the second write, and which concurrency control technique is this?
Answer in the comments. I'll reply to everyone.
Next in this series: CPU scheduling and memory management, and how EC2 instance types and Lambda's memory setting relate to them.
Loading article