jargon

Backend & systems·topic 6 of 13

Concurrency and coordination

Two things happening at once, and the machinery for making that safe. Most of these bugs are invisible under test load and obvious at 3am, which is exactly why the vocabulary matters.

Read in order · tick what you already know

  1. 01

    your service is handling four hundred requests at once on eight cores, interleaving them whenever one is waiting on I/O.

    Concurrency

  2. 02

    you split the batch across eight cores so eight rows are genuinely being computed at the same instant.

    Parallelism

  3. 03

    the bug only shows up under load, never in tests, and the outcome depends on which of two threads got there first.

    Race condition

  4. 04

    you wrap the shared counter in a lock so only one thread can be inside that block at a time.

    Mutex

  5. 05

    you allow at most ten things through at once, not one, because the thing you are protecting is a capacity rather than a variable.

    Semaphore

  6. 06

    you added more workers and throughput did not improve, because they are all queued behind the same lock.

    Lock contention

  7. 07

    two transactions each hold a lock the other one needs and both sit there until the database kills one of them.

    Deadlock

  8. 08

    the low-priority job never runs because higher-priority work keeps arriving and taking the resource first.

    Starvation

  9. 09

    you take a lock on the row up front with SELECT FOR UPDATE, so everyone else waits until your transaction ends.

    Pessimistic locking

  10. 10

    you read the row with its version, write it back only if the version has not changed, and retry the whole thing if it has.

    Optimistic locking

  11. 11

    you update the value only if it still equals what you last read, and loop round to try again if somebody beat you to it.

    Compare-and-swap

  12. 12

    you need only one instance across the whole fleet to run the job, so they all try to claim a key in Redis first.

    Distributed lock

  13. 13

    you can safely run the same operation five times and the system ends up in exactly the state it would after one.

    Idempotent

  14. 14

    you run it twice with the same input and get byte-identical output, because nothing inside it reads the clock or a random number.

    Deterministic

  15. 15

    you keep a fixed set of threads and hand them tasks, instead of spawning a new thread for every request.

    Thread pool

  16. 16

    you reuse a fixed set of open database connections instead of paying the handshake on every query.

    Connection pool

  17. 17

    one thread sits in a loop pulling ready events off a queue and running the callback for each one.

    Event loop

  18. 18

    the thread issues the request and goes off to do other work instead of sitting there waiting for the socket.

    Non-blocking I/O

  19. 19

    one slow request at the front of the queue holds up every fast request stuck behind it.

    Head-of-line blocking