These are assessments and choices that were made earlier in the
project process. This means that they may differ from current
choices and results. However, they were part of building the
foundation for what the project has, and will become.
Identifying users across services
Problem: With a separate database per service, how do you identify which users have which booking or payment? Decision: Carry user_id between services. Fields holding ids used in several databases are required, but not generated automatically. Show full reasoning Hide full reasoning
Trade-off: More manual handling and ids sent between services, instead of the full power of relational databases. In return: better separation of responsibility, and a more modular system that mostly keeps working if one service or database goes down.
When each microservice has its own database, how do you identify which users have which booking/payment? The user data lives in the auth database, and user_id has no relational meaning outside of it, the way it would have in one shared relational database. Still, user_id can be carried and handled by the system as a whole. This is just a symptom of separate databases per service preventing the use of the "full power" of relational databases, which requires more manual handling and ids being sent between services. In return, it gives better separation of responsibility and a more modular system, where the rest still functions fully if one service/db goes down. The databases must therefore be well planned to reduce redundancy and optimize speed. Database fields with ids used in several databases are set as required, but are not generated automatically.
Basic user information
Problem: If a front-end is added later, which service should deliver a user's name and basic information? Decision: A separate user service for non-auth user data (name, phone number, address). The auth database only holds user_id, email, password and role. Show full reasoning Hide full reasoning
Trade-off: An extra service and database, which can become overcomplicated. In return: it respects separation of responsibility, and gives a foundation for later changes to information that is not auth-related.
If a front-end is implemented later, it should show the user's name and some basic information, and which service should deliver that in that case? The auth service is a poor fit, since its responsibility is auth, but a separate service with its own database just for this can become overcomplicated. In return, it would provide a foundation for users to later change information that is not auth-related. Changing auth-related information, such as email, can on the other hand get messy, since it requires updates in several databases and services. It depends on the scope and focus of the project. I have chosen to create a user service for user-related functions that are not auth, for example employees being able to see customers' names and contact information. Then the auth database only needs user_id, email, password and role (user/admin), while name, phone number, address etc. are in the user service's own database. This respects separation of responsibility, since such information is not relevant for auth.
Caching and updating the db
Problem: When a post request is cached, should the db be updated first, or the cache? Decision: No single method fits everywhere. Reads use cache-aside. Writes mainly use write-through, sometimes with distributed locking. Show full reasoning Hide full reasoning
Trade-off: Updating the db first is slower but safer. Updating the cache first is faster, but the client may get a success response even if the db update then fails.
How should caching and updating the db be done? When a post request is to be cached, should the db be updated first and then the cache, or should the cache be updated first, the response sent to the client, and the db updated afterwards? Updating the db first takes longer, but is safer. Updating the cache first is faster, but more vulnerable: If the db update fails, the client has already received a success response, even though the response should have been an error. Ideally you want both speed and accuracy, but it is not certain both are possible. Caching must be assessed against several scenarios, both reading and writing, within and across microservices, so a single method does not fit everywhere. Fast response must go together with robustness and few points of failure. For reading, "cache aside" is used: The cache is checked first, then the db, and data is put in the cache when the db is read. For writing, it depends on the task, since some caching requires locking (bookings), while others concern updates. Mainly write-through is used, and sometimes distributed locking: It is written to the cache first, and the process is completed before the data is passed on from the cache to the db. That way the system can check that the write to the db went correctly before it responds to the client.
Reserving a booking
Problem: How do you hold a room for a user in the middle of a booking, so that others cannot book it but this user still can? Decision: Booking has two steps, reservation and payment. The booking service creates a booking_id and stores it in its cache with user_id, and asks the inventory service to reserve the room in its cache with that booking_id. Show full reasoning Hide full reasoning
Trade-off: If a db goes down, especially booking, data users need to check in is lost. Rebuilding it from the other databases (a kind of "RAID") is possible, but needs extra data and may not respect separation of responsibility as well.
What is the best way to reserve a room while a user is in the middle of a booking, so that it is not available to others, but still is for this user? If you store the reservation in the inventory service's cache, it has to store extra data that is somewhat redundant, but the cache must show that the room is unavailable anyway, so that others do not try to book the same one. The booking service's cache is more relevant, but is primarily used to lock bookings and prevent double payment, so it can come into conflict. An alternative is an endpoint that only reserves: the booking service creates a booking_id, stores it in its cache with user_id, and asks the inventory service to reserve the room in its cache with booking_id. Booking thus has two steps: reservation and payment. Changes before payment are updated in both caches. At payment, booking_id is retrieved via user_id, the booking is locked against double payment, and data is retrieved from the inventory service. When the payment is completed, data is moved from the cache to the db in the booking and inventory services, and the payment is stored in the payment db with booking_id and user_id. If a db goes down, especially booking, you lose data users need to check in. A system that puts together the lost information from the other databases (a kind of "RAID") is possible, but requires extra data and perhaps does not respect separation of responsibility as well.
Different booking variants
Problem: How can the same room type be offered in several variants, for example with or without breakfast, at different prices? Decision: Store variants as children of the room type. The room type holds shared data (base price, occupancy), and each variant holds an additional price and what it includes. Managers can create, change and deactivate variants. Show full reasoning Hide full reasoning
Trade-off: Needs two new tables in the inventory db. Variants tied to bookings cannot be deleted, only deactivated.
The hotel currently cannot offer multiple options for the same room type, for example with or without breakfast, with different prices. Separate room types for each variant can create problems with availability, and making it an add-on forces users to add something that could just as well have been part of the room. Therefore variants should be stored as "children" of the room type: the room type has data that applies to all variants, such as base price and occupancy, while each variant has an additional price and what it includes. This requires two new tables in the inventory db, but linking them only with a variant number creates more mess and is harder to handle. Managers must be able to create, change and deactivate variants, and since they are tied to bookings they cannot be deleted. The booking item must also know which variant it concerns, so that employees and users see what the booking includes. This requires no new table, since each item is one room, and one room can only be one variant. A table is therefore created for room variants and a table that links what each variant includes, for example breakfast or extended refund. The variant's id (PK) is added to the booking item table, and managers get three endpoints: create, change and deactivate. Deactivation determines whether the variant can be booked, without removing data needed for existing bookings.
Deleting booking options
Problem: How can a booking option (room, accommodation, bed etc.) be removed when there may be bookings with it? Decision: Do not delete. Set "is_active: false", and only delete when no future bookings are linked to it. Show full reasoning Hide full reasoning
Trade-off: Deleting would break relations within and between services, for example a payment no longer linked to an actual user, or a room type that no longer exists for future bookings.
If a booking option (room, accommodation, bed etc.) is deleted while there are bookings with it, you lose necessary information. How can something be removed from the inventory database or as a bookable option when there may be bookings with it? Deletion can create problems with relations within and between services, for example a payment no longer being linked to an actual user. If a room type is deleted, there are also problems for future bookings, since the room type no longer exists. It is therefore more relevant to use flags, such as "is_active", so that the system stays correctly linked. Elements are therefore not deleted directly, but set to "is_active: false", and are only deleted when no future bookings are linked to them.