当前位置:网站首页>The practice of event driven architecture in vivo content platform
The practice of event driven architecture in vivo content platform
2022-06-28 07:05:00 【Vivo Internet technology】
One 、 What is event driven architecture
The present , With the rise of microservices , The development of containerization Technology , And cloud native 、serverless The popularity of the concept , Event driven has again aroused widespread concern in the industry .
The so-called event driven architecture , That is, using events to implement business logic across multiple services . Event driven architecture is a software architecture and model for designing applications , To minimize coupling , Well extend and adapt different types of service components . In this framework , When something important happens , Such as updating business data , A service will publish Events , Other services subscribe to these events ; When a service receives an event, it can execute its own business process , Update business data , At the same time, a new event is released to trigger the next step .
Publish and subscribe to events , Need to rely on a reliable message broker . See the picture below :

Of course , In fact, many software projects use message queuing , But what needs to be clear here is , The use of message queuing does not mean that your project must be an event driven architecture , Many projects are just driven by technology , Some message queuing products are adopted on a small scale . So big a system , If your message queue is only used as a notification sent by mail , In this way, the system naturally does not adopt the event driven architecture .
When using event driven architecture , We need to consider business modeling 、 The design of the event 、 The boundaries of context and more technical factors , How should this system engineering be implemented from beginning to end , It needs thinking and deliberation . To make a long story short ,“ Event driven architecture ” It is not easy to design . There is an example for reference later in this article .
in addition , If you blindly use event driven design architecture , It is possible to bear the risk of interrupting the business logic , Because these business logics have a high degree of conceptual cohesion , But they are connected by the understanding coupling mechanism . let me put it another way , Is to forcibly separate the code that needs to be organized together , And it is difficult to locate the processing flow , There are also issues such as data consistency assurance . To prevent our code from becoming a bunch of complex logic , We should use an event driven architecture in certain explicit scenarios . In terms of experience , The following three Three scenarios can be developed using event driven :
Decoupling of components
Perform asynchronous tasks
Track changes in state
Two 、 When to use event driven architecture
2.1 Decoupling of components
When serving ( Or component ) A Services need to be performed B Business logic in , Compared to calling directly , We can report to the event broker ( Event distributor ) Send an event in . service B By listening for special event types in the dispatcher , Then when such an event is received, execute it .
This means service A And the service B Both depend on event proxies and events , Instead of focusing on each other's realization : That is to complete their decoupling . See the picture below :

Based on this loose coupling , Services can be implemented in different languages . The decoupled services can be easily extended independently on the network , Modify their systems by dynamically adding or removing event producers and consumers , Without changing any logic in any service .
2.2 Perform asynchronous tasks
Sometimes we have a set of business logic to execute , But because they take a long time to execute , So we don't want to see users spend time waiting for these logical processes to complete . under these circumstances , It is best to run them as asynchronous tasks , And immediately return a message to the user , Notify them to continue processing related operations later .
such as , The content field check and other warehousing processes can adopt “ Sync ” Perform processing , However, the implementation content is understood by ” asynchronous “ Task to deal with . under these circumstances , All we have to do is trigger an event , Add events to the task queue , Until a service can acquire and execute this task . here , It doesn't matter if the relevant business logic is in the same context , Anyway? , The business logic is executed .
2.3 Track changes in state
In the traditional way of data storage , We store data through entity models . When the data in these entity models changes , We just need to update the row records in the database to represent the new values . Here's the problem , That is, in business, we cannot accurately store the change and modification time of data . But in an event driven architecture , You can save the modified content into the event through event traceability . It will be discussed in detail below “ Event source “.
3、 ... and 、 Why use an event driven architecture
When people talk about event driven architecture , For example, people say that they have just adopted the event driven architecture in their recent projects , actually , They may be talking about one or more of these four patterns :
Event notification
Event hosted state transitions
Event source
CQRS
notes : Concept source 2017 year GOTO Conference On Martin Fowler To share the The many meanings of Event-Driven architecture.
3.1 Event notification
Suppose we now want to design a simple content platform , It consists of three parts :
Content introduction system
Author microservice
Focus on the center
After the content creator uploads the video through the content introduction system , The following call process will be triggered, as shown in the following figure :

