DEV Community

Derek mwale
Derek mwale

Posted on

Data Structures That Made My APIs Faster

When developers talk about making APIs faster, the conversation usually goes straight to:

  • Adding more servers
  • Increasing database resources
  • Adding caching
  • Optimizing queries
  • Using faster frameworks

Those things matter.

But sometimes the biggest performance improvement doesn't come from infrastructure.

It comes from choosing the right way to store and access data.

The data structures inside your application quietly decide how fast your API responds.

A bad data structure can turn a millisecond operation into a second-long bottleneck.

A good one can handle millions of operations effortlessly.

Here are the data structures that have had the biggest impact on building faster APIs.


1. Hash Maps: The King of Fast Lookups

One of the most useful data structures in backend development is the hash map.

You probably use it every day without realizing it.

In many languages:

dict

Enter fullscreen mode Exit fullscreen mode
Object

Enter fullscreen mode Exit fullscreen mode
HashMap

Enter fullscreen mode Exit fullscreen mode
HashMap

Enter fullscreen mode Exit fullscreen mode

They all solve the same problem:

"How do I find something quickly?"

Imagine you have an API that retrieves users.

A beginner approach might look like:

users = [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"},
    {"id": 3, "name": "Charlie"}
]


def find_user(id):
    for user in users:
        if user["id"] == id:
            return user
Enter fullscreen mode Exit fullscreen mode

This works.

Until you have millions of users.

Every search requires scanning the entire list.

The complexity is:

O(n)
Enter fullscreen mode Exit fullscreen mode

A hash map changes everything.

users = {
    1: {"name": "Alice"},
    2: {"name": "Bob"},
    3: {"name": "Charlie"}
}


user = users[2]
Enter fullscreen mode Exit fullscreen mode

Now the lookup is:

O(1)
Enter fullscreen mode Exit fullscreen mode

Constant time.

The difference between searching one million records and instantly finding one record is enormous.


Real API Example: User Sessions

Imagine an authentication service.

Every request contains:

Authorization: Bearer token123
Enter fullscreen mode Exit fullscreen mode

Your API needs to find the user.

A bad design:

Request
 |
Search all sessions
 |
Find token
 |
Return user
Enter fullscreen mode Exit fullscreen mode

A better design:

Hash Map

token123 → User ID 5421
token456 → User ID 8821
token789 → User ID 9912
Enter fullscreen mode Exit fullscreen mode

Now authentication becomes extremely fast.

This is why caching systems like Redis are built around key-value lookups.


2. Queues: The Backbone of Scalable Systems

Many APIs fail because they try to do everything immediately.

Example:

A user uploads a video.

Your API tries to:

  1. Save the video
  2. Compress it
  3. Generate thumbnails
  4. Send notifications
  5. Update analytics

All before responding.

The user waits.

The server struggles.

A queue changes the architecture.

API Request

      |

      ▼

Add Job To Queue

      |

      ▼

Return Response

      |

      ▼

Background Workers Process Tasks
Enter fullscreen mode Exit fullscreen mode

A queue follows:

First In
First Out
Enter fullscreen mode Exit fullscreen mode

The oldest job gets processed first.

Queues power:

  • Email systems
  • Payment processing
  • Video processing
  • Notification systems
  • Data pipelines

A good API doesn't always work harder.

Sometimes it simply moves work somewhere smarter.


3. Trees: Making Search More Efficient

Trees appear everywhere in backend systems.

Databases use tree structures internally.

File systems use trees.

Search engines use trees.

A common example is the B-Tree index.

Imagine this query:

SELECT *
FROM users
WHERE email='derek@example.com';
Enter fullscreen mode Exit fullscreen mode

Without an index:

The database scans every row.

User 1
User 2
User 3
...
User 10,000,000
Enter fullscreen mode Exit fullscreen mode

With an index:

              M
           /     \
        A-M       N-Z
       /             \
   Derek          Sarah
Enter fullscreen mode Exit fullscreen mode

The database doesn't search everything.

It follows a path.

That is the magic of trees.


4. Sets: Eliminating Duplicate Work

Sets are underrated.

They solve a very common backend problem:

"I only want unique values."

Example:

A social media API recommends users to follow.

You collect recommendations from:

  • Friends
  • Interests
  • Location
  • Trending users

The same person might appear multiple times.

Without a set:

John
Sarah
John
Michael
Sarah
Enter fullscreen mode Exit fullscreen mode

With a set:

John
Sarah
Michael
Enter fullscreen mode Exit fullscreen mode

Instant cleanup.

Sets are useful for:

  • Removing duplicates
  • Permission checks
  • Feature flags
  • User relationships
  • Tracking processed events

5. Stacks: The Hidden Power Behind APIs

Stacks follow:

Last In
First Out
Enter fullscreen mode Exit fullscreen mode

The newest item is handled first.

You might not build a stack manually often, but they power many important systems.

Examples:

  • Function calls
  • Undo systems
  • Expression evaluation
  • Request processing

A practical API example:

Imagine processing nested permissions:

Admin

  └── Manager

        └── Employee
Enter fullscreen mode Exit fullscreen mode

The system can use stack-based traversal to evaluate access rules.


6. Graphs: When Relationships Matter

Modern applications are built on relationships.

Social networks.

Maps.

Recommendations.

Payments.

All of these can be modeled as graphs.

Example:

Derek

 |
 |

Alice ---- Bob

 |
 |

Charlie
Enter fullscreen mode Exit fullscreen mode

A graph contains:

  • Nodes
  • Connections

Social media:

User → User
Enter fullscreen mode Exit fullscreen mode

Banking:

Account → Transaction → Account
Enter fullscreen mode Exit fullscreen mode

Travel:

Airport → Flight → Airport
Enter fullscreen mode Exit fullscreen mode

Recommendation engines are basically graph problems.


7. Priority Queues: Handling What Matters First

Not every task has equal importance.

A payment failure notification should probably happen before a marketing email.

Priority queues solve this.

Example:

High Priority

Payment Failed

Password Reset


Medium Priority

Order Update


Low Priority

Newsletter
Enter fullscreen mode Exit fullscreen mode

The system always processes important tasks first.

They power:

  • Cloud scheduling
  • Background jobs
  • Gaming matchmaking
  • Distributed systems

Data Structures Are Architecture Decisions

A common mistake is thinking:

"Data structures are only for coding interviews."

They are not.

They are architecture decisions.

Every time you build:

  • An API
  • A database
  • A queue system
  • A search feature
  • A recommendation engine

you are choosing how information moves.

The choice determines:

  • Speed
  • Scalability
  • Memory usage
  • Complexity

The Best Backend Engineers Think In Data Structures

A junior developer asks:

"How do I implement this feature?"

An experienced engineer asks:

"What is the right way to represent this problem?"

That difference matters.

A slow API is often not slow because the server is weak.

Sometimes the problem is that the application is searching through lists when it should be using maps.

Or doing synchronous work when it should use queues.

Or scanning everything when it should use indexes.


Final Thoughts

The fastest systems are not always built with the newest technologies.

Sometimes the biggest performance improvements come from understanding the fundamentals.

Hash maps for instant lookups.

Queues for background processing.

Trees for efficient searching.

Sets for uniqueness.

Graphs for relationships.

Priority queues for intelligent scheduling.

Data structures are not just computer science theory.

They are the building blocks behind every fast API, scalable platform, and reliable system we use today.

Before adding more servers, ask a simpler question:

"Am I storing and accessing my data the right way?"

Sometimes the fastest optimization is changing the way you think.

Top comments (0)