Debian Codegraph @

www/src/content/essays/two-approaches-to-decoupling.md

raw symbols SCIP
1 ---
2 title: "Two Approaches To Decoupling"
3 description: "Carson Gross explores two different approaches to decoupling in web applications: decoupling at the application level using a JSON Data API and decoupling at the network architecture level using a hypermedia API. He discusses the trade-offs between the two methods, highlighting how a hypermedia API, despite introducing tighter coupling at the application level, offers greater resilience to change at the system level. Carson also touches on the limitations of each approach and discusses strategies like GraphQL and splitting APIs to address specific challenges in web development."
4 created: 2022-05-01
5 modified: 2022-05-01
6 authors: ["Carson Gross"]
7 tags: ["foundations"]
8 ---
9
10 > The central feature that distinguishes the REST architectural style from other network-based styles is its emphasis on
11 > a uniform interface between components. By applying the software engineering principle of generality to the component
12 > interface, the overall system architecture is simplified and the visibility of interactions is improved.
13 > Implementations are decoupled from the services they provide, which encourages independent evolvability.
14
15 _-Roy Fielding, <https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_5>_
16
17 In this essay we will look at two different types of decoupling in the context of web applications:
18
19 * Decoupling at the _application level_ via a generic JSON Data API
20 * Decoupling at the _network architecture level_ via a hypermedia API
21
22 We will see that, at the application level, a hypermedia API tightly couples your front-end and back-end. Despite this
23 fact, surprisingly, the hypermedia API is in fact more resilient in the face of change.
24
25 ## Coupling
26
27 [Coupling](https://en.wikipedia.org/wiki/Coupling_%28computer_programming%29) is a property of a software system in
28 which
29 two modules or aspects of the system have a high degree of interdependence. _Decoupling_ software is the act of reducing
30 this
31 interdependence between unrelated modules so that they can evolve independently of one another.
32
33 The concept of coupling and decoupling is closely (and inversely) related to
34 [cohesion](https://en.wikipedia.org/wiki/Cohesion_(computer_science)). Highly cohesive software has related logic
35 within a module or conceptual boundary, rather than spread out throughout a codebase. (A related concept is our own
36 idea
37 of [Locality of Behavior](/essays/locality-of-behaviour))
38
39 Broadly, experienced developers strive for decoupled and cohesive systems.
40
41 ## JSON Data APIs - Application Level Decoupling
42
43 A common approach to building web applications today is to create a JSON Data API and then consume that JSON API using
44 a JavaScript framework such as React. This application-level architectural decision decouples the front-end code
45 from the back-end code, and allows the reuse of the JSON API in other contexts, such as a mobile applications, 3rd
46 party client integrations, etc.
47
48 This is an _application-level_ decoupling because the decision and implementation of the decoupling is done by the
49 application developer themselves. The JSON API provides a "hard" interface between the two pieces of software.
50
51 Using my favorite example, consider a simple JSON for a bank that has a `GET` end point at
52 `https://example.com/account/12345`.
53 This API might return the following content:
54
55 ```json
56 HTTP/1.1 200 OK
57
58 {
59 "account": {
60 "account_number": 12345,
61 "balance": {
62 "currency": "usd",
63 "value": -50.00
64 },
65 "status": "overdrawn"
66 }
67 }
68 ```
69
70 This Data API can be consumed by any client: a web application, a mobile client, a third party, etc. It is, therefore
71 decoupled from any particular client.
72
73 ### Decoupling Via A JSON API In Practice
74
75 So far, so good. But how does this decoupling work out in practice?
76
77 In our essay [Splitting Your Data & Application APIs: Going Further](https://htmx.org/essays/splitting-your-apis/) you
78 will find the following quote:
79
80 > The worst part of my job these days is designing APIs for front-end developers. The conversation goes inevitably as:
81 >
82 > Dev – So, this screen has data element x,y,z… could you please create an API with the response format {x: , y:, z: }
83 >
84 > Me – Ok
85 >
86 > Jean-Jacques Dubray - <https://www.infoq.com/articles/no-more-mvc-frameworks>
87
88 This quote shows that, although we have driven coupling out with a pitchfork (or, in our case, with a JSON API) it has
89 come
90 back through requests for web application-specific JSON API end points. These sorts of requests end up recoupling the
91 front-end and back-end code: the JSON API is no longer providing a generic JSON Data API, but rather a specific API for
92 the front-end needs.
93
94 Worse, these front-end needs will often change frequently as your application evolves, necessitating the modification
95 of your JSON API. What if other non-web application clients have come to depend on the original API?
96
97 This problem leads to the "versioning hell" that many JSON Data API developers face when supporting both web
98 applications as well
99 as other non-web application clients.
100
101 #### A Solution: GraphQL
102
103 One potential solution to this problem is to introduce [GraphQL](https://graphql.org/), which allows you to have a much
104 more expressive JSON API. This means that you don't need to change it as often when your API client's needs change.
105
106 This is a reasonable approach for addressing the problem outlined above, but there are problems with it. The biggest
107 issue that we see is security, as we outline this
108 in [The API Churn/Security Trade-off](https://intercoolerjs.org/2016/02/17/api-churn-vs-security.html) essay.
109
110 Apparently facebook uses a [whitelist](https://twitter.com/AdamChainz/status/1392162996844212232) to deal with the
111 security
112 issues introduced by GraphQL, but many developers who are using GraphQL appear to not understand the security threats
113 involved with it.
114
115 #### Another Solution: Splitting Your Application & General Data APIs
116
117 Another approach recommended by [Max Chernyak](https://max.engineer/) in his article
118 [Don’t Build A General Purpose API To Power Your Own Front End](https://max.engineer/server-informed-ui), is to build
119 *two* JSON APIs:
120
121 * An application specific JSON API that can be modified as needed
122 * A general purpose JSON API that can be consumed by other clients such as mobile, etc.
123
124 This is a pragmatic solution to address what appears to be the _inherent_ coupling between your web application's
125 front-end
126 and the back-end code supporting it, and it doesn't involve the security tradeoffs involved in a general GraphQL API.
127
128 ## Hypermedia - Network Architecture Decoupling
129
130 Now let us consider how a _hypermedia API_ decouples software.
131
132 Consider a potential response to the same `GET` for `https://example.com/account/12345` that we saw above:
133
134 ```html
135 HTTP/1.1 200 OK
136
137 <html>
138 <body>
139 <div>Account number: 12345</div>
140 <div>Balance: $100.00 USD</div>
141 <div>Links:
142 <a href="/accounts/12345/deposits">deposits</a>
143 <a href="/accounts/12345/withdrawals">withdrawals</a>
144 <a href="/accounts/12345/transfers">transfers</a>
145 <a href="/accounts/12345/close-requests">close-requests</a>
146 </div>
147 <body>
148 </html>
149 ```
150
151 (Yes, this is an API response. It just happens to be a hypermedia-formatted response, in this case HTML.)
152
153 Here we see that, at the application level, this response could not be more tightly coupled to the "front-end". In fact,
154 it *is* the front-end, in the sense that the API response specifies not only the data for the resource, but also
155 provides
156 layout information on how, exactly, to display this data to the user.
157
158 The response also contains _hypermedia controls_, in this case, links, that an end user can select from to continue
159 navigating the hypermedia API that
160 this [Hypermedia-Driven Application](https://htmx.org/essays/hypermedia-driven-applications/) provides.
161
162 So, where is the decoupling in this case?
163
164 ### REST & The Uniform Interface
165
166 The decoupling in this case is occurring at a _lower level_. It is happening at the _network architecture_ level, which
167 is to say, at the system level. [Hypermedia systems](https://hypermedia.systems) are designed to decouple the
168 hypermedia
169 client (in the case of the web, the browser) from the hypermedia server.
170
171 This is accomplished primarily via the Uniform Interface constraint of REST and, in particular, by using
172 Hypermedia As The Engine of Application State ([HATEOAS](/essays/hateoas)).
173
174 This style of decoupling allows tighter coupling at the higher application level (which we have seen may be an
175 _inherent_ coupling) while still retaining the benefits of decoupling for the overall system.
176
177 ### Decoupling Via Hypermedia In Practice
178
179 How does this sort of decoupling work in practice? Well, let's say that we wish to remove the ability to transfer money
180 from our bank to other banks as well as the ability to close accounts.
181
182 What does our hypermedia response for this `GET` request now look like?
183
184 ```html
185 HTTP/1.1 200 OK
186
187 <html>
188 <body>
189 <div>Account number: 12345</div>
190 <div>Balance: $100.00 USD</div>
191 <div>Links:
192 <a href="/accounts/12345/deposits">deposits</a>
193 <a href="/accounts/12345/withdrawals">withdrawals</a>
194 </div>
195 <body>
196 </html>
197 ```
198
199 You can see that in this response, links for those two actions have been removed from the HTML. The browser simply
200 render the new HTML to the user. To a rounding error, there are no clients sitting around using the _old_ API. The
201 API is encoded within and discovered through the hypermedia.
202
203 This means that we can dramatically change our API without breaking our clients.
204
205 This flexibility is the crux of the REST-ful network architecture and, in particular, of [HATEOAS](/essays/hateoas).
206
207 As you can see, despite much tighter _application-level_ coupling between our front-end and back-end, we actually have
208 more flexibility due to the _network architecture_ decoupling afforded to us by the Uniform Interface aspect of
209 REST-ful [hypermedia systems](https://hypermedia.systems).
210
211 ### But That's A Terrible (Data) API!
212
213 Many people would object that, sure, this hypermedia API may be flexible for our web application, but it makes for a
214 terrible general purpose API.
215
216 This is quite true. This hypermedia API is tuned for a specific web application. It would be cumbersome and error-prone
217 to try to download this HTML, parse it and try to extract information from it. This hypermedia API only makes sense as
218 part
219 of a larger hypermedia system, being consumed by a proper hypermedia client.
220
221 This is exactly why we recommend creating a general purpose JSON API alongside your hypermedia API in
222 [Splitting Your Data & Application APIs: Going Further](https://htmx.org/essays/splitting-your-apis/). You can
223 take advantage of the flexibility of hypermedia for your own web application, while providing a
224 general purpose JSON API for mobile applications, third party applications, etc.
225
226 (Although, we should mention, a [hypermedia-based mobile application](https://hyperview.org) might be a good choice
227 too!)
228
229 ## Conclusion
230
231 In this essay we looked at two different types of decoupling:
232
233 * Application level decoupling via a JSON Data API
234 * Network-architecture decoupling via REST/HATEOAS in a hypermedia system
235
236 And we saw that, despite the tighter application-level coupling found in a hypermedia-based application, it is the
237 hypermedia system that handles changes more gracefully.