Designing APIs
Damir Svrtan
Less Data is More
Damir Svrtan
He/him
Senior Software Engineer @ Netflix
Twitter: @DamirSvrtan
San Francisco 🇺🇸
Zagreb 🇭🇷
Designing APIs
Less Data is More
Avoiding overhead when
designing APIs
Bloated overly flexible APIs
What kind of APIs?
• HTTP based APIs
• REST APIs, JSON APIs, GraphQL etc
API technology agnostic
Applicable for all kinds of APIs
Blogging Platform
Blogging Platform:
• Authors can release posts
• Posts can have comments
Two principles for
building APIs
• Designing the minimal API surface
• Designing from Strict to Loose
1st Principal
Design the minimal API surface
Bloated API surface:
- redundant fields
- redundant relationships
- redundant input fields
Don’t Expose Redundant
Fields
Requirement:
“Show the author of the blogpost”
CREATE TABLE authors (
id
first_name
last_name
email
avatar_url
);
Friendly API developer:
“Let’s expose the email, someone might find it
useful in the future”
type Author {
id
firstName
lastName
avatarUrl
email
}
6 months later:
Privacy reasons: stop exposing the email
..deprecation cycle..
..extreme amounts of communication..
..breaking clients..
..extreme amounts redundant cycles..
Don’t Expose Redundant
Relationships
Requirement:
“Indicate whether a post was reviewed”
type Post {
title:String
body:String
}
type Post {
title:String
body:String
reviewed:Boolean
}
Friendly API developer:
“Let’s expose the reviewer for future use
cases”
type Post {
title:String
text:String
reviewed:Boolean
reviewer: Reviewer
}
Development costs are growing;
- batch/eager loading
- testing
- performance impacts
type Post {
title:String
text:String
reviewed:Boolean
reviewers: [Reviewer]
}
type Post {
title:String
text:String
reviewed:Boolean
reviewer: Reviewer
}
..deprecation cycle..
..breaking clients..
Delaying Decisions
Adding later when we have more knowledge is better
It’s easier to add in the future than to
remove
Don’t Expose Redundant
Input Fields
Requirement:
“Readers need to be able to create and
update comments”
input CreateComment {
postId: ID
body: String
}
The friendly API developer:
“Let’s have parity between inputs”
input UpdateComment {
id: ID
postId: ID
body: String
}
The blogging platform doesn’t support
comment transitions from one post to another
input UpdateComment {
id: ID
postId: ID
body: String
}
Avoid anemic data modeling
Ambiguity deteriorates
Developer Experience
YAGNI
You Ain’t Gonna Need It
2nd Principal
Strict to Loose
- Avoid extra flexibility
- Breaks First!
Avoid extra flexibility
Requirement:
“The users need to be able to see comments
on a post”
comments(postId: ID @required)
The friendly API developer:
“Our clients might want to fetch all comments
in the future”
comments(postId: ID @optional)
Unneeded application logic
If postId.present?
…
else
…
end
More code = More maintenance
More code = More tests
On inputs, it’s easy to go from required to
optional, not the other way around
Strict to Loose gives you more
control
Loose to Strict
is a breaking change
Breaks First!
Requirement:
“Ability to fetch comments for a post”
The friendly API developer:
“Let’s just give them an array of comments,
we don’t need pagination!”
The friendly API developer:
“Let’s just give them an array of comments,
we don’t need pagination yet!”
comments(postId: ID): [Comment]
The friendly API developer:
“We won’t have more than 5 comments per
post!”
6 months after:
“There’s posts with 100’s of comments”
Developer:
“Let’s add pagination”
comments(postId: ID): PaginatedComments
Work is done, let’s go home!
comments(postId: 1, first: 1000)
The Friendly API Developer didn’t
add limits to pagination
Add pagination limitations from the
beginning
Error Message:
Requesting 1000 records exceeds the `first`
limit of 100 records.
Super hard to add retroactively, but super
easy to adjust in the future.
How to avoid these overheads?
Adopt Schema First Design
Talk to your clients
Be responsive to your clients
requests
There’s always exceptions
Platform APIs
Constraints due to release
cycles
Conclusion
Redundant work slows down
progress on important features
“The road to hell is paved with good
intentions”
Thank you! 👋
Twitter: @DamirSvrtan

apidays LIVE Hong Kong 2021 - Less Data is More by Damir Svrtan, Netflix