Advertisement
Advertisement

More Related Content

Slideshows for you(20)

Advertisement

Recently uploaded(20)

Advertisement

Serverless patterns

  1. 01
  2. 02
  3. 10-12 March 2021 03
  4. Let's start! 04
  5. What is serverless? 05
  6. Look ma, no servers! 06
  7. Serverless I do not want to care about the servers! “ 07
  8. Serverless may also mean Containerless/Dockerless Orchestratorless/Kubernetesless • • 08
  9. Serverless is not... Serverless != Costless Serverless != FaaS Serverless != Operationless • • • 09
  10. Serverless = Servicefull 10
  11. Compute models L1: Hardware machines L2: Virtual machines and hypervisors L3: Containers and orchestrators L4: Functions and services • • • • 11
  12. Serverless is... a spectrum (not a boolean) an operational construct a billing model (pay as you go) an architecture style (split your monolith) • • • • 12
  13. Idea to production 13
  14. Note Every organization that I worked with in the last 3 years runs at least one function-as-a-service in production in the cloud. “ 14
  15. FaaS 15
  16. Just code def handler_name(event, context): # implement logic return some_value 01. 02. 03. 16
  17. Just code? 17
  18. Built-in features Event system Logging and auditing Rate limits Auto-scaling Security controls Multiple versions Simplified deployment • • • • • • • 18
  19. Bought-in services Database-as-a-service Storage-as-a-service Messaging-as-a-service Function-as-a-service • • • • 19
  20. FaaS elements Function store Gateway (ingress, routing, certificates) Security Event bus GitOps experience • • • • • 20
  21. Patterns 21
  22. Strangler pattern You can always optimize and migrate slowly! 22
  23. Strangler pattern 23
  24. Platform 24
  25. Pattern: Cloud platform AWS GCP Azure IBM Cloud • • • • 25
  26. Pattern: Multi-vendor integration Cloudflare (Workers), Mailgun, MongoDB Atlas Airtable, AWS Lambda, Slack Amplitude, Aglolia, Akamai IFTTT, Google Sheets, Zeit (Vercel) Glitch, Firebase • • • • • 26
  27. Pattern: Hosted platform Custom built (Docker or Kubernetes-based) Kubeless OpenFaaS OpenWhisk • • • • 27
  28. Event 28
  29. Pattern: event-as-a-service 29
  30. Pattern: event-as-a-service HTTP call (browser call, web hook) Database event (DynamoDB, Firebase) Storage event (S3, GS) Queue/topic message (SQS, PubSub) Scheduled event (AWS Scheduled tasks, Google Scheduler) E-mail (Mailgun) • • • • • • 30
  31. Deployment 31
  32. Source: Lambda Trilogy 32
  33. Pattern: Single-purpose function 33
  34. Single-purpose function Small function that does one thing, does it only and does it well! 34
  35. Lambdalith (functionlith) 35
  36. Lambdalith Function implements several features, most likely responds to various endpoints. It may be a fully-blown web server. 36
  37. Function vs Service 37
  38. Function vs Service F: Fast to start, invoked on demand, uses limited resources, is not guarateed to preserve state. S: Always there (at least one instance), may have some long-living internal cache, background proceses, usually takes longer to start. 38
  39. Function vs Container 39
  40. AWS Fargate 40
  41. Google CloudRun 41
  42. On-prem/custom OpenFaaS Kubeless KNative • • • 42
  43. Pattern: Fat function 43
  44. Pattern: Fat function One code base, many (deployed) functions. 44
  45. Pattern: Routing function 45
  46. Pattern: Routing function Function is calling other functions (through event bus or through direct calls). 46
  47. Pattern: Routing function 47
  48. Pattern: Gateway Routing logic is delegated to a higher level service e.g. API Gateway or Event Bridge. 48
  49. Pattern: Step-functions 49
  50. Pattern: Configuration discovery 50
  51. Pattern: Configuration discovery C1: Baked into a function C2: Configuration manager (AWS SSM Parameter store/Secret Manager, Secrethub.io, ConfigMap) C3: Bucket (AWS S3, Google Cloud Storage) C4: DNS • • • • 51
  52. Pattern: Optimizing cold-start Optimizing cold-start times. 52
  53. Anti-pattern: Over-optimizing cold-start Over-optimizing cold-start times. 53
  54. Complex patterns/use cases 54
  55. Pattern: circuit breaker 55
  56. Pattern: backend API 56
  57. Pattern: ETL 57
  58. Use case: video processing 58
  59. Testing 59
  60. Pattern: simulated function environment AWS SAM Local Google Functions Framework • • 60
  61. Pattern: mocked cloud services Use code organization techniques to abstract/stub/mock your dependencies inside function logic. 61
  62. Pattern: log stream Standard output is your friend, take care of it. 62
  63. Pattern: tracing id FaaS environments contain extra context (like request id) that can be passed to logs or next invokations. 63
  64. AWS Lambda context 64
  65. Pattern: callback chat channel 65
  66. Pattern: callback chat channel Important business logic events can be sent to chat (debugging) channel. 66
  67. Example: callback channel 67
  68. Database 68
  69. Pattern: database-as-a-service D0: Custom server(s) with database software D1: Custom container(s) with database and persistent (Kubernetes, Helm, Kubedb) D2: Database server as a service (AWS RDS, MongoDB Atlas) D3: Scalable database as a service (AWS DynamoDB, Azure Cosmos, Google Firebase) • • • • 69
  70. Pattern: database-in-a-function Dataset is small enough (upto 100MBs) It is mostly read-only It changes infrequently (re-deploy) It does not affect cold start too much (upto 30 seconds) • • • • 70
  71. dev.tube 71
  72. Peak time 72
  73. Architecture Node.js/Typescript/Express/Vue.js GCP/Firebase GitHub • • • 73
  74. Architecture 74
  75. Price optimization Data is inside a function! HA database attached to public internet traffic is expensive! Load balancer is expensive! f1.micro with nginx was cheaper. Indexer runs once a day with some heuristics to not over-use YouTube API quota. • • • 75
  76. Security 76
  77. Security issues What if my hardware is vulnerable? (Meltdown and Spectre) What if my hypervisor is vulernable? What if my operating system is vulernable? (Heartbleed) What if my container engine is vulnerable? (CVE-2019-5736) What if my application is vulnerable? • • • • • 77
  78. Risk: injection attacks Replay attack? SQL injection? XSS? Input must be validated. 78
  79. Pattern: request signing Function expects that all incoming requests are signed (e.g. HMAC) to check that message content didn't change in transit. 79
  80. Risk: denial-of-payment Someone called your function so many times, you have to sell your house! 80
  81. Pattern: rate limiting API Gateway Limited concurrency • • 81
  82. Anti-pattern: recursive function call 82
  83. Risk: public function No authentication? Seriously? 83
  84. Pattern: security token Function expects known security token to be passed as a proof of trusted invoker. 84
  85. Pattern: platform authentication Function can only be invoked by authenticated user (AWS Cognito, Google IAM user). 85
  86. Conclusion It is servicefull! It is code! It is a mix of Dev and Ops! • • • 86
  87. Thank you! 87
  88. 10-12 March 2021 88
  89. 89
  90. Links I https://serverless.com/ https://cdkpatterns.com/ https://aws.amazon.com/blogs/architecture/updates-to-serverless- architectural-patterns-and-best-practices/ • • • 90
  91. Links II https://www.jeremydaly.com/serverless-microservice-patterns-for-aws/ https://www.youtube.com/watch?v=tHD3i06Z6gU https://www.youtube.com/watch?v=vuWiB3vNiHc https://www.simform.com/serverless-examples-aws-lambda-use- cases/ • • • • 91
Advertisement