一起架构-某实时分析项目云原生 serverless 架构的设计思路和poc代码实现

本文涉及的产品
简介: 一起架构-某实时分析项目云原生 serverless 架构的设计思路和poc代码实现

1. 前言 - 云原生与多云混合云的部署架构

大家好,我是明哥!

在数字化转型的大背景下,越来越多的企业不断将越来越多的应用部署到云上,应用的架构也更加倾向云原生,以支持多云和混合云的部署架构。

前段时间,笔者参与了某个实时分析项目在 AWS 上的架构设计和 POC 开发,该项目使用了 serverless 的云原生架构,在此跟大家分享下架构设计和 poc 代码的细节,希望大家喜欢。

2. 项目背景和目标 Background and goals

整个项目的背景和目标如下:

640.png


经提炼和概括,项目的背景,基本目标和额外目标如下:

  • 背景:Ingest, transform and prepare the netCDF data provided by UK Met Office, make them available for secure querying by our customer, as soon as it arrives in the S3 bucket.
  • 基本目标:Core capabilities include:
  • high availability (no downtime)
  • quick response
  • timely availability of new data.
  • 额外目标:Extra Goals:
  • Security
  • cost effectiveness

3. 整体架构图 Architecture overview

最终设计的完整的架构图如下:

640.png


4. 架构设计和技术选型 Architecture details and thought process

4.1 How to discover new available data ASAP? - SQS

  • UK Met Office prepares the original data in netCDF format and uploaded them to a S3 bucket, but as listing a bucket is both expensive and slow (file system vs object store), we can’t take this approach for quickly discover of new available data in s3;
  • We noticed that UK Met Office will also send a message to a SNS topic once new data is available in the S3 bucket, so we can use a SQS to scribe to the SNS topic, and got notified when new objects are created, this solution is latency-efficient, cost-effective and scalable;

4.2 Can we use the original S3 bucket used by the UK Met Office?

  • We noticed that the original data will be held in the bucket for 7 days after the notification is sent, then they will be deleted;
  • We can use our own S3 bucket to store the data, so we have full control of the data, including the data lifecycle, the data security policies, etc;

4.3 How to server our end users, with quick response and high availability? – API gateway + DynamoDB

  • Our end users typically ask questions like “how will the weather/humidity/temperature be like in city C1, at time T1? how about city C2 and C3? How about time T2?”, to answer that question, we have to first figure out which files in the S3 bucket contains forecast results for that specific time (all the files contains forecast results for all the cities in UK, so place should not be a problem);
  • So we can use a RDS or DynamoDB to store the metadata “which s3 file contains forecast results for which time”, then when we receive a specific question from our customer, we can first query the RDS/DynamoDB to find out the corresponding S3 file, then they can query the s3 file to get all the forecast details, including weather/humidity/temperature etc, for all the UK cities;
  • RDS is a relational database and is typically for well-formed structured data, while DynamoDB is a fully-managed Key-value NoSql data store, both can fulfill our functional requirements, but considering that we don’t have highly-structured data, and DynamoDB shines in Availability. Scalability and Performance, so we will go with DynamoDB;
  • We can use an API gateway as a proxy to the DynamoDB and answer the end user’s request directly, with out an extra lambda layer between API gateway and DynamoDB, hence the whole data pipeline is shorter, which will be more time-effective, cost-effective, and less issues will occur; Also API gateway provides many security mechanisms, including authentication, authorization,audit and encryption;
  • With api gateway, DynamoDB and S3, the whole serving layer will response quickly with high availability, and is also cost-effective and secured;

4.4 How to ingest, transform and prepare the original data - Lambda!

  • To consume messages in SQS queues, we normally follow the event-driven architecture and use streaming processing frameworks like spark streaming/flink/kafka stream, but to use them, you need to first provision ec2 servers and possibly use ecs/eks, but you need to deploy, monitor and scale(both up and down) your app all by yourself, this is cumbersome and not cost-effective;
  • You can consider using serverless Fargate, but you have to deal with the event-driven by yourself;
  • Lambda is both serverless and event-driven, it automatically scales according to your data volume, it integrates with other aws services like sqs, s3, DynamoDB, api gateway well, and it allow you to pay for what you use, so it is a perfect match for our case!
  • we can use lambda and create a sqs trigger, so right after events arrived in sqs, it will trigger the execution of lamba where we can do the transform and load into downstream DynamoDB table;

5 技术组件细节和示例代码 Component details and code samples

