Back to blog

SQL or NoSQL: which one fits a photo app?

Thắng Đoàn
Thắng Đoàn

FotoFlow has 10,000 users and the data model is settling, so the database choice made now will be hard to undo. Most apps start with a relational database such as PostgreSQL or MySQL: data in tables with a fixed set of columns, a schema, tables pointing at each other with keys, and a join combining them in one query. When unsure, relational databases are a very safe default.

The relational guarantee

Moving 50 dollars from Ana to Ben is two updates, and a crash between them makes money disappear. A transaction groups them: both happen or neither does, the ACID guarantee. That is why payments, orders, and likes counts live relational.

The classic pain points show up later. A products table sprouts hundreds of mostly-empty columns because shoes have sizes and laptops have CPUs, and every new product type means a migration on a huge table, some of which lock it for hours. And all writes still go through one database server.

Documents, keys, and what you give up

A document store such as MongoDB keeps each record as a self-contained document: a shoe and a laptop with completely different fields, no migration to run, reading one product as a single fetch, spreading naturally across servers. Documents shine when you read and write whole objects at once: catalogs, profiles, content.

A key-value store such as Redis or DynamoDB is the simplest of all: get and set by key, in about a millisecond. Perfect for sessions, carts, and counters, anything you always fetch by one id.

The trade: NoSQL stores scale out easily largely *because* they give things up, joins across records, multi-record transactions, and often strict consistency. Picking NoSQL for scale you do not have yet often means rebuilding joins and transactions by hand. The lines blur, PostgreSQL stores JSON, MongoDB offers transactions, but the defaults still lean these ways.

Pick per kind of data

Real systems mix stores, called polyglot persistence. For FotoFlow: money and orders stay relational for transactions, photo metadata and profiles could live in documents if shapes diverge, sessions already go to a key-value store. Start with one database and add another only when a kind of data clearly wants it. The question is not SQL or NoSQL, but what this data needs: relations, flexibility, or raw lookup speed.

One specialist worth knowing: free-text search. Typing "red sunset" into a search box becomes WHERE caption LIKE '%red%' on millions of rows, slow and unable to rank or handle typos. A search engine builds an inverted index, for every word the list of documents containing it, fed from the main database by a stream of changes. It is a copy, not the source of truth, and can always be rebuilt.

What this does not solve

Choosing the store says nothing about how fast it answers. A perfectly chosen SQL database can still take two seconds to find one user by email, and the fix is not a bigger database. That is the next post.

If you renamed a column tomorrow, which of your app's features would notice first?
Which table in your schema is already sprouting mostly-empty columns, and what is it secretly trying to become?

One database, chosen per question the data must answer.

Next in the series: One table got slow: do you need a bigger database yet?

Sources

Designing Data-Intensive Applications

Amazon Dynamo (2007)

undefined

System Design Primer

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.