When we chat about serverless, we often bring up Knative, AWS Lambda, and Azure Functions. Let's not forget Google's version, although I will keep it simple in this article. These tech toys might seem like they compete with each other, but from a clean architecture angle, they are not exactly playing the same game. If you’re new to clean architecture, feel free to check out the article, Implementing clean architecture solutions: A practical example for reference.
My perspective of clean architecture
Here's the deal... When I put on my clean architecture glasses, Knative fits snugly in the Kubernetes playground, making it a core part of your platform (Figure 1). In this setup, the platform itself becomes sort of like an application within the clean architecture model, which is similar to the positioning of Red Hat OpenShift within the hypervisors landscape as described in my previous article.
On the flip side, AWS Lambdas, Azure Functions, and their buddies are more about handling the nitty-gritty infrastructure. They're all about storage, triggers, and other techy bits (Figure 2).
So, if we map these serverless pals to a clean architecture-style setup, I go back to that onion layer concept we talked about before (see Figure 3). AWS Lambda, Azure Functions, and their gang become part of the infrastructure/data providers layer, which is kind of like the outer layer of the onion. But Knative, it hangs out in the core layer like the cool kid in town.
The cool thing is that Knative isn't tied to any specific vendor, so you can shift it around – from your own servers to AWS, from AWS to Google, or even to Azure – without messing with the core layer. All the action happens in the infrastructure layer, keeping things neat and tidy. Therefore, this setup lines up with the clean architecture vibes. You can spot this in the illustration below, where we tap into the built-in connection between S3 buckets and AWS Lambda to kickstart a Knative function.
The blue box is tailored to a specific hypervisor (like AWS in this case), while the green one is flexible and works with any hypervisor. If you ever decide to switch to Azure, all you need to do is recreate the blue box in Azure and copy and paste the green part there too. Since the green part is our clean architecture core layer,"shifting functions between hypervisors becomes a breeze (quick and hassle-free).
Implement clean architecture
If you want to give clean architecture a try, go explore my demo code on GitHub. Please feel free to comment below. We welcome your feedback.