Write libraries instead of services, where possible#
Background#
Spencer Baugh, better known as catern, has some interesting thoughts on their blog. One in particular that I like is their idea to write libraries instead of services, where possible. It is short piece, so I highly recommend reading it in full.
Definitions#
First, lets start with some definitions, as I understand them:
- library
-
software run by the user on their machines, i.e. "shared objects, modules, servers, command line utilities, and others"1 (emphasis mine)
- service
-
software the user cannot run, and must rely on the service provider to run (probably through an API)
- administration costs
-
the cost of maintaining a library or service, such as the cost of running the software, the cost of updating the software, and the cost monitoring the software
The Directive#
Whenever you possibly can, write a library instead of a service, i.e., you should write software that the user can run on their own machine, without needing to interact with a central service.
Why Do This?#
There are a few reasons to write libraries instead of services:
- Users can upgrade at their own pace: Users do not share state, so different users can use different versions of the library without conflict. Users who are slower to upgrade do not hold back users who are faster to upgrade.
- Lower Administration Costs: The user bears the burden of running the software, not the software provider. The provider does not need to worry about how/where the software runs, how to monitor it, or when to update it.
- Providers can focus on the software functionality: The provider has lower administrative costs, so they have more time to focus on providing functionality of the software. This means that the software can be more feature-rich than if they ran a service.
A Few Notes#
It’s Not Always Possible to Write Libraries#
Catern lacks examples in their post of when to write and not to write libraries, but they also make it clear that this is not always possible. Sometimes, the software needs to be run via a central service, and that is okay. The directive is a little bit clickbaity, but once broken down, it makes more sense.
It Might Actually Be Possible to Write a Library#
By Catern's definition, a library is just code that must be run by the user. This means that a library can be a server, or even a database. So if the only reason you are writing a service is because it needs to interact with a database etc, you might still be able to get away with writing a library. The library would just have to handle setting up the database before providing other functionality.
Wrap a Library With a Service#
If you do need to write a service, a few people have suggested wrapping the library in a service.234 This provides the benefits of a library, while still providing the benefits of a service. A library can be unit tested easily, and be distributed to users to run on their own machines. And users that would prefer a service could opt for that.
Other Resources#
- A Hacker News post from March 2021
- A Hacker News post from November 2023