LUC #09: Exploring the Power of Event-Driven Architecture
How event-driven architecture can supercharge your application
Good morning, fellow programming enthusiasts!
We’re ready to dive into another edition of Level Up Coding’s newsletter.
In today’s issue:
Read time: 6 minutes
Understanding Event-Driven Systems
Quite a lot of the tasks modern-day systems are required to do are in response to changes in state.
Adding an item to a shopping cart, liking a post, and paying a bill are all state changes that trigger a set of tasks in their respective systems.
This requirement has paved the way for a popular approach to system architecture known as event-driven architecture (EDA).
There are four main components in an event-driven architecture — events, producers, consumers, and channels.
Events are updates to a system’s state which are generated by producers. When an event occurs, it is sent to consumers via channels which would then trigger actions or processes.
Events can be handled synchronously or asynchronously.
When using a synchronous approach, the producer would need to wait for the consumer to process the event before moving on to the next task. For example, when booking a hotel online your web browser would wait for the system to complete your request and return a result before it can proceed.
Synchronous processing provides a predictable execution path for every given event, which makes it far simpler to understand and debug. It is necessary in scenarios where an immediate response is critical, such as processing financial transactions. However, its rigid nature makes it incompatible with components that generate high amounts of events or carry out time-consuming processing operations. Synchronous processing limits the system's scalability and fault tolerance which is why it should only be reserved for when it is absolutely necessary.
On the other hand, asynchronous processing doesn't wait for a response. Producers queue up events for consumers to process and then move on to the next task. For example, posting a status update on social media does not need immediate action. Instead, your followers can be notified asynchronously.
Asynchronous processing is more commonly used in event-driven architecture because it promotes scalability, loose coupling, resilience, and flexibility better than a synchronous approach. It is ideal in scenarios where events are generated frequently and in large portions, which is generally the case for most systems built with an event-driven design. Race conditions are the most notable pitfall to asynchronous processing, this is often best solved with well-designed queues.
Event-driven architecture is a scalable and efficient solution for systems that consist of many nodes or services and process large volumes of events. It promotes loose coupling and helps keep the system flexible and simpler to maintain.
Protocols are the backbone of digital communication. Without them, the Internet would cease to function. Below are the 8 main protocols powering the internet:
HTTP (Hypertext Transfer Protocol) — Used by web browsers and servers to communicate and exchange.
HTTPS (Hypertext Transfer Protocol Secure) — An extension of HTTP that offers secure and encrypted communication.
FTP (File Transfer Protocol) — Used to transfer files between a client and server.
TCP Transmission Control Protocol — Delivers a stream of ordered bytes from one computer to another.
IP (Internet Protocol) — Addresses and routes packets of data sent between networked devices.
UDP (User Datagram Protocol) — A simple and connectionless protocol that does not divide messages into packets and send them in order.
SMTP (Simple Mail Transfer Protocol) — Used to transmit emails across IP networks.
SSH (Secure Shell) — A cryptographic network protocol for secure data communication, remote command-line login, and remote command execution between two networked computers.
Python Built-in Compound Data Structures
Compound data structures hold a collection of data.
There are three core built-in compound data structures in Python — Lists, Sets, and Dictionaries. All three are mutable data structures.
Beyond that, there are two commonly used immutable alternatives — Tuples and Frozensets.
Tuples are an immutable version of lists, and frozensets are an immutable version of sets.
The Pub/Sub Model
Big tech companies like Twitter can handle BILLIONS of notifications for MILLIONS of users thanks to communication patterns like this one.
Pub/Sub is an approach to messaging that is commonly used in distributed systems.
There are three entities involved — publishers, topics, and subscribers.
Subscribers tell the system which messages they would like to be informed about by subscribing to a topic.
Publishers send messages to topics, without knowledge of who the message should be sent to.
A message broker or event bus then forwards these messages to the appropriate subscribers.
Message senders and receivers are heavily decoupled in a Pub/Sub model. This leads to improved scalability, flexibility, and fault tolerance.
That wraps up this week’s issue of Level Up Coding’s newsletter!
Join us again next week where we’ll explore Git branching strategies, how to choose the right database, and methods to improve the performance of your CI/CD pipeline.