Practice: Avoid special handle
Published: 2023-07-09 08:11:42

## what is Serverless?
Build and run applications without thinking about servers

- automatic scaling
- high availability
- pay-for-use billing model
- event-driven compute

----
## what is api gateway?
Create, maintain, and secure APIs at any scale

- concurrent API calls
- traffic management
- CORS support
- authorization and access control
- throttling
- monitoring
- API version management

----
## Overall structure


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5oclc4tvokqrkenee2h.png)

----
## Background
- service provider
- 30 merchants caller our service
- every merchants are different
- some custom feature for one merchant only
- apply special handling to code base
- code base is mess
- no documentation
- no people know why need the sepcific handling

----
## Real situation

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2j58fc2bk71uydo239hi.png)

----
## Suggestion
- Stop add special handling

----
## Drawback of special handling
- hard to add new feature
- hard to understand
- code base is mess

----
## common operational issue of special handling

**Issue 1**
- merchant ask you to change special hanlding on your lambda service
- no way to say no becuase you take responsibility to finish merchant job

**Issue 2**
- add new feature but suddenly run special handling for specific merchant
- why?
- If the value is specific prefix, run that special handling.
- But I cannot change the value. What can I Do?

**Issue 3**
- merchant say our serive is too slow and we need to make the servive faster.
- What is the side effect for? I cannot change it becuase I don't know it.
- Merchant say they don't know it too.

You can imagine the crazy thing will keep going.

----
## Not good Solution 1

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/io99hdt0pfzqr9oxrgsp.png)

- create api endpoint suffix merchant special id
- Separate the special handle to different lambda function
- more complex to understand the system
- merchant still ask you to update the special handling
- still don't know the needs of special handling
- system will go mess

----
## Not good Solution 2

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bh4p0y59azf013ykmy3a.png)

- every merchant have their specific lambda function
- on boarding merchant take long time
- a lot of infrastructure that need to manage
- merchant still ask you to update the special handling
- still don't know the needs of special handling
- system will go mess

----
## Good Solution

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2tlqfnvbegyki7caz2m.png)

- change Custom validate for merchant to new optional validate featue
- the feature is design by the developer
- merchant use the new optional feature to fuifull their business logic
- good documentation for merchant to implement API endpoint
- know the purpose of feature
- other merchants can use the same feature

- change Custom prefix to run side effect action to sending notifiy 
- notify merchant the feature is running
- merchant handle their job
- avoid implement merchant business logic on our system

- change response specific output to standard error code
- merchant know the root cause of error
- merchant handle the error flow
- avoid to implement some unexpected output on system