{"id":3316,"date":"2021-07-09T13:59:00","date_gmt":"2021-07-09T11:59:00","guid":{"rendered":"https:\/\/blog.besharp.it\/?p=3316"},"modified":"2021-07-08T18:06:11","modified_gmt":"2021-07-08T16:06:11","slug":"caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows","status":"publish","type":"post","link":"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/","title":{"rendered":"Caching and real-time notifications in a fully serverless AWS based web application with long-running workflows"},"content":{"rendered":"\n

Serverless infrastructures provide huge advantages with respect to \u201cclassic\u201d server infrastructures.  One can easily understand this by looking at a basic AWS-based Serverless web application developed using AWS Lambda as the backend tier, DynamoDB on-demand (database), Cognito for Authentication, and S3-CloudFront for the frontend. An application like this will scale automatically, in real-time, to accept any amount of traffic and will cost significantly less than an EC2 hosted counterpart while being much simpler to deploy and maintain. Going serverless is thus a no-brainer in most situations: you\u2019ll pay less for an application that will be able to scale better, will have increased isolation, security, and a much lower maintenance effort.<\/p>\n\n\n\n

\"Basic
Figure 1: Basic Serverless application example<\/figcaption><\/figure><\/div>\n\n\n\n

Nowadays several AWS Lambda-centered development frameworks have been created and they make the development of serverless applications on AWS a breeze (e.g. Chalice for Python, Serverless framework for several languages). Furthermore, they come with batteries included so it is pretty straightforward to create CI\/CD pipelines using AWS native DevOps tools (CodeBuild, CodeDeploy, CodePipeline) and to integrate the serverless applications with several other AWS services such as SQS (Lambda based queue consumer architecture), SNS and Kinesis.<\/p>\n\n\n\n

However, moving to a serverless architecture involves new problems and solutions with respect to more traditional architectures. Caching of both internal and external resources (e.g. database queries, frontend assets, and third-party APIs) in particular requires different tools and techniques from the ones used in more traditional architecture, and so does the management of long-running processes. <\/p>\n\n\n\n

Caching<\/h2>\n\n\n\n

In a traditional application (e.g. Ruby on Rails or Django) cache is managed with a local or centralized redis\/memcached deployment which is often used for everything, from frontend components to DB queries and external API calls responses.<\/p>\n\n\n\n

In a serverless environment caching needs to be not only very fast to access (single-digit ms) and straightforward to use but also connectionless and infinitely scalable. These requirements make DynamoDB the most obvious, and usually the better, choice: when configuring a table as on-demand its write and read capacity scales automatically allowing the application to remain responsive even in case of a sudden steep traffic burst and the table access time from Lambda is single-digit millisecond. If an even lower access time is needed it is possible to activate Dynamo DAX Accelerator which can lower the read latency from milliseconds to microseconds!<\/p>\n\n\n\n

\"Caching
Figure 2: Caching in AWS: EC2 based app on the left, serverless on the right<\/figcaption><\/figure><\/div>\n\n\n\n

Long-Running Jobs in a serverless web Application<\/h2>\n\n\n\n

In a traditional architecture, a web application would usually manage long-running tasks by spawning threads and sending notifications via open WebSocket connections. On the other end, a Lambda-based application would launch a long-running task using AWS SQS or Kinesis as a queuing service and Fargate containers or standalone Lambdas as workers. Particularly complex or contrived tasks can also be carried out by starting serverless workflows using AWS StepFunctions.<\/p>\n\n\n\n

\"Long
Figure 3: Long-running jobs in an AWS web application: EC2 based app on the left, serverless on the right<\/figcaption><\/figure><\/div>\n\n\n\n

Finally, it is often useful to send notifications directly to the client browser in order to update the state of the frontend components in response to events such as the execution status of running tasks, notify the user of action carried out by other users (e.g. in online games), deliver messages and more and more often notify users about status changes of IoT devices. <\/p>\n\n\n\n

Real-Time Notifications<\/h2>\n\n\n\n

While in classical applications it is possible to directly use WebSockets, even through AWS ELBs which supports HTTP\/2, for serverless applications we need to leverage the AWS ApiGateway Websocket support which is also natively supported by several serverless frameworks, such as Chalice. When a Web socket connection is established by a client a lambda can be invoked by the $connect<\/em> hook and it is able to register the connection id to a database, usually DynamoDB. When a user disconnects the $disconnect<\/em> endpoint is invoked which allows our application to delete the connection from the connections table. Developing a logic to send notifications is thus pretty straightforward: when a message needs to be delivered from the backend to a user the ApiGateway @connections POST API is invoked using the id\/ids of the user\u2019s open WebSocket connections and ApiGateway takes care of forwarding the message in the open WebSocket channel.<\/p>\n\n\n\n

\"Real
Figure 4: Real-time Websocket notifications in an AWS web application: EC2 based app on the left, serverless on the right <\/figcaption><\/figure>\n\n\n\n

A real-world example<\/h2>\n\n\n\n

While these techniques are especially useful for high traffic applications, even low traffic apps can take great advantages by implementing them, especially those managing complex workflows. <\/p>\n\n\n\n

The following example architecture is a greatly simplified version of the architecture we actually deployed for an application allowing a customer to dynamically manage S3 buckets and IAM users accessing them.<\/p>\n\n\n\n

\"dynamically
Figure 5: First version of the example application infrastructure<\/figcaption><\/figure><\/div>\n\n\n\n

The application allows the customer\u2019s project managers to easily create secured S3 buckets in dedicated, project-specific, isolated, and hardened AWS Accounts. Once the buckets are created it is also possible to generate managed IAM users to access them. The user credentials can then be shared securely with third parties to allow them to download and\/or upload files from S3 using legacy on-premise systems, leveraging either the AWS S3 Apis or SFTP (AWS Transfer for SFTP). Users and Bucket can easily be added and removed from each project and User permissions can also be managed through a straightforward user interface. <\/p>\n\n\n\n

In order to simplify the backend development and at the same time make the application more resilient, we decided not to set up any database and simply use the backend to create, update and modify Cloudformation templates. In this way the consistency of the AWS infrastructures in the Accounts is strongly enforced, each action performed is automatically logged, and in case of any error during the creation of a resource, the rollbacks are automatically executed by the Cloudformation service.<\/p>\n\n\n\n

However, this approach has two main drawbacks, one for the LIST\/GET operations and one for the CREATE\/UPDATE operations. <\/p>\n\n\n\n

In fact, each time an end-user lists the existing resources the backend needs to fetch all the CloudFormation templates, parse them to list the resources, and finally return the response. This flow needs to be executed at least on one template any time a user performs a LIST or GET operation. For accounts with dozens of buckets and users, this operation can take several seconds, making the user experience very poor and potentially, in a few extreme cases, breaking the 30-sec ApiGateway response limit.<\/p>\n\n\n\n

The second problem involves update operations and manifests itself when several customers are connected: if one manager updates a bucket or user all the other users cannot modify it until after the Cloudformation execution is over, which in this use case only takes a few seconds. However other connected users have no way to know that a user is being updated and may try to modify it resulting in unexpected errors which, while harmless, make the user experience clumsy.<\/p>\n\n\n\n

Beyond these problems there is also a further concern: since the described infrastructure is \u201cGET heavy\u201d towards the Cloudformation Apis if a big enough number of managers log in, the Cloudformation API rate limit could potentially be breached resulting in further slowdowns (AWS SDKs implement exponential backoff).<\/p>\n\n\n\n

To resolve all these problems, after the initial MVP, we applied the design pattern described above to make the application production-ready: we used DynamoDB to cache the CloudFormation state and we used the builtin Cloudformation SNS integration to update the state in real-time through ApiGateway managed web sockets connections and at the same time update the dynamo cache with the latest Cloudformation state, so that the state in dynamo always mirror the Cloudformation state.<\/p>\n\n\n\n

\"Ready
Figure 6: Final version of the example application infrastructure<\/figcaption><\/figure><\/div>\n\n\n\n

This resulted in huge speed-ups, which completely changed the user experience for the better, with response times for GET\/LIST requests dropping consistently below 200ms. Furthermore, CloudFormation updates also became significantly faster and the resource state is consistent on all the connected clients.<\/p>\n\n\n\n

The customer was more than satisfied with the end result and a happy customer is always the best final confirmation of a well-done engineering job!<\/p>\n\n\n\n

If you have any questions about DynamoDB design patterns or any other topic concerning (or not concerning) this article do not hesitate to contact us!<\/strong> We would love to discuss it and help you \ud83d\ude42<\/p>\n\n\n\n

See you again in 14 days!<\/p>\n","protected":false},"excerpt":{"rendered":"

Serverless infrastructures provide huge advantages with respect to \u201cclassic\u201d server infrastructures.  One can easily understand this by looking at a […]<\/p>\n","protected":false},"author":9,"featured_media":3346,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[482],"tags":[264,521,523,268],"yoast_head":"\nCaching and real-time notifications in a fully serverless AWS based web application with long-running workflows - Proud2beCloud Blog<\/title>\n<meta name=\"description\" content=\"In this article, we'll see how to handle the caching and the management of long-running processes in fully serverless AWS-based web apps.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Caching and real-time notifications in a Serverless AWS based web app\" \/>\n<meta property=\"og:description\" content=\"In this article, we'll see how to handle the caching and the management of long-running processes in fully serverless AWS-based web apps.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/\" \/>\n<meta property=\"og:site_name\" content=\"Proud2beCloud Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-09T11:59:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-08T16:06:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.besharp.it\/wp-content\/uploads\/2021\/07\/CopertineBlog-07-21-social-eng.png\" \/>\n\t<meta property=\"og:image:width\" content=\"5001\" \/>\n\t<meta property=\"og:image:height\" content=\"2618\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Matteo Moroni\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Caching and real-time notifications in a Serverless AWS based web app\" \/>\n<meta name=\"twitter:description\" content=\"In this article, we'll see how to handle the caching and the management of long-running processes in fully serverless AWS-based web apps.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/blog.besharp.it\/wp-content\/uploads\/2021\/07\/CopertineBlog-07-21-social-eng.png\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Matteo Moroni\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/\",\"url\":\"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/\",\"name\":\"Caching and real-time notifications in a fully serverless AWS based web application with long-running workflows - Proud2beCloud Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.besharp.it\/#website\"},\"datePublished\":\"2021-07-09T11:59:00+00:00\",\"dateModified\":\"2021-07-08T16:06:11+00:00\",\"author\":{\"@id\":\"https:\/\/blog.besharp.it\/#\/schema\/person\/0b3e69eb2dcb125d58476b906ec1c7bc\"},\"description\":\"In this article, we'll see how to handle the caching and the management of long-running processes in fully serverless AWS-based web apps.\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.besharp.it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Caching and real-time notifications in a fully serverless AWS based web application with long-running workflows\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.besharp.it\/#website\",\"url\":\"https:\/\/blog.besharp.it\/\",\"name\":\"Proud2beCloud Blog\",\"description\":\"il blog di beSharp\",\"alternateName\":\"Proud2beCloud Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.besharp.it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.besharp.it\/#\/schema\/person\/0b3e69eb2dcb125d58476b906ec1c7bc\",\"name\":\"Matteo Moroni\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.besharp.it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/acad790b9bb4c6d62e076ecdc1debb35?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/acad790b9bb4c6d62e076ecdc1debb35?s=96&d=mm&r=g\",\"caption\":\"Matteo Moroni\"},\"description\":\"DevOps e Solution Architect di beSharp, mi occupo di sviluppare soluzioni Saas, Data Analysis, HPC e di progettare architetture non convenzionali a complessit\u00e0 divergente. Appassionato di informatica e fisica, da sempre lavoro nella prima e ho un PhD nella seconda. Parlare di tutto ci\u00f2 che \u00e8 tecnico e nerd mi rende felice!\",\"url\":\"https:\/\/blog.besharp.it\/author\/matteo-moroni\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Caching and real-time notifications in a fully serverless AWS based web application with long-running workflows - Proud2beCloud Blog","description":"In this article, we'll see how to handle the caching and the management of long-running processes in fully serverless AWS-based web apps.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/","og_locale":"en_US","og_type":"article","og_title":"Caching and real-time notifications in a Serverless AWS based web app","og_description":"In this article, we'll see how to handle the caching and the management of long-running processes in fully serverless AWS-based web apps.","og_url":"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/","og_site_name":"Proud2beCloud Blog","article_published_time":"2021-07-09T11:59:00+00:00","article_modified_time":"2021-07-08T16:06:11+00:00","og_image":[{"width":5001,"height":2618,"url":"https:\/\/blog.besharp.it\/wp-content\/uploads\/2021\/07\/CopertineBlog-07-21-social-eng.png","type":"image\/png"}],"author":"Matteo Moroni","twitter_card":"summary_large_image","twitter_title":"Caching and real-time notifications in a Serverless AWS based web app","twitter_description":"In this article, we'll see how to handle the caching and the management of long-running processes in fully serverless AWS-based web apps.","twitter_image":"https:\/\/blog.besharp.it\/wp-content\/uploads\/2021\/07\/CopertineBlog-07-21-social-eng.png","twitter_misc":{"Written by":"Matteo Moroni","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/","url":"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/","name":"Caching and real-time notifications in a fully serverless AWS based web application with long-running workflows - Proud2beCloud Blog","isPartOf":{"@id":"https:\/\/blog.besharp.it\/#website"},"datePublished":"2021-07-09T11:59:00+00:00","dateModified":"2021-07-08T16:06:11+00:00","author":{"@id":"https:\/\/blog.besharp.it\/#\/schema\/person\/0b3e69eb2dcb125d58476b906ec1c7bc"},"description":"In this article, we'll see how to handle the caching and the management of long-running processes in fully serverless AWS-based web apps.","breadcrumb":{"@id":"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.besharp.it\/caching-and-real-time-notifications-in-a-fully-serverless-aws-based-web-application-with-long-running-workflows\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.besharp.it\/"},{"@type":"ListItem","position":2,"name":"Caching and real-time notifications in a fully serverless AWS based web application with long-running workflows"}]},{"@type":"WebSite","@id":"https:\/\/blog.besharp.it\/#website","url":"https:\/\/blog.besharp.it\/","name":"Proud2beCloud Blog","description":"il blog di beSharp","alternateName":"Proud2beCloud Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.besharp.it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.besharp.it\/#\/schema\/person\/0b3e69eb2dcb125d58476b906ec1c7bc","name":"Matteo Moroni","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.besharp.it\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/acad790b9bb4c6d62e076ecdc1debb35?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/acad790b9bb4c6d62e076ecdc1debb35?s=96&d=mm&r=g","caption":"Matteo Moroni"},"description":"DevOps e Solution Architect di beSharp, mi occupo di sviluppare soluzioni Saas, Data Analysis, HPC e di progettare architetture non convenzionali a complessit\u00e0 divergente. Appassionato di informatica e fisica, da sempre lavoro nella prima e ho un PhD nella seconda. Parlare di tutto ci\u00f2 che \u00e8 tecnico e nerd mi rende felice!","url":"https:\/\/blog.besharp.it\/author\/matteo-moroni\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/posts\/3316"}],"collection":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/comments?post=3316"}],"version-history":[{"count":0,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/posts\/3316\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/media\/3346"}],"wp:attachment":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/media?parent=3316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/categories?post=3316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/tags?post=3316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}