Compare

SQL vs NoSQL

SQLTables on one machine
Posts

virat.kohli

1,204,337 likes

virat.kohli 2 hours ago

App serverone database
PostgreSQLone machine
  • likesone per person
  • count1,204,337
NoSQLRows by key, on many
Posts

virat.kohli

1,204,337 likes

virat.kohli 2 hours ago

App serverroutes by key
  • Machine 1counters
  • Machine 2followers_by_user
  • Machine 3likes_by_post
  • Machine 4users
  • Machine 5users

Instagram with every fact in PostgreSQL, or in Cassandra: a double-tapped like, a story seen a hundred million times, then a question nobody planned. Illustrative numbers.

SQL or NoSQL?

SQL when facts relate and must stay right: follows, likes, comments, payments. One table answers any question, and a rule or a transaction keeps it right. NoSQL when small writes come by the hundred million and are read back by key: views, trays, messages. It spreads across machines by design, one table per question. Instagram keeps both.

DifferenceSQLNoSQL
ShapeTables with fixed columns; each fact once, linked by idsRows by key, laid out per question; facts copied
Several changes at onceOne transaction: all or nothingOne row at a time
A rule (one like per person)The table enforces itThe app enforces it
Writes beyond one machineSplit by hand; transactions stop at the piecesSpread by key; add a machine to grow
A new questionOne more query, tonightA new table, filled from the old
Pick it forFollows, likes, comments, paymentsViews, trays, messages, logs

SQL vs NoSQL, explained step by step

1. A like

Three moments on Instagram: Rajat double-taps Virat’s post, a hundred million fans watch his story, and Virat asks where his followers are from.

On the left, a SQL database like PostgreSQL: tables on one machine, every fact stored once. On the right, a NoSQL database like Cassandra: rows by key, spread across many machines.

First, the like. One transaction writes the like row and the post’s count together: both land, or neither does.

Rajat’s thumb slips and taps again. The table has a rule, one like per person per post, so the second row is refused and the count stays put.

With NoSQL, the row and the counter are two separate writes on two machines; nothing holds them together.

The second tap writes the same row again, but the counter still goes up: one person, counted twice. That rule now lives in the app.

2. The story

Next, a hundred million fans watch Virat’s story in an hour: a row per view, twenty-eight thousand writes a second, all to one machine.

Say it manages twenty thousand: the rest queue up, and after an hour it’s thirty million rows behind.

With NoSQL, each row goes to the machine that owns its key, so a hundred machines share the flood, under three hundred a second each.

Need more? Add a machine, and it takes its share of the keys.

3. Insights

Finally, Virat opens Insights: where are my followers from? Nobody planned that question, but one query joins followers with users and counts by city.

It takes minutes over 273 million rows, but it answers tonight, with no new table.

With NoSQL, the followers table holds only ids, and there is no join to reach each user’s city.

So the team builds a new table, followers by city, fills it overnight, and every new follow writes there too. Each new question needs a new table.

In short

SQL when facts relate and must stay right: follows, likes, comments, payments. NoSQL when small writes come by the hundred million and are read back by key: views, trays, messages. Instagram keeps both.