Skip to content

Commit 4015af9

Browse files
committed
clarifications 12b
1 parent aff13f8 commit 4015af9

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

src/content/12/en/part12b.md

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ There are two distinct methods to store the data:
512512
513513
The first choice is preferable in most cases whenever one <i>really</i> needs to avoid the data being deleted.
514514
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>
516516
517517
```yml
518518
services:
@@ -531,7 +531,7 @@ services:
531531
532532
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.
533533
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>
535535
536536
```yml
537537
services:
@@ -560,7 +560,7 @@ local todo-backend_mongo_data
560560
$ docker volume inspect todo-backend_mongo_data
561561
[
562562
{
563-
"CreatedAt": "2022-10-04T12:52:11Z",
563+
"CreatedAt": "2024-19-03T12:52:11Z",
564564
"Driver": "local",
565565
"Labels": {
566566
"com.docker.compose.project": "todo-backend",
@@ -626,8 +626,8 @@ We know how to answer the latter: by listing the running containers.
626626

627627
```bash
628628
$ 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
631631
```
632632

633633
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
649649

650650
```bash
651651
$ 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
652+
CONTAINER ID IMAGE COMMAND PORTS NAMES
653+
7edcb36aff08 nginx ... 0.0.0.0:8080->80/tcp wonderful_ramanujan
654654

655655
$ docker exec -it wonderful_ramanujan bash
656656
root@7edcb36aff08:/#
@@ -683,7 +683,7 @@ Refresh the page, and our message is displayed! Now we know how exec can be used
683683

684684
> Use _script_ to record what you do, save the file as script-answers/exercise12_8.txt
685685
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.
687687

688688
The command to open CLI when inside the container is _mongosh_
689689

@@ -698,7 +698,7 @@ When you have connected to the Mongo cli you can ask it to show dbs inside:
698698
```bash
699699
> show dbs
700700
admin 0.000GB
701-
config 0.000GB
701+
config 0.000GB
702702
local 0.000GB
703703
the_database 0.000GB
704704
```
@@ -734,7 +734,7 @@ We can now access the data in those collections:
734734
]
735735
```
736736

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.
738738

739739
Ensure that you see the new todo both in the Express app and when querying from Mongo CLI.
740740

@@ -744,13 +744,13 @@ Ensure that you see the new todo both in the Express app and when querying from
744744

745745
### Redis
746746

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>).
748748

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.
750750

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](![alt text](image.png)). 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.
752752

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?
754754

755755
</div>
756756

@@ -774,9 +774,9 @@ Since the Docker Hub page doesn't have all the info, we can use Google to aid us
774774
775775
![](../../images/12/redis_port_by_google.png)
776776
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.
778778
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>
780780
781781
```bash
782782
$ 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.
819819

820820
- setAsync function takes in key and value, using the key to store the value.
821821

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.
823823

824824
Implement a todo counter that saves the number of created todos to Redis:
825825

826826
- 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:
828828

829829
```json
830830
{
@@ -836,15 +836,15 @@ Implement a todo counter that saves the number of created todos to Redis:
836836

837837
> Use _script_ to record what you do, save the file as script-answers/exercise12_11.txt
838838
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.
840840

841841
- Go to the Redis container with _docker exec_ and open the redis-cli.
842842
- 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)
844844
- Set the value of the counter to 9001, find the right command from [here](https://redis.io/commands/)
845845
- Make sure that the new value works by refreshing the page http://localhost:3000/statistics
846846
- 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
848848

849849
</div>
850850

@@ -863,14 +863,14 @@ services:
863863
- ./redis_data:/data
864864
```
865865
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.
867867
Remember to add the directory to .gitignore!
868868
869869
#### Other functionality of Redis
870870
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.
872872
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.
874874
875875
</div>
876876
@@ -880,8 +880,20 @@ Redis can also be used to implement so called [publish-subscribe](https://en.wik
880880
881881
#### Exercise 12.12: Persisting data in Redis
882882
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
884884
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
893+
894+
```bash
895+
docker compose -f docker-compose.dev.yml down
896+
docker compose -f docker-compose.dev.yml up
897+
```
886898

887899
</div>

0 commit comments

Comments
 (0)