The content introduction system receives the video uploaded by the creator , Execute the warehousing process ;
The content introduction system calls the author's Micro service API, increase “ video - Creator ” The subordination of ;
The author service invokes the API, Let the attention center send a notification of the author's video update to other users who have followed the creator .
The above call process , Inevitably, the following dependencies are created :
The content introduction system depends on the API, Although the content introduction system doesn't really care about the author's Micro service business .
Author microservices rely on the focus of API, Although the author doesn't care about the business and process of the center .
This dependency may not be what we expect . Content introduction system is a general business , Different types of content introduction systems are likely to have similar functions , Such as field type check 、 Into the content library 、 Start high sensitivity audit, etc . Author service is a very professional system , If not homologous 、 Different types of content have different business logic about the author . Let a general system depend on a professional system , From a design point of view , From the perspective of subsequent system maintenance , Are not a good plan . The author's microservice may often change according to the business needs , But the content introduction system is relatively stable , The above dependency makes it difficult for us to “ The content introduction system is not adjusted ” Change the author's Micro service at will .
From the architecture level , We want to make the author microservice depend on the content introduction system , Let a professional system rely on a stable 、 Universal system , Increase the stability of the system . At this time, we can use “ Event notification ”. See the picture below :

advantage
The architecture is more robust . If the queued events can be executed in the source component , But in other components, due to bug Make it impossible to execute ( Because it is added to the queue task , They can be in bug Repair and then execute ).
Business processing reduces latency . When the user doesn't have to wait for all the logic to complete , This kind of work can be added to the event queue .
Easy system expansion , Enable the R & D team of components to develop independently , Speed up the project 、 Reduce functional difficulty 、 Reduce problems and be more organized .
Encapsulate information in “ event ” in , Easy to spread in the system .
shortcoming
If not used properly , May make our code “ Spaghetti ” Code .
Data consistency issues . Because processes depend on final consistency , Therefore, it is generally not supported ACID Business , Therefore, the processing of repeated or out of sequence events will make the service code more complex , And it's hard to test and debug everything .
“ Event notification ” The disadvantages and advantages of , It is precisely because it provides a good decoupling capability , It will be difficult for us to get a complete picture of the whole system and process by reading the code . Because the relationship between these logics is no longer the previous dependency . This will be a challenge .
3.2 Event hosted state transitions
When we use event notification , Events often do not contain all the information that the downstream system needs to process this event . For example, when the content is changed off the shelf , The content platform will generate a “ Content off the shelf “ Events , But when the downstream system handles this event , Often you need to know , What was the last status of the content , Who triggered off the shelf and other information , To complete subsequent processing . So inevitably , When the downstream system handles this event , It is often necessary to obtain these additional information through platform services .
To solve this problem , We introduce a new model , be called “ Event hosted state transitions ”. Simply speaking , It is to let the event consumer keep a copy of the upstream system data that needs to be used in business processing . For example, let the downstream system keep a copy of the status before the content change that is needed to process the content status change event , Avoid going back to the platform to query .
advantage
The architecture is more robust . Reduce the additional dependence of event consumers on producers ( Get the data required for event processing );
Business processing reduces latency . Increase the response speed of the event consumer system , Because you no longer need to call the platform API To get the data required for event processing ;
There is no need to worry about the load of the queried component ( Especially remote components ).
shortcoming
Although data storage is no longer the root cause of the problem , Multiple read-only copies of the data will still be saved , Consistency is further broken ;
Increase the complexity of data processing , Even if the processing logic conforms to the specification , It also requires additional processing and maintenance of local copies of external data business logic .
3.3 Event source
Sometimes we are not only concerned about the current state of the system , We are also concerned about how to become the current state , But the database simply saves the current state of the entity . Event tracing can help us solve this problem .
Event tracing is a special idea , It does not persist entity objects , Instead, only the initial state and the event of each change are recorded , And restore the latest state of the entity object in memory according to the event ,mysql For master-slave backup binary log as well as redis Of aof Persistence mechanism , Can be considered as “ Event source ” The implementation of the .
The event traceability is performed after the database is updated , It converts the event sending operation into writing an event record to the database or log system , Other nodes query the database or file system , To get these events , And ensure the final consistency of data through playback .
advantage
Can present a complete history of changes ;
Provide more convenient debug methods ;
It can be traced back to any historical state ;
It is convenient to modify the current event ;
shortcoming
- To implement a reliable and high-performance event Repository ( Saved event records ) It's not an easy thing , The application code needs to be based on the API Rewrite .
3.4 CQRS
CQRS The full name is Command Query Responsibility Segregation. Simply speaking , It is the read / write operation for the system , Use different data models 、API Interface 、 Security mechanisms, etc , To achieve complete isolation of read and write operations , Meet different business needs . See the picture below :