5.1 Component details and code samples – sqs and lambda

  • Sqs type:as there is no need for First-in-first-out message delivery and Exactly-once processing, we can stay with the standard type sqs,which offers better scalability;
  • Sqs encryption: Amazon SQS provides in-transit encryption by default, we also added at-rest encryption to our queue by enable server-side encryption (uses Amazon SQS key (SSE-SQS));
  • Lambda: lambda has an sqs trigger, and for performance consideration, we are using batch to writer into dynamodb;
  • Labmda permission: to follow the least-access polity, we created a new IAM role with basic Lambda permissions (with just polices like AWSLambdaSQSQueueExecutionRole/AWSLambdaExecute/AWSLambdaDynamoDBExecutionRole)

5.2 Component details and code samples - dynamoDB

  • dynamoDB is serverless and will auto scale based on data volume and query, so to avoid hot spot bottleneck, we used forecast_period as partition key/hash key and forecast_time as sort key;(forecast_period is the difference between forecast_reference_time and forecast_time);
  • As end users typically query based on time, so we created a secondary global indexes sgi, with partition key on the time field forecast_time;
  • Encryption: we turned on Encryption at rest, and used encryption keys stored in AWS Key Management Service, whch is managed by DynamoDB at no extra cost;
  • permission: for apis to query the dynamoDB, we followed the least-access polity and created an access control policy with only read policy on the table and index;

5.3 Component details and code samples – api gateway

  • Api: I created two methods and resources, and configured the integration request and integration response’s mapping template, to full fill the scan and query on the dynamoDB, with paths like /times and /times/{time}, the latter one will use the sgi we created for the table;
  • Api key: I configured the method request to use API Key;
  • permission: to follow the least-access polity, we created a new IAM role with only necessary permissions (with just polices like AmazonAPIGatewayPushToCloudWatchLogs, and the dynamodb read-only policy we created earlier)

5.4 Component details and code samples – lambda codes

640.png

5.5 Component details and code samples – api codes

640.png

6. 脚本与自动化 automation using script - cloudFormation

  • I believe in IaC (infrustructure As Code) and GitOps, humans will make mistakes and automation helps us on this (plus automation is more efficient and script is more repeatable);
  • So I tried to use cloudFormation template to simplify the infrastructure management (due to time constraint, I only finished the dynamodb template);
  • Below are part of the cloudFormation script for the dynamodb table creation;

640.png

7. 终端用户模拟访问效果 End user query simulation results

640.png

  • IAM user with read only permission – IAM user name: arn:aws:iam::000435319421:user/demo
  • IAM user with read only permission – IAM user password: demo123@aws
  • End user request url: https://jye2m0pw20.execute-api.us-east2.amazonaws.com/v1/times/2022-04-16T22:45:00Z
  • End user request sample path parameter: 2022-04-17T22:30:00Z/2022-04-16T22:45:00Z, etc;
  • End user request type: get
  • End user request Authorization Type: api key
  • Key: x-api-key
  • Value: kNKmXfQGNx802XU1f75Mu9vRAFBvWIdM5uT7NmHa
  • Add to: header

8. 总结 Wrap up

  • high availability (no downtime): The solution used components like sns,sqs,lambda,dynamodb,api gateway and s3, all of which are managed services which scaled well and scaled automatically, to ensure high availability (no downtime);
  • quick response: The solution used dynamoDB in the serving layer, which scales well and scales automatically, and with the careful design of hashkey,sortkey and sgi, it offers quick response time to end users;
  • timely availability of new data: The solution followed the event driven architecture, with sqs and lambda, and ensured the timely availability of new data;
  • cost effectiveness:The solution followed the server-less architecture and used aws serveless services, so we can pay only what we use, and hence is cost effective;
  • security:
  • Encryption:aws service used TLS to provide encryption between user application and the AWS service which offered data-in-motion/transit encryption, and we enabled data-at-rest encryption;
  • Authentication and Authorization:we also followed the least-access policy to create IAM roles and policyes. we also used an api key to protect our api gateway from malicious attacks
  • Audit: CloudWatch is used for the audit;
