Cloud & Data Centers

Databases in the Cloud: SQL and NoSQL

How relational and NoSQL databases differ, what managed services handle, and how to choose.

Key takeaways

  • Relational databases enforce structure and relationships and handle flexible queries and transactions well.
  • NoSQL covers several different designs, each optimized for particular ways of reading and writing data.
  • Many modern systems blur the line, so compare specific features and guarantees instead of labels.
  • Managed services reduce operational work but leave data modeling, access control and recovery testing to you.
  • Choose by listing your queries, consistency needs, growth and the skills of the people who'll run it.

SQL databases store data in related tables with a defined structure and are queried with the Structured Query Language. "NoSQL" is a loose label for databases built around other shapes, such as documents, key-value pairs, wide columns or graphs. In the cloud you can rent either as a managed service, so the provider handles much of the installation and upkeep.

If you're unsure, a relational database is a sound default for most applications. A NoSQL system earns its place when your access patterns, scale or data shape fit it clearly better.

How relational databases work

A relational database organizes data into tables of rows and columns. A schema defines each column's type and the rules, such as "every order must belong to an existing customer." Keys link tables together, and a join combines them in a query.

SELECT customers.name, SUM(orders.total) AS spent
FROM customers
JOIN orders ON orders.customer_id = customers.id
GROUP BY customers.id, customers.name
ORDER BY spent DESC;

You didn't have to plan for that report when you designed the tables. Flexible querying is one of the model's great strengths.

The other is transactions, often described by the initials ACID: atomicity, consistency, isolation and durability. In practice, a group of changes either all happen or none do, rules stay satisfied, concurrent users don't see half-finished work in ways your settings forbid, and committed data survives a crash. PostgreSQL, MySQL, MariaDB and SQL Server are familiar examples.

Indexes and why queries slow down

An index is an extra structure that lets a database find rows without reading all of them, much like the index of a book. A query that's instant on a thousand rows can crawl on ten million if no index supports it.

Indexes aren't free. Each one takes storage and has to be updated on every write. Add them for the searches your application really performs, and remove ones that nothing uses.

Most relational databases can show how they plan to run a query, usually through an EXPLAIN command. A full scan of a large table in that output is a common sign of a missing index. In NoSQL systems, the equivalent mistake is querying on a field that isn't part of a key or secondary index, which may read, and bill for, far more data than you expected.

The NoSQL families

Four common NoSQL designs
Key-value Fetch a value by its key. Caches and sessions. Document Nested records, often JSON-like. Profiles, catalogs. Wide-column Rows grouped by partition key. Large write volumes. Graph Nodes and relationships. Networks and paths. Key-value Fetch a value by its key. Caches and sessions. Document Nested records, often JSON-like. Profiles, catalogs. Wide-column Rows grouped by partition key. Large write volumes. Graph Nodes and relationships. Networks and paths.

A key-value store is the simplest: give it a key, get back a value. Redis is a well-known in-memory example.

A document database such as MongoDB stores self-contained records that can differ from one another. An order and its line items can live together and be read in one step.

Wide-column stores such as Apache Cassandra spread huge numbers of rows across many machines, organized around a partition key chosen to match your queries.

Graph databases such as Neo4j treat relationships as first-class data, which helps when questions involve following many connections.

Search engines and time-series databases are further specialized options, often used alongside a primary database.

The differences that matter

Schema. Relational systems check structure when data is written. Many NoSQL systems let records vary, which speeds early change. The schema still exists, in your application code, and old records may not match new expectations.

Queries. Relational databases handle questions you didn't foresee. Several NoSQL designs perform very well for the access paths you planned and poorly, or not at all, for others.

Scaling. Relational databases traditionally scale up and add read replicas. Many NoSQL systems were designed to split data across machines from the start. Scaling up versus scaling out explains the trade-off.

Duplication. Relational design usually stores each fact once, called normalization. Document and wide-column designs often copy data to make reads fast, which means updating several places.

The line keeps blurring. Many relational databases handle JSON columns, many document databases offer transactions, and some distributed systems provide SQL across many machines. Check the actual guarantees of the product and settings you'd use.

Modeling the same data two ways

Take a shop with customers, orders and products. In a relational design you'd have separate tables, with each order line pointing to a product and each order pointing to a customer. Changing a customer's email means updating one row. Showing an order page means joining several tables.

In a document design, you might store the order as one record that includes the lines, the product names and prices as sold, and the shipping address. Reading the page takes one fetch. Copying the price is correct here, since an old order should show what was paid. Copying the customer's current email into every order would create a chore each time it changed.

The relational habit is to ask what the facts are. The document habit is to ask how they'll be read. Good designs of either kind consider both.