According to the event collection stored in the event library , The status of each business entity can be calculated , These states are stored in a database as materialized views . When a new event occurs , It also automatically updates the view . such , The view query service can realize various query scenarios just like querying ordinary database data . The specific design can refer to the following figure :

Four 、 The practice of event driven architecture in content platform
In modern society , Content “ transverse ” Era , Content platform enterprises need to have strong flexibility and adaptability . Especially in a content industry like China ( Video frequency ) In the rapidly developing market , Enterprises require that the platform can quickly respond to content business needs , Otherwise, the first mover advantage will be lost . This is somewhat similar to modern war conditions , All countries require their troops to have a rapid response capability , This capability is mainly reflected in that the platform can be rapidly developed or reused / Integrate existing resources to quickly respond to business needs .
As the business of the content industry becomes larger and more complex , The type of storage involved 、 processor 、 Account system 、 Efficiency tools 、 There are many data and settlement systems , This requires the platform to have a strong ability to integrate and adapt to heterogeneous environments .
Last , Due to the rapid development of the content industry , Specific types of content business ( Such as small video ) The city will usher in a rapid expansion period after its junior high school development , The volume and type of business will increase dramatically , This also requires the platform to have good scalability . See the following figure for the relevant platform architecture :

4.1 Create an event
The event is actually DDD( Domain-driven design ) A concept in , Represents a business value event that occurs in an area , Falling to the technical level is any change that affects the business process or state . Events have their own properties , Such as the time of occurrence 、 What happened? 、 The relationship between events 、 State and change , Events can also generate new events , Generate new business events according to different events . When creating an event , First, you need to record some general information about the event , Like a unique logo ID And creation time , Create an event base class for this ContentEvent:
public abstract class AbstractContentEvent {
private String eventId;
private String publisher;
private String receiver;
private Long publishTime;
}public abstract class AbstractContentEvent {
private String eventId; private String publisher; private String receiver; private Long publishTime; }
In a normal situation , Events generally follow the aggregation root ( It's also DDD A concept of , This generally refers to video id) State update , in addition , On the consumer side of the event , Sometimes we want to listen for all the events that happen under a certain aggregation root , For this reason, it is recommended to create a corresponding event base class for each aggregation root object , It contains the aggregation root videoId, For example, for video (Video) class , establish VideoEvent:
public class VideoEvent extends AbstractContentEvent {
private final String videoId;
}
Then for actual video events , Unity is inherited from VideoEvent, For example, for the introduction of video VideoInputEvent event ;
public class VideoInputEvent extends VideoEvent {
private Article article; // Basic video information
}
See the following figure for the inheritance chain of video domain events ;

When creating an event , Two things to note :
The event itself should be immutable ;
The event should carry the context data information related to the occurrence of the event , But it's not the state data of the entire aggregate root . for example , The basic information of the video can be carried when the video is introduced article, For video status update VideoStatusChangeEvent event , The status before and after the update should be included status:
public class VideoStatusChangeEvent extends VideoEvent {
private String preStatus; // Status before update
private String status; // Updated status
}
4.2 Release events
There are many ways to publish events , For example, it can be published in the application . The usual business process updates the database and then publishes events , A common scenario here is : You need to ensure atomicity between database updates and event publishing , That is to say, both of them succeed , Or they all failed ; Of course, there are also scenarios where atomicity does not need to be guaranteed . If atomicity is required , With “ Content introduction ” Business process of , See the picture below :

Receiving content ;
Write content table ;
Write event table , And in the same local database transaction as the update of the content table ;
After the transaction completes , Trigger event sending ;
Read the event table ;
Send the event to the message queue ;
After sending successfully , Mark the record as “ Has been sent ”;
4.3 Consumer events
In the event of consumption , In addition to completing the basic message processing logic , We need to focus on the following three points :
Idempotence of consumer ;
The consumer is likely to have further events ;
Consumer data consistency ;
about “ Idempotency ”, The event sending mechanism guarantees “ Deliver... At least once ”, This is guaranteed by message oriented middleware , Attention should be paid to when selecting technology . In order to be able to handle duplicate messages correctly , The consumer is required to be idempotent , That is to say, the effect of multiple consumption events is the same as that of single consumption . Guarantee “ Consumption idempotence ” There are many ways , Here is a kind of . Create an event table on the consumer side , Used to record events that have been consumed , When dealing with Events , First check if the event has been consumed , If so, do not do any consumption processing .
For the second point , I still use what I said before “ Event table ” The way . in fact , Whether it's processing service requests , Or as the consumer of the message , For aggregate roots (videoId) They are all unconscious , Events are generated by the aggregation root and then persisted by the event library , These processes have nothing to do with the specific source of business operations .
about “ Data consistency ”, In essence, it is derived from the second point , Event driven architecture synchronizes states between business objects through asynchronous messages , Some messages can also be published to multiple services at the same time , stay “ Messages cause a service to synchronize ” It may cause other messages , Events will spread . Strictly speaking, event driven is not called synchronously , How to ensure consistency , It is more complex than non event driven architecture , Usually used “cache aside” Patterns and “ Distributed lock ” To ensure consistency .
Sum up , In the course of consumption Events , The application needs to update the business table 、 Event log sheet , See the following figure for the release and consumption process of the whole event ;

5、 ... and 、 summary
In mainstream scenarios , Traditional service orientation ( Or data driven ) The platform is not systematic enough , The following capabilities need to be enhanced :
On the basis of traditional data integration, the business integration capability needs to be further improved .
It is necessary to improve the business agility and responsiveness of the integration platform .
It is necessary to further realize decoupling and high reliability between business systems .
The real-time response capability of the control platform needs to be further improved .
” Event driven architecture “ It naturally meets these capability requirements . Event driven architecture ” born “ The advantages of , such as , encapsulation 、 High cohesion and low coupling , It can also improve the maintainability of the code 、 Demand for performance and business growth , Through the event traceability mode , It can also improve the reliability of system data .
however , Event driven also has drawbacks , Because both the conceptual complexity and the technical complexity have increased , When it is abused, it will lead to disastrous consequences . therefore , In the selection of technology stack , Give the following message :
1) Don't “ Blindly pursuing the new ” The preference of technical personnel is often to pursue what technology is popular . Now the technology is developing rapidly , Various frameworks are emerging at the front and back end , We can't wait to use these frameworks in our own projects , Proceed from reality , Use... As needed , Appropriate space shall be reserved for technical pre research .
2) Don't “ Stand in line by technology , Extrapolate from the result “ Many people regard means as an end , Became a believer in the framework . It was used Java Development , Does your design have to be object-oriented ? It was used Spring boot Is it a micro service ? Be sure to combine the technology with the actual scene , Architects should also have a deep understanding of the technology , But it is more about understanding the advantages and disadvantages of the technology and the use scenarios , Instead of simply copying mechanically .
author :vivo Internet server team -Gao Xiang
边栏推荐
- Batch import of pictures into WPS table by date
- Build your jmeter+jenkins+ant
- CMAKE小知识
- 【Rust日报】2020-05-24 Rash, Rocket, Mun, Casbin
- An important term in MySQL -- CRUD
- Niubi 666, this project makes web page making as simple as building blocks
- 【星海出品】 运维巡检合集
- 编译配置in文件
- eyebeam高级设置
- Using interceptor and cache to complete interface anti brushing operation
猜你喜欢