相关实践学习
基于函数计算一键部署掌上游戏机
本场景介绍如何使用阿里云计算服务命令快速搭建一个掌上游戏机。
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
相关文章
|
2天前
|
敏捷开发 Cloud Native 持续交付
构建未来:云原生架构在企业数字化转型中的关键作用
【5月更文挑战第13天】 随着企业加速其数字化进程,云原生架构已经成为推动创新和维持市场竞争力的核心要素。本文将探讨云原生技术的基本原理、它如何促进敏捷开发,以及它在实现可扩展性、弹性和资源优化方面的优势。通过分析案例研究和行业趋势,我们将深入理解云原生架构是如何成为企业转型不可或缺的技术支撑。
|
1天前
|
安全 Serverless API
Serverless架构在图像处理中展现出高成本效益,按需付费降低费用,动态调整资源避免浪费
【5月更文挑战第16天】Serverless架构在图像处理中展现出高成本效益,按需付费降低费用,动态调整资源避免浪费。其出色的并发处理能力和自动扩展确保高并发场景的顺利执行。简化开发流程,让开发者专注业务逻辑,同时提供丰富API和集成服务。安全方面,Serverless通过云服务商管理基础架构和多种安全机制保障任务安全。因此,Serverless是处理高并发、动态需求的理想选择,尤其适合图像处理领域。随着技术发展,其应用前景广阔。
12 4
|
2天前
|
Kubernetes Cloud Native 持续交付
构建未来:云原生架构在企业数字化转型中的关键作用
【5月更文挑战第15天】 随着企业加速迈向数字化时代,传统的IT架构日益显得笨重且不灵活。本文探讨了云原生架构如何成为推动企业敏捷性、可扩展性和创新能力的关键因素。通过分析微服务、容器化、持续集成/持续部署(CI/CD)和动态编排等核心技术,揭示了云原生架构如何帮助企业快速响应市场变化,优化资源利用,并最终实现业务目标。文章还将展示一个实际案例,说明企业如何成功实施云原生策略以支持其转型过程。
|
2天前
|
Cloud Native 安全 Devops
构建未来:云原生架构在企业数字化转型中的关键作用
【5月更文挑战第15天】 随着企业加速迈向数字化时代,传统的IT架构正遭遇前所未有的挑战。本文深入探讨了云原生架构如何成为推动企业数字化转型的引擎,并分析了其核心组件和优势。我们将从容器化技术、微服务、DevOps实践以及持续集成与持续部署(CI/CD)等方面,探讨云原生架构如何促进资源优化、加快产品上市时间,并提供高度可扩展及弹性的系统。
|
2天前
|
Cloud Native Devops 持续交付
构建未来:云原生架构在企业数字化转型中的关键作用
【5月更文挑战第15天】 随着企业不断追求敏捷性、可扩展性与成本效益,云原生架构已经成为推动数字化转型的核心技术。本文深入探讨了云原生技术如何通过提供灵活的资源管理、无缝的服务集成以及自动化的操作环境,助力企业构建更加动态和响应迅速的IT基础结构。文章分析了容器化、微服务、DevOps等关键概念,并通过案例研究展示这些技术如何在实际场景中发挥作用,从而为追求数字化优势的企业提供实用的策略和见解。
|
2天前
|
运维 Kubernetes Cloud Native
构建未来:云原生架构在企业数字化转型中的关键作用
【5月更文挑战第14天】 随着数字浪潮的汹涌推进,企业面临着前所未有的转型压力。传统的IT架构已无法满足市场对速度、灵活性和创新的渴望。云原生架构,作为一种新兴的设计理念和技术实践,正日益成为推动企业数字化变革的重要力量。本文将探讨云原生架构的核心组件,如何利用这些组件来构建高度可靠、可扩展的系统,并分析其在加速企业级应用部署、提高运维效率及促进业务创新方面的战略意义。
|
2天前
|
存储 设计模式 数据库
字节3-1大佬分享:字节跳动代码架构设计
字节3-1大佬分享:字节跳动代码架构设计
13 0
|
2天前
|
Cloud Native Devops 持续交付
构建未来:云原生架构在企业数字化转型中的关键作用
【5月更文挑战第14天】 随着企业加速其数字化转型的步伐,云原生架构已经成为实现敏捷性、可扩展性和创新的关键推动力。本文将探讨云原生技术如何助力企业构建灵活的IT环境,支持连续交付和部署,以及促进企业的持续创新。通过分析微服务、容器化、DevOps和持续集成/持续部署(CI/CD)等核心概念,我们将揭示这些技术如何共同作用,为企业提供竞争优势并推动业务增长。
|
2天前
|
Cloud Native Devops 持续交付
构建未来:云原生架构在企业数字化转型中的关键作用
【5月更文挑战第14天】 随着企业加速其数字化转型的步伐,云原生架构已经成为实现敏捷、可扩展和高效运营的重要基石。本文深入探讨了云原生技术的核心组件,包括容器化、微服务、持续集成/持续部署(CI/CD)以及DevOps实践,并分析了它们如何共同推动企业应用的快速迭代和可靠性提升。通过对这些技术的细致剖析,我们揭示了云原生架构如何帮助企业应对复杂多变的市场环境,同时确保了安全性和合规性的要求得到满足。
|
2天前
|
运维 Cloud Native 持续交付
构建未来:云原生架构的进化之路
【5月更文挑战第14天】 随着数字化转型的深入,企业对IT基础设施的要求越来越高,云原生技术以其灵活性、可扩展性和敏捷性成为推动创新的重要力量。本文将探讨云原生架构的关键组件,包括容器化、微服务、持续集成/持续部署(CI/CD)和自动化管理,并分析如何利用这些技术构建一个高效、可靠的云平台。通过深入解析云原生技术的发展趋势和挑战,为企业提供构建和维护云原生环境的实用指南。

热门文章

最新文章