You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/12/en/part12b.md
+38-26Lines changed: 38 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -512,7 +512,7 @@ There are two distinct methods to store the data:
512
512
513
513
The first choice is preferable in most cases whenever one <i>really</i> needs to avoid the data being deleted.
514
514
515
-
Let's see both in action with Docker compose. Let us start with <i>bind mount</i>:
515
+
Let's see both in action with Docker compose. Let us start with <i>bind mount:</i>
516
516
517
517
```yml
518
518
services:
@@ -531,7 +531,7 @@ services:
531
531
532
532
The above will create a directory called *mongo\_data* to your local filesystem and map it into the container as _/data/db_. This means the data in _/data/db_ is stored outside of the container but still accessible by the container! Just remember to add the directory to .gitignore.
533
533
534
-
A similar outcome can be achieved with a <i>named volume</i>:
534
+
A similar outcome can be achieved with a <i>named volume:</i>
535
535
536
536
```yml
537
537
services:
@@ -560,7 +560,7 @@ local todo-backend_mongo_data
560
560
$ docker volume inspect todo-backend_mongo_data
561
561
[
562
562
{
563
-
"CreatedAt": "2022-10-04T12:52:11Z",
563
+
"CreatedAt": "2024-19-03T12:52:11Z",
564
564
"Driver": "local",
565
565
"Labels": {
566
566
"com.docker.compose.project": "todo-backend",
@@ -626,8 +626,8 @@ We know how to answer the latter: by listing the running containers.
626
626
627
627
```bash
628
628
$ docker container ls
629
-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
630
-
3f831a57b7cc nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp keen_darwin
629
+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
630
+
3f831a57b7cc nginx ...3 sec ago Up 2 sec 80/tcp keen_darwin
631
631
```
632
632
633
633
Yes! We got the first question answered as well. It seems to listen on port 80, as seen on the output above.
@@ -649,8 +649,8 @@ Let's look at the app by going to http://localhost:8080. It seems that the app i
649
649
650
650
```bash
651
651
$ docker container ls
652
-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
653
-
7edcb36aff08 nginx "/docker-entrypoint.…"About a minute ago Up About a minute 0.0.0.0:8080->80/tcp, :::8080->80/tcp wonderful_ramanujan
@@ -683,7 +683,7 @@ Refresh the page, and our message is displayed! Now we know how exec can be used
683
683
684
684
> Use _script_ to record what you do, save the file as script-answers/exercise12_8.txt
685
685
686
-
While the MongoDB from the previous exercise is running, access the database with Mongo command-line interface (CLI). You can do that using docker exec. Then add a new todo using the CLI.
686
+
While the MongoDB from the previous exercise is running, access the database with the Mongo command-line interface (CLI). You can do that using docker exec. Then add a new todo using the CLI.
687
687
688
688
The command to open CLI when inside the container is _mongosh_
689
689
@@ -698,7 +698,7 @@ When you have connected to the Mongo cli you can ask it to show dbs inside:
698
698
```bash
699
699
> show dbs
700
700
admin 0.000GB
701
-
config 0.000GB
701
+
config 0.000GB
702
702
local 0.000GB
703
703
the_database 0.000GB
704
704
```
@@ -734,7 +734,7 @@ We can now access the data in those collections:
734
734
]
735
735
```
736
736
737
-
Insert one new todo with the text: "Increase the number of tools in my toolbelt" with status done as false. Consult the [documentation](https://docs.mongodb.com/v4.4/reference/method/db.collection.insertOne/#mongodb-method-db.collection.insertOne) to see how the addition is done.
737
+
Insert one new todo with the text: "Increase the number of tools in my toolbelt" with the status done as <i>false</i>. Consult the [documentation](https://docs.mongodb.com/v4.4/reference/method/db.collection.insertOne/#mongodb-method-db.collection.insertOne) to see how the addition is done.
738
738
739
739
Ensure that you see the new todo both in the Express app and when querying from Mongo CLI.
740
740
@@ -744,13 +744,13 @@ Ensure that you see the new todo both in the Express app and when querying from
744
744
745
745
### Redis
746
746
747
-
[Redis](https://redis.io/) is a [key-value](https://redis.com/nosql/key-value-databases/) database. In contrast to eg. MongoDB, the data stored to a key-value storage has a bit less structure, there are eg. no collections or tables, it just contains junks of data that can be fetched based on the <i>key</i> that was attached to the data (the <i>value</i>).
747
+
[Redis](https://redis.io/) is a [key-value](https://redis.com/nosql/key-value-databases/) database. In contrast to eg. MongoDB, the data stored in key-value storage has a bit less structure, there are eg. no collections or tables, it just contains junks of data that can be fetched based on the <i>key</i> that was attached to the data (the <i>value</i>).
748
748
749
-
By default Redis works <i>in-memory</i>, which means that it does not store data persistently.
749
+
By default, Redis works <i>in-memory</i>, which means that it does not store data persistently.
750
750
751
-
An excellent use case for Redis is to use it as a <i>cache</i>. Caches are often used to store data that is otherwise slow to fetch and save the data until it's no longer valid. After the cache becomes invalid, you would then fetch the data again and store it in the cache.
751
+
An excellent use case for Redis is to use it as a [cache](). Caches are often used to store data that is otherwise slow to fetch and save the data until it's no longer valid. After the cache becomes invalid, you would then fetch the data again and store it in the cache.
752
752
753
-
Redis has nothing to do with containers. But since we are already able to add <i>any</i> 3rd party service to your applications, why not learn about a new one.
753
+
Redis has nothing to do with containers. But since we are already able to add <i>any</i> 3rd party service to your applications, why not learn about a new one?
754
754
755
755
</div>
756
756
@@ -774,9 +774,9 @@ Since the Docker Hub page doesn't have all the info, we can use Google to aid us
774
774
775
775

776
776
777
-
We won't have any idea if the configuration works unless we try it. The application will not start using Redis by itself, that shall happen in next exercise.
777
+
We won't have any idea if the configuration works unless we try it. The application will not start using Redis by itself, that shall happen in the next exercise.
778
778
779
-
Once Redis is configured and started, restart the backend and give it the <i>REDIS\_URL</i>, that has the form <i>redis://host:port</i>
779
+
Once Redis is configured and started, restart the backend and give it the <i>REDIS\_URL</i>, which has the form <i>redis://host:port</i>
780
780
781
781
```bash
782
782
$ REDIS_URL=insert-redis-url-here MONGO_URL=mongodb://the_username:the_password@localhost:3456/the_database npm run dev
@@ -819,12 +819,12 @@ The project already has [https://www.npmjs.com/package/redis](https://www.npmjs.
819
819
820
820
- setAsync function takes in key and value, using the key to store the value.
821
821
822
-
- getAsync function takes in key and returns the value in a promise.
822
+
- getAsync function takes in a key and returns the value in a promise.
823
823
824
824
Implement a todo counter that saves the number of created todos to Redis:
825
825
826
826
- Step 1: Whenever a request is sent to add a todo, increment the counter by one.
827
-
- Step 2: Create a GET /statistics endpoint where you can ask the usage metadata. The format should be the following JSON:
827
+
- Step 2: Create a GET /statistics endpoint where you can ask for the usage metadata. The format should be the following JSON:
828
828
829
829
```json
830
830
{
@@ -836,15 +836,15 @@ Implement a todo counter that saves the number of created todos to Redis:
836
836
837
837
> Use _script_ to record what you do, save the file as script-answers/exercise12_11.txt
838
838
839
-
If the application does not behave as expected, a direct access to the database may be beneficial in pinpointing problems. Let us try out how [redis-cli](https://redis.io/topics/rediscli) can be used to access the database.
839
+
If the application does not behave as expected, direct access to the database may be beneficial in pinpointing problems. Let us try out how [redis-cli](https://redis.io/topics/rediscli) can be used to access the database.
840
840
841
841
- Go to the Redis container with _docker exec_ and open the redis-cli.
842
842
- Find the key you used with _[KEYS *](https://redis.io/commands/keys)_
843
-
- Check the value of the key with command [GET](https://redis.io/commands/get)
843
+
- Check the value of the key with the command [GET](https://redis.io/commands/get)
844
844
- Set the value of the counter to 9001, find the right command from [here](https://redis.io/commands/)
845
845
- Make sure that the new value works by refreshing the page http://localhost:3000/statistics
846
846
- Create a new todo with Postman and ensure from redis-cli that the counter has increased accordingly
847
-
- Delete the key from cli and ensure that counter works when new todos are added
847
+
- Delete the key from the cli and ensure that the counter works when new todos are added
848
848
849
849
</div>
850
850
@@ -863,14 +863,14 @@ services:
863
863
- ./redis_data:/data
864
864
```
865
865
866
-
The data will now be persisted to directory <i>redis_data</i> of the host machine.
866
+
The data will now be persisted to the directory <i>redis_data</i> of the host machine.
867
867
Remember to add the directory to .gitignore!
868
868
869
869
#### Other functionality of Redis
870
870
871
-
In addition to the GET, SET and DEL operations on keys and values, Redis can do also a quite a lot more. It can for example automatically expire keys, that is a very useful feature when Redis is used as a cache.
871
+
In addition to the GET, SET and DEL operations on keys and values, Redis can do also quite a lot more. It can for example automatically expire keys, which is a very useful feature when Redis is used as a cache.
872
872
873
-
Redis can also be used to implement so called [publish-subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) (or PubSub) pattern that is a asynchronous communication mechanism for distributed software. In this scenario Redis works as a <i>message broker</i> between two or more services. Some of the services are <i>publishing</i> messages by sending those to Redis, that on arrival of a message, informs the parties that have <i>subscribed</i> to those messages.
873
+
Redis can also be used to implement the so-called [publish-subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) (or PubSub) pattern which is an asynchronous communication mechanism for distributed software. In this scenario, Redis works as a <i>message broker</i> between two or more services. Some of the services are <i>publishing</i> messages by sending those to Redis, which on arrival of a message, informs the parties that have <i>subscribed</i> to those messages.
874
874
875
875
</div>
876
876
@@ -880,8 +880,20 @@ Redis can also be used to implement so called [publish-subscribe](https://en.wik
880
880
881
881
#### Exercise 12.12: Persisting data in Redis
882
882
883
-
Check that the data is not persisted by default: after running _docker compose -f docker-compose.dev.yml down_ and _docker compose -f docker-compose.dev.yml up_ the counter value is reset to 0.
883
+
Check that the data is not persisted by default: after running
884
884
885
-
Then create a volume for Redis data (by modifying <i>todo-app/todo-backend/docker-compose.dev.yml </i>) and make sure that the data survives after running _docker compose -f docker-compose.dev.yml down_ and _docker compose -f docker-compose.dev.yml up_.
885
+
```bash
886
+
docker compose -f docker-compose.dev.yml down
887
+
docker compose -f docker-compose.dev.yml up
888
+
```
889
+
890
+
the counter value is reset to 0.
891
+
892
+
Then create a volume for Redis data (by modifying <i>todo-app/todo-backend/docker-compose.dev.yml </i>) and make sure that the data survives after running
0 commit comments