Graph, time-series and object storage
Three kinds of data a general database handles badly, the stores built for each, and a decision guide across every store type in this module.
The problem
Three teams use a relational database for three different jobs, and each hits a wall.
Friends of friends. A social app keeps friendships in an edge table with a billion rows. “People you may know” needs friends of friends of friends. In SQL each hop is a self-join. With 200 friends per user on average, three hops means following about 200 × 200 × 200, or 8 million, paths for a single request, each step an index lookup into the billion-row table. A question such as “what is the shortest connection between these two people?” is not even a fixed number of joins.
Metrics. A monitoring system collects 100 metrics from 10,000 servers every 10 seconds. That is 100,000 points per second and 8.64 billion per day. In an ordinary indexed table, with 50 bytes or more per row once row overhead and the index are counted, that is over 430 GB a day. Removing last month’s data means deleting about 260 billion rows.
Photos. An app stores 4 MB photos in a binary column. Ten million photos put 40 TB inside the database. Backups take days, replicas fall behind, and the page cache fills with image bytes, all on the most expensive storage the company owns.
In each case the data has a shape that a general-purpose engine cannot exploit, and a specialised store exists that can.
Graph databases
The data model is the property graph. Nodes stand for things and relationships connect them. Both carry properties, and relationships have a type and a direction. A query describes a pattern to match, here in Neo4j’s Cypher:
MATCH (me:User {id: 42})-[:FRIEND*2..3]-(other:User)
RETURN DISTINCT other LIMIT 20
The mechanism. A graph database stores each node’s relationships with the node. Moving along an edge follows a stored reference and does not search a global index. The cost of a traversal therefore depends on how much of the graph it visits, not on the total size of the graph.
Example systems. Neo4j, Amazon Neptune, and JanusGraph, which runs on top of stores such as Cassandra.
Choose it for queries that follow several hops or a variable number of them: fraud rings that share devices and cards, recommendations, knowledge graphs, dependency and network topologies, permission inheritance.
Do not choose it when:
- Queries are one hop. “Who does this user follow?” is a lookup on an edge table with an index in each direction. A relational or wide-column store does that at any scale.
- You need to scale writes across many machines. A graph has no clean place to cut. Once it is partitioned, a traversal crosses the network on many hops.
- A few nodes are enormous. An account with 50 million followers makes any traversal through it expensive.
- The job is analysis of the whole graph, such as ranking every node. That is batch processing.
Facebook’s social graph is a useful reference point. Its TAO system, described in a 2013 paper, serves a graph of objects and associations from sharded MySQL behind a large cache, not from a graph database product.
Time-series databases
The data model. A series is identified by a metric name and a set of tags, such as cpu_usage with host=web-17 and region=eu. Each series is a sequence of points, and a point is a timestamp and a value. The workload has four properties: it is append-only, it arrives roughly in time order, it is read as time ranges and aggregates, and it expires by age.
The mechanisms follow from those properties.
- Partition by time. Data is stored in chunks that each cover a time window. Expiring old data means dropping whole chunks, which is quick and leaves nothing to clean up.
- Compress by column. Timestamps arrive at regular intervals and neighbouring values are similar, so both compress very well. Facebook’s Gorilla paper (2015) reported an average of 1.37 bytes per point, down from 16.
- Downsample. Keep 10-second data for a week and 5-minute averages for a year.
Example systems. Prometheus, which is built for monitoring and keeps 15 days of data by default. InfluxDB. TimescaleDB, which is an extension to PostgreSQL. Columnar databases such as ClickHouse are often used for the same job.
Choose it for metrics, sensor readings and market data at high volume with a retention policy. Do not choose it for records that are corrected or updated by key, for anything transactional, or for modest volumes. A PostgreSQL table partitioned by month handles a few thousand points per second without trouble.
Object storage
The data model. A bucket holds objects, and each object is a blob of bytes with a key and some metadata. The namespace is flat: a key such as photos/2026/42.jpg looks like a path, but folders are only key prefixes. You write and read whole objects over HTTP. To change an object, you replace it.
Example systems. Amazon S3, Google Cloud Storage, Azure Blob Storage, and MinIO for self-hosting.
Durability. S3 is designed for 99.999999999% durability of objects over a year, often called eleven nines. The standard storage class keeps redundant copies across at least three availability zones. Durability is not availability: the same class is designed for 99.99% availability. Neither protects you from your own deletes, which is what versioning is for.
Consistency. Since December 2020, S3 gives strong read-after-write consistency: once a write succeeds, every later read and list reflects it. Before then, overwrites and listings were eventually consistent, so older designs contain workarounds that are no longer needed. There are still no transactions across objects.
Multipart upload. A large object is uploaded as parts, in parallel, and a failed part is retried on its own. A final call assembles the parts. In S3 a single PUT is limited to 5 GB, parts are 5 MB to 5 GB, and an upload can have up to 10,000 parts. Unfinished uploads are billed until they are aborted, so add a lifecycle rule that aborts them after a few days.
Presigned URLs. Your server signs a URL that permits one operation on one key for a short time. The client then uploads or downloads directly, and the bytes never pass through your fleet.
Step 1 of 8
A user wants to upload a 2 GB video. Sending it through the API servers would hold a connection and memory on your own fleet for minutes, and one dropped connection would restart the whole upload.
Read the steps as text
- A user wants to upload a 2 GB video. Sending it through the API servers would hold a connection and memory on your own fleet for minutes, and one dropped connection would restart the whole upload.
- Instead, the app first asks the API for permission. The API checks who the user is and whether a file of this size and type is allowed. No file data is sent in this request.
- The API records a metadata row with the owner, the object key and the status pending. It starts a multipart upload with the storage service and signs one URL per part. Each URL permits one operation on one key and expires after a few minutes.
- The app splits the file into parts and sends them straight to object storage, several at a time. The API servers never see the bytes, so upload traffic does not compete with ordinary requests.
- One part fails on a weak mobile connection. Only that part is sent again. When every part has arrived, a completion call lists the parts in order and the storage service assembles them into a single object.
- The storage service emits an event for the new object. A worker takes it from a queue, checks the real size and type, and does any processing, such as a virus scan or transcoding.
- The worker marks the metadata row as ready. Only now does the file appear in other users' queries. Uploads that never finish stay pending, and a clean-up rule removes their abandoned parts so they stop costing money.
- The bytes live in object storage, the facts about them live in a database, and the object key links the two. Downloads work the same way in reverse, through a presigned GET or a CDN in front of the bucket.
Metadata lives elsewhere. Object storage can fetch by key and list by prefix, 1,000 keys per page in S3. It cannot answer “this user’s newest 20 photos”. Keep a row per object in a database with the owner, object key, size, type, checksum and status. The database is the source of truth for what exists. Serve popular content through a CDN. The file sync and video streaming case studies build on this pattern.
Choose it for images, video, documents, backups, logs and data-lake files. Do not choose it for small records that change often, for lookups that need single-digit millisecond latency, or for files that must be edited in place or appended to.
A decision guide for this module
| You need | Reach for | Not when |
|---|---|---|
| Relationships, constraints, transactions, ad hoc queries | Relational: PostgreSQL, MySQL | Writes exceed one primary and access is by key |
| Lookup by exact key at very high throughput | Key-value: Redis, DynamoDB | You must query by value |
| Self-contained records of varying shape | Document: MongoDB | Data is highly connected |
| Very high write volume, time-ordered reads per key | Wide-column: Cassandra, HBase | Queries are ad hoc, or deletes are heavy |
| Traversals over several hops | Graph: Neo4j, Neptune | Queries are one hop |
| Metrics and events by time, with retention | Time-series: Prometheus, TimescaleDB | Records are updated by key |
| Large binary objects | Object storage plus a metadata database | Records are small and hot |
Two more stores appear in many designs as copies derived from the system of record: a search index for full-text queries, and a columnar warehouse for analytics. The criteria behind the table are in SQL vs NoSQL.
Start with the fewest stores that meet the requirements. Each one you add is another system to operate, back up, secure and keep consistent with the rest.
In the interview
These stores come up when one part of the data does not look like the rest. Name the shape of the data, then the store, then what stays in the main database.
A strong answer takes about a minute: “Video files are large and immutable, so they go to object storage. The client uploads with multipart through presigned URLs, and a metadata row in PostgreSQL holds the owner, the object key and a status that flips to ready after processing. Playback goes through a CDN. View counts per minute are time-series data, so I would use a time-series store with a 30-day retention at full resolution. The follow graph is one hop, so it stays in an indexed table. A graph database would only earn its place if we served multi-hop recommendations online.”
Key takeaways
- A graph database makes each hop along a relationship cheap, so it suits queries that follow several hops or a variable number of them.
- One-hop queries such as listing followers do not need a graph database. An indexed edge table is enough.
- Time-series databases exploit the shape of the data: append-only, ordered by time, read as ranges, expired by age. They partition by time and compress heavily.
- Tag cardinality is the main way to break a time-series database. Never put a user id or request id in a tag.
- Object storage holds large immutable blobs cheaply and durably. Keep the metadata in a database and link the two by object key.
- Clients should upload to and download from object storage directly, using presigned URLs and multipart upload, so the bytes never pass through your servers.
Check yourself
0 of 5 answered
Flashcards
These 6 cards are in your review deck. Tap a card to see the answer.
What problem does a graph database solve?
Queries that follow relationships across several hops or to a variable depth. It stores each node's relationships with the node, so the cost depends on the part of the graph visited and not on its total size.
When should you not use a graph database?
When queries are one hop, when the data needs to be partitioned across many machines for write scale, or when the work is analytics over the whole graph. Use an edge table, or batch graph processing.
What four properties of time-series data do specialised stores exploit?
It is append-only, arrives roughly in time order, is read as time ranges and aggregates, and expires by age.
What is cardinality in a time-series database?
The number of distinct series, meaning unique combinations of metric name and tag values. High-cardinality tags such as user ids make the series index explode.
What consistency does Amazon S3 provide?
Strong read-after-write consistency for puts, overwrites, deletes and lists, since December 2020. It has no transactions across objects.
Why split blobs and metadata?
Object storage is cheap and durable for bytes but can only fetch by key or list by prefix. A database holds owner, size, status and the object key, and answers the queries.