whatisgithub

What is sqlrace?

nsf/sqlrace — explained in plain English

Analysis updated 2026-08-07 · repo last pushed 2015-10-28

8GoAudience · developerComplexity · 2/5DormantSetup · moderate

In one sentence

A teaching tool in Go that demonstrates race conditions in databases. It shows how concurrent workers corrupt shared data and proves that transactions alone aren't enough, you need row locking or atomic updates.

Mindmap

mindmap
  root((repo))
    What it does
      Demo race conditions
      Tests update strategies
      Shows correct methods
    Tech stack
      Go
      MySQL-compatible DB
    Use cases
      Learn DB concurrency
      Teach race conditions
      Test locking methods
    Audience
      Developers
      Students
    Key concepts
      Transactions not enough
      Row locking works
      Atomic updates work
      Atomic is fastest

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Teach students how race conditions corrupt data when multiple workers update the same database row simultaneously.

USE CASE 2

Demonstrate that a standard database transaction alone does not prevent lost updates under concurrent access.

USE CASE 3

Compare row locking versus atomic updates to find the safest and fastest strategy for concurrent decrements.

USE CASE 4

Simulate an e-commerce oversell scenario where two customers buy the last item in stock at the same time.

What is it built with?

GoMySQL

How does it compare?

nsf/sqlracebeppetemp/cartographerd4l3k/turtle
Stars888
LanguageGoGoGo
Last pushed2015-10-282018-01-27
MaintenanceDormantDormant
Setup difficultymoderatemoderatemoderate
Complexity2/54/52/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · moderate Time to first run · 30min

Requires a running MySQL-compatible database and Go installed to build and execute the demo.

No license information is provided in the repository, so usage rights are unclear.

So what is it?

SQL Race is a teaching tool that demonstrates a common but tricky bug called a "race condition" in database applications. It shows that simply wrapping database operations in a transaction does not, by itself, prevent data corruption when multiple processes try to change the same data at the same time. A race condition happens when multiple workers all try to read and update the same piece of data at once, stepping on each other's toes. This tool runs a simple experiment: it starts with a counter set to 4,096 and asks four workers to subtract 1 from it repeatedly, over a thousand times each. If everything works correctly, the counter should reach zero. The tool lets you test different strategies for updating the database, ranging from a naive approach with no protection to more sophisticated methods. The demo's output tells the story clearly. When the workers use the naive approach, the final result is wrong, they lose some decrements because the workers overwrite each other's changes. Switching to a standard transaction does not fix the problem, the result is still incorrect. Only two methods get the right answer: locking the row so no one else can touch it until the update is complete, or using an atomic update where the database itself handles the subtraction safely. This project is aimed at developers and students learning about database concurrency. A practical example would be an e-commerce checkout where two customers are buying the last item in stock simultaneously, without proper safeguards, both might succeed, and the store oversells. The demo proves that you need specific techniques like row locking or atomic operations, not just transactions, to keep data accurate under concurrent load. The project is written in Go and requires a MySQL-compatible database to run. The README notes that the atomic method is also the fastest, since it avoids the overhead of locking, making it both correct and efficient.

Copy-paste prompts

Prompt 1
Set up SQL Race locally: I have Go and a MySQL-compatible database ready. Walk me through cloning, configuring the database connection, and running the four-worker experiment that decrements a counter from 4096.
Prompt 2
Help me understand the SQL Race output: the naive approach and standard transaction both gave wrong results, but row locking and atomic updates worked. Explain in simple terms why transactions alone don't prevent race conditions.
Prompt 3
Using the SQL Race project as a reference, write a Go function that safely decrements a shared database counter under concurrent access using an atomic SQL update instead of a transaction.
Prompt 4
Extend SQL Race to test a fifth strategy: compare the current four methods plus a SELECT...FOR UPDATE approach. Show me how to add this new worker strategy to the existing Go code and run it against the same 4096-counter experiment.

Frequently asked questions

What is sqlrace?

A teaching tool in Go that demonstrates race conditions in databases. It shows how concurrent workers corrupt shared data and proves that transactions alone aren't enough, you need row locking or atomic updates.

What language is sqlrace written in?

Mainly Go. The stack also includes Go, MySQL.

Is sqlrace actively maintained?

Dormant — no commits in 2+ years (last push 2015-10-28).

What license does sqlrace use?

No license information is provided in the repository, so usage rights are unclear.

How hard is sqlrace to set up?

Setup difficulty is rated moderate, with roughly 30min to a first successful run.

Who is sqlrace for?

Mainly developer.

Open on GitHub → Ask about another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.