eyebeam高级设置

Compile configuration in file

VM332 WAService. js:2 Error: _ vm. Changetabs is not a function

微信小程序编译页面空白bug的原因

MySQL installation steps - installing MySQL on Linux (3)

Techo Day 腾讯技术开放日,6月28日线上等你!

js正则表达式系统讲解(全面的总结)

2021 VDC: technological architecture evolution of vivo Internet service for 100 million users | PPT download attached

什么是一致性哈希?可以应用在哪些场景?

redis的入门学习到起飞,就这一篇搞定
随机推荐
文件头信息对照表
KMP string
Wechat applet paging function, pull-down refresh function, direct dry goods
MySQL installation steps - Linux configuration file JDK installation (II)
Students who do not understand the code can also send their own token. The current universal dividend model can be divided into BSC and any generation B
Principle and practice of bytecode reference detection
4~20ma input /0~5v output i/v conversion circuit
What if the applet page is set to 100% height or left blank?
How bacnet/ip gateway collects data of building centralized control system
【星海出品】 运维巡检合集
SQL statement optimization steps (1)
Puge -- understanding of getordefault() method
LLVM 与 Clang
Force buckle 515 Find the maximum value in each tree row
Vivo browser rapid development platform practice - Overview
Call interface event API common event methods
Detailed explanation of collection class methods____ (4) Judgment and assignment, etc
R 语言 ggmap 可视化集群
DOM parsing of XML file case code sentence by sentence analysis
Devtools implementation principle and performance analysis practice