The video owner has disabled playback on external websites.

This video is no longer available on YouTube.

This video cannot be played right now.

Watch on YouTube

KI-gestützte Lerntools freischalten

Registriere dich, um leistungsstarke Tools zu nutzen, die dir helfen, schneller aus jedem Video zu lernen.

Szenen-Erklärer Phrasen-Jäger Karteikarten-Review Nachsprechübung Sprachausgabe
Kostenlos registrieren
Englisch 22:25 Science & Tech

CQRS in Python: Clean Reads, Clean Writes

Late Night with Seth Meyers · 36,281 Aufrufe · Hinzugefügt vor 4 Tagen

Untertitel (558 Segmente)

00:00

Let's say you're doing customer support

00:02

for OpenAI and you want to compute some

00:04

data to get an idea of how broken your

00:05

product is, but your API is a bit

00:08

limited and can only return all ticket

00:10

data at once. That is probably a lot of

00:13

data. On top of that, say you're running

00:15

some analytics script that gathers that

00:17

data while those support tickets are

00:19

flowing in, your reads and your rights

00:21

may interfere and that leads to all

00:23

sorts of performance issues. Now, you

00:26

can avoid all those problems using the

00:28

CQRS architecture. And I'll explain

00:31

exactly how it works in this video using

00:33

an example with fast API and MongoDB. I

00:36

talk about when to use it and when not

00:38

to use it and why MongoDB actually fits

00:41

this architecture really well. That's

00:43

why they're also the sponsor of this

00:45

video. Now, let's take a look at the

00:48

code. Like I said, the starting point is

00:50

a fast API app that uses MongoDB. So

00:53

first create this file that uses pyongo

00:55

to set up a MongoDB database. So create

00:58

some uh URL. This by the way I'm running

01:00

MongoDB locally on my machine with

01:03

Docker Compose and then I define a

01:05

database type. I create a client

01:08

with that URL. And then I have a couple

01:10

of functions that are useful in my app

01:13

like getting the database which will be

01:15

injected into the endpoints uh shutting

01:17

down the database and some helpful

01:20

things like being able to convert

01:21

strings to and from object ids. Now the

01:25

API app itself is then also pretty

01:27

straightforward. So I have a collection

01:30

of tickets, customer support tickets.

01:32

These tickets can have a status that's

01:34

either open, triaged or closed. Uh I

01:37

have a couple of other functions that

01:39

are useful like getting the current

01:41

time. Uh making a preview of a message

01:43

that's part of a customer support

01:45

ticket. Then I create the app. Uh

01:47

there's a startup phase which gets the

01:49

DB and set some indices which is helpful

01:52

for uh you know common queries that we

01:55

do. Uh then there's of course shutting

01:57

down the database when the app shuts

01:59

down. And then there's a couple of

02:01

pyantic models. uh one for creating

02:05

tickets uh one for updating tickets. As

02:07

you can see, we can update the status or

02:09

add a note to a customer support ticket.

02:11

Then we have a ticket details model that

02:13

contains basically everything related to

02:15

the ticket. And then there's another uh

02:19

model here called ticket list item that

02:21

I'll talk about in a minute. And then we

02:23

have some endpoints. So I can post to

02:26

the tickets endpoint and that gets a

02:29

payload which is this ticket in then

02:31

creates a ticket. uh we have updating a

02:34

ticket uh that this is actually kind of

02:36

a complicated operation because well uh

02:39

of course first have to check that the

02:41

ticket actually exists then uh we can

02:44

only update certain things in certain

02:46

ways. For example, if a status is closed

02:49

then uh we cannot reopen that ticket in

02:52

this particular setup. And for updating

02:55

the agent there is actually no rule

02:57

though we can simply update that. And

02:58

then I have list tickets endpoint that

03:00

retrieves tickets. talk more about that

03:02

in a minute. Uh we have retrieving a

03:04

particular ticket uh given an ID and I

03:08

have a sort of dashboard endpoint that

03:10

does some aggregates like collecting how

03:12

many tickets are open, triaged and

03:14

closed. So that's the API and this

03:16

interacts with this database that I

03:18

created right here. Let's say that there

03:20

is a UI requirement that the list view

03:24

needs some extra information like a

03:26

short preview of the agent note or a has

03:30

note boolean flag. So what you would do

03:32

if you just implement stuff right inside

03:35

this fast API app is that you would have

03:37

this ticket list item model that

03:40

contains the um preview message and a

03:44

has node boolean value, right? And then

03:46

in the endpoint where we list the

03:48

tickets, we would basically return that

03:50

as a response model. And that's already

03:53

where you see kind of a problem

03:55

occurring here because in this uh list

03:58

view, we need to of course compute this

04:00

preview and compute whether there there

04:02

is a note. That means that every time

04:04

you list the tickets using this endpoint

04:06

that these things are going to be

04:08

computed. Now, of course, computing a

04:10

preview message and whether there's a

04:12

note or not is not really that

04:14

computationally intensive unless you

04:16

have a lot of tickets, but uh you can

04:19

imagine that there are other things that

04:21

may also add uh extra uh computation and

04:25

extra problems if you need to compute

Vollständige Untertitel im Videoplayer verfügbar

Mit Übungen trainieren

Erstelle Vokabel-, Grammatik- und Verständnisübungen aus diesem Video

Vokabeln & Grammatik Verständnisquiz IELTS-Prüfung Schreib-Übung
Registrieren zum Üben
Noch keine Kommentare. Sei der Erste, der seine Gedanken teilt!

Registriere dich, um alle Features freizuschalten

Verfolge deinen Fortschritt, speichere Vokabeln und übe mit Übungen

Kostenlos Sprachen lernen