WEBVTT

1
00:00:00.480 --> 00:00:01.970
<v Jose>Hi, guys and welcome back.</v>

2
00:00:01.970 --> 00:00:04.360
Before we start working on the project,

3
00:00:04.360 --> 00:00:07.500
we need to talk about what is ACID?

4
00:00:07.500 --> 00:00:11.360
And ACID is something in databases, nothing illegal,

5
00:00:11.360 --> 00:00:13.740
and it stands for Atomicity, Consistency,

6
00:00:13.740 --> 00:00:16.120
Isolation, and Durability.

7
00:00:16.120 --> 00:00:18.890
We've discussed before that SQL operations occur

8
00:00:18.890 --> 00:00:20.390
in transactions.

9
00:00:20.390 --> 00:00:23.130
So, are transactions ACID?

10
00:00:23.130 --> 00:00:24.820
And the answer to that is not really,

11
00:00:24.820 --> 00:00:26.830
although they are a result of ACID,

12
00:00:26.830 --> 00:00:31.830
or, rather, in order to be ACID, we must use transactions.

13
00:00:32.400 --> 00:00:34.220
There's no way around it.

14
00:00:34.220 --> 00:00:38.950
Here's why, the first letter in ACID is Atomicity,

15
00:00:38.950 --> 00:00:42.590
and that means that transactions are indivisible.

16
00:00:42.590 --> 00:00:45.020
Every operation must happen successfully

17
00:00:45.020 --> 00:00:47.350
in a transaction or none do.

18
00:00:47.350 --> 00:00:49.120
Essentially, what Atomicity means

19
00:00:49.120 --> 00:00:52.300
is that you can't divide the unit of work.

20
00:00:52.300 --> 00:00:54.840
And here, the unit of work must be a transaction

21
00:00:54.840 --> 00:00:56.870
because if you want to group to operations

22
00:00:56.870 --> 00:01:00.030
that depend on each other, you must be able to do that

23
00:01:00.030 --> 00:01:03.950
without being able to run one, but not the other.

24
00:01:03.950 --> 00:01:06.900
So, for example, if you try to UPDATE and INSERT

25
00:01:06.900 --> 00:01:11.710
in the same transaction, then you must be able to do both.

26
00:01:11.710 --> 00:01:15.230
And if one fails, then the other one is rolled back,

27
00:01:15.230 --> 00:01:17.180
or just ignored.

28
00:01:17.180 --> 00:01:19.700
This is what Atomicity means.

29
00:01:19.700 --> 00:01:22.520
For Consistency, this means that the rules and constraints

30
00:01:22.520 --> 00:01:25.690
of the database are followed at all times.

31
00:01:25.690 --> 00:01:27.640
For example, with foreign key constraints,

32
00:01:27.640 --> 00:01:29.530
you can't have a foreign key pointing

33
00:01:29.530 --> 00:01:32.400
to a non-existing primary key value.

34
00:01:32.400 --> 00:01:33.720
We've learned about this in Postgres

35
00:01:33.720 --> 00:01:35.790
when we had users and bank accounts.

36
00:01:35.790 --> 00:01:37.520
You couldn't delete a user,

37
00:01:37.520 --> 00:01:39.350
but keep the user's bank account

38
00:01:39.350 --> 00:01:42.313
because that was an inconsistency in the data.

39
00:01:43.370 --> 00:01:46.390
There are more constraints than just foreign keys though,

40
00:01:46.390 --> 00:01:49.800
such as UNIQUE constraints, or NOT NULL, and many more.

41
00:01:49.800 --> 00:01:52.900
We're going to learn about those constraints later on.

42
00:01:52.900 --> 00:01:56.770
Isolation means that data is only visible in the database

43
00:01:56.770 --> 00:02:00.053
when the transaction has ended and been committed.

44
00:02:00.900 --> 00:02:03.290
For example, if one transaction makes 10 INSERTs,