Consistency across copies

Cloud databases keep multiple copies. When data changes, a strongly consistent read returns the latest confirmed value. An eventually consistent read may briefly return an older one, with copies converging afterward.

The CAP theorem describes the underlying tension: when a network partition separates copies, a system must give up either consistency or availability for the affected requests. Outside such failures there's still a trade-off between delay and consistency, because agreement takes round trips.

A stale read from a lagging replica
App Primary Replica Update address 1 Saved 2 Read address 3 Old address 4 Change hasn't arrived yet Replicate update 5 App Primary Replica Update address 1 Saved 2 Read address 3 Old address 4 Change hasn't arrived yet Replicate update 5

A slightly old like count is harmless. An old account balance during a withdrawal isn't. Many services let you choose per request.

Backups and recovery

Check what your service backs up by default, how far back it keeps copies and how long a restore takes at your data size. Point-in-time recovery, where available, lets you return to the moment just before a bad update.

A restore usually creates a new database instance, so your application has to be pointed at it, and recent legitimate changes may need to be merged back. Plan for that. Also consider what happens if the whole account or database is deleted, and keep an independent export if the answer is that the backups go with it. RPO and RTO help you decide how much protection is enough.

Serverless and distributed options

Some providers offer databases that adjust capacity automatically, or pause when idle. They suit irregular workloads and development environments. Check limits on connections, the delay when waking and how costs behave under steady heavy use.

Globally distributed databases place copies in several regions. They can shorten reads for distant users and ride through a regional failure. Writes then involve either a single home region or coordination across distance, with delay or conflict handling to match. Adopt that complexity when you have a clear need for it.

What a managed service does for you

With a managed database, the provider typically handles hardware, installation, patching within windows you choose, automated backups, monitoring basics and failover to a standby if you enable it.

You still own a lot:

  • Table or document design and indexes
  • Query performance
  • Users, permissions and network access
  • Encryption choices and key handling
  • Backup retention, and proof that restores work
  • Cost control and capacity settings

Running a database yourself on a virtual server gives more control and can cost less in fees, but you take on patching, backups and failover. For small teams, managed is often worth it.

Pricing and lock-in

Billing models vary: by instance size and hours, by storage, by read and write requests, or by consumed capacity. Backups, replicas and data transfer may be extra. Request-based pricing suits spiky light use and can surprise you under heavy load or with an inefficient query that scans everything. Prices change, so model your workload against the current rate pages.

Proprietary databases can be excellent and hard to leave, since data models and query methods don't transfer directly. Services compatible with widely used open source engines make moving easier, though compatibility is rarely total. See limiting cloud lock-in.

Securing a cloud database

Keep the database off the public internet where possible, inside a virtual private cloud that only your application servers can reach. Require encrypted connections. Give the application an account with only the permissions it needs, not the administrator login. Rotate credentials and store them in a secrets manager.

In your code, use parameterized queries so input is never treated as part of a command. This is the core defense against SQL injection, and NoSQL query interfaces need similar care.

Turn on deletion protection and point-in-time recovery if offered. Replicas copy mistakes, so they don't replace backups.

How to choose

Write down the main things your application reads and writes, how often, and which must be exactly right immediately. Estimate data size and growth honestly.

Which database should you start with?
Do records relate in many ways and need multi-item transactions? YES Choose a relational database 1 NO Do you mostly fetch short-lived values by a known key? YES Add a key-value store or cache 2 NO Are records self-contained and read as a whole? YES A document database may fit well 3 NO Do you need very high write volume across many machines? YES Evaluate wide-column or distributed systems 4 NO Default to relational It adapts well while your needs are still unclear. Do records relate in many ways and need multi-item transactions? 1 IF YES Choose a relational database NO Do you mostly fetch short-lived values by a known key? 2 IF YES Add a key-value store or cache NO Are records self-contained and read as a whole? 3 IF YES A document database may fit well NO Do you need very high write volume across many machines? 4 IF YES Evaluate wide-column or distributed systems NO Default to relational It adapts well while your needs are still unclear.

Using more than one is normal: a relational database for orders, a cache for sessions and a search index for the catalog. Each addition means another thing to secure, back up and keep in sync, so add them when a measured need appears. A queue or event stream is one common way to carry changes between them.

Before you commit

Load a realistic amount of sample data and run your most important queries. Restore a backup into a new instance and time it. Trigger a failover in a test copy and see how your application reconnects. Export the data in a portable format and look at what you'd have to rebuild elsewhere. An afternoon of this tells you more than any feature table.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Cloud & Data Centers →

Latest news on this topic