The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

kkelleher (19) [Avatar] Offline
#1
Hi John,

In the examples I have run to date (chp1 -> chp smilie , I've run them from within STS eclipse, not docker.Thus my test db is external to the deployed service.

My (very limited!) reading of how docker & the examples work, is that , via docker compose, the db is started up with the services ( organisation service etc) and populated with data via schema.sql files etc.

My question is this ... if there are multiples instances of say the organization service, are there multiples instances of the organization db and if so , how are they kept in synch?
My understanding is that if I started up a docker image, changed some data, restarted the image, all changes would be lost.

I actually think it is just me missing something important, so if I could just be pointed in the right direction please...

thanks,
Kevin
John C Carnell (44) [Avatar] Offline
#2
Hi Kevin,

Great question.

I will try to see if I can answer your questions.

1. The database used in the Docker container is ephemeral and not backed with a persistent data volume. This means every you start and stop the docker-compose the
database will disappear. Usually you are going to run your databases outside of Docker and have your docker containers talk your database service. In chapter 10,
when I deploy the containers to Amazon, I actually setup an Amazon RDS Postgres instance and then have the Docker containers talk to that. It is possible to setup your
Postgres database instance in Docker to use a persistent volume that will keep the data between starts and stops, but I would highly recommend you get some experience
with this and understand the performance characteristics of the database before your trying using Docker and Postgres. One of the other teams in my company use a
Postgres database in Docker, but had to work pretty hard to tune their database and queries.

2. Typically with microservices each type (not instance) of microservice are going to have their own datastore that only they can access. So for instance, even if you have
multiple instances of the organization service, it would only be speaking one organization database. The licensing service would then have their own database. For purposes
of the book I have only one data store for all of the services so that I do not have multiple databases running in the Docker container. (I could, but then it would go unwieldly).
I have seen different organizations when it comes to databases and microservices. Some companies just have a gentleman's agreement that services will not touch each others data, but they are running on the same physical database. Other, companies will require each service to have their own user id and schema and only the service is allowed to access the tables in the schema. Finally, at the far end of the scale, some companies require physically separate databases.

I hope I answered your question. I am going to update the Appendix to include a note about the database being ephemeral.

Thanks,
John
kkelleher (19) [Avatar] Offline
#3
Thanks John,

Yes, you have answered my question in detail.

Much appreciated,
Kevin
367393 (2) [Avatar] Offline
#4
Hi Jhon, fist I just want to thank you for writing this astonishing book!

now to the questions... xD


I have an existential doubt with docker in chapter4 and I really want to understand how the docker container specifically for postgres is creating the database/tables, as fas as I can see in the main docker folder is as follows:

database:
image: postgres:9.5
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=p0stgr@s
- POSTGRES_DB=eagle_eye_local

but where are you indicating the loading of schema.sql from resources folder in licenses and organizations ? If I go directly to the container I see that the data has been already created, but how? where is the magic?


postgres=# \connect eagle_eye_local;
You are now connected to database "eagle_eye_local" as user "postgres".
eagle_eye_local=# \d
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+----------
public | licenses | table | postgres
public | organizations | table | postgres
(2 rows)

eagle_eye_local=# select * from licenses;
license_id | organization_id | license_type | product_name | license_max | license_allocated | comment
--------------------------------------+--------------------------------------+--------------+-----------------------------+-------------+-------------------+---------
f3831f8c-c338-4ebe-a82a-e2fc1d1ff78a | e254f8c-c442-4ebe-a82a-e2fc1d1ff78a | user | CustomerPro | 100 | 5 |
t9876f8c-c338-4abc-zf6a-ttt1 | e254f8c-c442-4ebe-a82a-e2fc1d1ff78a | user | suitability-plus | 200 | 189 |
38777179-7094-4200-9d61-edb101c6ea84 | 442adb6e-fa58-47f3-9ca2-ed1fecdfe86c | user | HR-PowerSuite | 100 | 4 |
08dbe05-606e-4dad-9d33-90ef10e334f9 | 442adb6e-fa58-47f3-9ca2-ed1fecdfe86c | core-prod | WildCat Application Gateway | 16 | 16 |
(4 rows)

eagle_eye_local=#





John C Carnell (44) [Avatar] Offline
#5
Hi,

I think what you are looking for is the <<service-name>>/src/main/resources/schema.sql directory. If this file is present, Spring will automatically try to execute it against the default database defined via Spring Data. The licensing and organization service both have the schema.sql file defined because you can not guarantee ordering of startup with services with straight up docker-compose (you can, but its basically when the container starts).

I hope that helps and thanks for the buying the book. I am glad you liked it.

Thanks,
John