Enqueue Zero Become a Patron!

12-factor

21st July 2018 at 9:21pm

Context

Software as a service, or Saas, requires the application to be built with portability and resilience when porting itself as a web service. The application can vary from functions to forms but they can share some same design principles to become modern web-based services.

Introduction

12-Factor manifesto was first published in 2011 and widely adopted by many applications. Note that the factors introduced don't necessarily to be applied all at once.

Solutions

Below table show all factors:

FactorNameDescriptionMemo
ICodebaseOne codebase tracked in revision control, many deploysPull all code in Git or SVN or HG, etc.
IIDependenciesExplicitly declare and isolate dependenciesWrite all software dependencies in a file such as requirements.txt, Gemfile etc, and vendor system tools
IIIConfigStore config in the environmentRead config in the form like redis_url = os.environ['REDIS_URL'] in the application
IVBacking servicesTreat backing services as attached resourcesRead service reference in the config, such as mysql://localhost:3306, memcache://10.0.1.1:10010, redis://192.168.10.1:6379/0, etc
VBuild, release, runStrictly separate build and run stagesTurning the code into a package (build) somewhere first, and then install the package (release) in production
VIProcessesExecute the app as one or more stateless processesMake sure the application can run standalone on every server
VIIPort bindingExport services via port bindingExpose your service in the form like ip:port
VIIIConcurrencyScale-out via the process modelRun application in a set of different types, such as web processes, worker processes, and clock process
IXDisposabilityMaximize robustness with fast startup and graceful shutdownStart and shut down quickly
XDev/prod parityKeep development, staging, and production as similar as possibleUse vagrant or docker to diminish the difference among different environments
XILogsTreat logs as event streamsWrite logs and errors into stdin and stderr
XIIAdmin processesRun admin/management tasks as one-off processesProvide CLI for application
  • Advantages
    • Can be applied to any programming languages.
    • Can mix with different backend services, such as databases, queues, cache system, etc.
  • Disadvantages
    • Some rules such as Config, Logs are not must-have in some cases.

Conclusion

12-Factor methodology describes how to turn your application into a robust, reliable and least-astonishment web service. Some of the rules are absorbed in web frameworks or online platforms. Although some cause debates, it has spread widely. Choose the factors you like when porting your application as service!

References