45
00:02:03.290 --> 00:02:06.470
the other transactions won't be able to see any of that data

46
00:02:06.470 --> 00:02:09.410
until the first transaction has been committed.

47
00:02:09.410 --> 00:02:11.670
That means that transactions are essentially isolated.

48
00:02:11.670 --> 00:02:14.960
They work on the database as it exists

49
00:02:14.960 --> 00:02:17.670
at this point in time, but they don't see the results

50
00:02:17.670 --> 00:02:20.960
of any other transactions until they have been committed.

51
00:02:20.960 --> 00:02:24.490
Durability means that data is saved to permanent storage

52
00:02:24.490 --> 00:02:26.500
once a transaction is committed.

53
00:02:26.500 --> 00:02:29.240
So, this is in contrast to some other database systems

54
00:02:29.240 --> 00:02:32.370
that operate either partially or fully in memory.

55
00:02:32.370 --> 00:02:35.350
Operating in memory is faster, but it's also a bit riskier.

56
00:02:35.350 --> 00:02:37.280
For example, there's databases

57
00:02:37.280 --> 00:02:40.490
that hold a lot of the results in memory,

58
00:02:40.490 --> 00:02:44.480
and they only save to disc every now and then.

59
00:02:44.480 --> 00:02:46.090
And if you use a database like that,

60
00:02:46.090 --> 00:02:47.130
it's gonna be much faster.

61
00:02:47.130 --> 00:02:51.260
You can write and read from that memory data very quickly,

62
00:02:51.260 --> 00:02:53.470
but if your database server crashes,

63
00:02:53.470 --> 00:02:55.920
you might lose some work.

64
00:02:55.920 --> 00:02:59.760
The Durability aspect of ACID means that SQL databases,

65
00:02:59.760 --> 00:03:02.650
or databases that follow these ACID properties,

66
00:03:02.650 --> 00:03:04.470
will always save to permanent storage

67
00:03:04.470 --> 00:03:06.280
once a transaction is committed.

68
00:03:06.280 --> 00:03:08.440
Remember, if you join this with Isolation,

69
00:03:08.440 --> 00:03:11.060
then it makes sense to start committing transactions

70
00:03:11.060 --> 00:03:13.400
all the time, otherwise the transactions

71
00:03:13.400 --> 00:03:16.010
that aren't committed don't make the data available

72
00:03:16.010 --> 00:03:16.980
to other transactions,

73
00:03:16.980 --> 00:03:18.880
which means that other transactions can't work

74
00:03:18.880 --> 00:03:21.130
with that data, and so on.

75
00:03:21.130 --> 00:03:24.300
So, you want to be committing your transactions consistently

76
00:03:24.300 --> 00:03:26.160
as that saves them to permanent storage,

77
00:03:26.160 --> 00:03:29.420
you can't lose your work, and also makes the data available

78
00:03:29.420 --> 00:03:31.650
to other transactions as well.

79
00:03:31.650 --> 00:03:34.020
So, as I mentioned down below, you can use a database

80
00:03:34.020 --> 00:03:35.550
that operates in memory for things

81
00:03:35.550 --> 00:03:36.910
that you don't really mind losing,

82
00:03:36.910 --> 00:03:39.080
like, for example, cash data

83
00:03:39.080 --> 00:03:41.850
or the logged in status of users.

84
00:03:41.850 --> 00:03:44.170
If the data for logged in status is lost,

85
00:03:44.170 --> 00:03:45.570
the users may need to log in again,

86
00:03:45.570 --> 00:03:47.200
but it's not a massive deal.

87
00:03:47.200 --> 00:03:50.460
Whereas, for most of the other data, you do want to keep.

88
00:03:50.460 --> 00:03:51.990
All right, guys, thank you for watching this video.

89
00:03:51.990 --> 00:03:54.760
I hope you understand a bit more about what ACID is.

90
00:03:54.760 --> 00:03:56.393
I'll see you in the next one.

