Lesson 7 of 9 Data Engineering Practices

Applying Data Structures and Algorithms to Data Systems

This lesson explains how the data structures and algorithms of computer science underpin databases, query engines and pipelines, and help data engineers predict how operations perform at scale.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the data structures behind indexes, joins and caches.
  • Compare operations by their complexity.
  • Model pipelines as directed acyclic graphs.

Data structures in data systems

Familiar data structures explain how data systems behave. Database indexes are usually B-trees, which keep keys sorted so that lookups and range queries are fast. Hash tables underlie hash joins, where one table is loaded into a hash table and the other is probed against it, and hash partitioning, which spreads rows across nodes by key. Graphs represent pipelines as DAGs, data lineage, and vector indexes such as HNSW. Trees represent hierarchies and nested documents, and queues and stacks buffer work.

A Bloom filter is a compact structure that can say a value is definitely not present, or might be. Parquet files and databases use them to skip files or blocks that cannot contain a searched value. The following table summarises where each structure appears.

StructureUsed in
B-treesDatabase indexes
Hash tablesHash joins and hash partitioning
GraphsPipelines as DAGs, data lineage, vector indexes such as HNSW
TreesHierarchies and nested documents
Queues and stacksBuffering work
Bloom filtersSkipping files or blocks that cannot contain a value
Match the pairs

Data structures

Exam skills 1.4.11

Match each data structure with where it is used.

Log in to try this activity Interactive activities are free with an account.

Complexity

Complexity describes how the cost of an operation grows with the amount of data, n. A hash lookup takes about the same time however large the table, O(1). A B-tree index lookup grows very slowly, O(log n), because each level of the tree narrows the search. Sorting grows slightly faster than the data, O(n log n). A nested loop join compares every row of one table with every row of the other, O(n × m), so joining two tables of a million rows means a trillion comparisons. The following table summarises the typical complexities.

OperationComplexity
Hash lookupO(1) on average
Index (B-tree) lookupO(log n)
SortingO(n log n)
Nested loop joinO(n × m)

In the cloud, inefficient algorithms directly increase compute cost.

The following diagram compares the number of comparisons needed to check records against a list and against a set.

Searching a list compared with looking up a set. Checking 200,000 records against a list of 50,000 blocked IDs scans the list for each record, about 10 billion comparisons. Converting the list to a set once, outside the handler, makes each check a hash lookup, about 200,000 in total.
The set is built once, outside the handler.
Match the pairs

Complexity

Exam skills 1.4.11

Match each operation with its typical complexity.

Log in to try this activity Interactive activities are free with an account.

Pipelines as graphs

Orchestrators such as Apache Airflow and AWS Step Functions model pipelines as directed acyclic graphs (DAGs). Each step is a node, each dependency is a directed edge from a step to the steps that depend on it, and the absence of cycles guarantees that the pipeline can finish, because no step can end up waiting for itself.

Two properties of DAGs matter in practice. A topological order of the graph, an ordering in which every step comes after all the steps it depends on, gives a valid sequence in which to run the steps. And steps with no dependency between them, independent branches of the graph, can run in parallel, which shortens the pipeline's total running time.

The following diagram shows a small pipeline as a DAG.

A pipeline as a directed acyclic graph. Each step is a node and each dependency an edge. A topological order runs every step after all the steps it depends on, and the two independent branches, cleaning orders and cleaning customers, run in parallel, which shortens the pipeline.
The steps are illustrative.
Worked example

A lookup that grows quadratically

A Python Lambda function matches each of 200,000 incoming records against a list of 50,000 blocked account IDs, using if account in blocked_list. It times out as the list grows. The following table compares the two approaches.

ApproachCost per lookupTotal comparisons
Search a listO(n)About 10 billion
Look up in a set (hash table)O(1) on averageAbout 200,000

Converting the list to a set once, outside the handler, makes the function finish in under a second. The same principle explains why databases use hash joins and indexes instead of nested loops.

Check yourself

How do Airflow and Step Functions model a pipeline?

Show answer

As a directed acyclic graph (DAG) of tasks and their dependencies.

Exam Tips

Remember for the exam

  • Database indexes are B-trees; hash joins use hash tables.
  • Airflow and Step Functions model pipelines as DAGs.
  • Sorting is O(n log n); hash lookups are O(1) on average.
  • HNSW vector indexes are graphs.
  • Avoid nested loop joins on large tables.