Photo by Dan Roizer on Unsplash

Initial Neo4j Graph Data Modeling of Train Ticket Booking System

May 6: La Manche, One Month Graph Challenge

Vlad Batushkov

--

Welcome word

In this series of small posts I do one simple graph daily. Domain model of graph somehow related to day’s history, some historical event, celebration or person. I do this challenge to learn Neo4j Data Modeling and Cypher. Every day. One month. Follow me. Maybe you will be inspired and next month would be yours One Month Graph Challenge. #OMGChallenge

Domain model

25 year ago, May 6, 1994, was a day of opening ceremony of the transport tunnel under the English Channel, also known as la Manche. Around 51 km of railroad tunnel connects England and France. Today I plan to build a small graph of travel service, where I can store information of booked la Manche journey.

From traveller perspective starting point of travel booking is timetable. System must allowed you to choose a city departing from and city arriving in, outbound date with time and optional date and time in case of round trip. Obviously, trains goes by scheduler and limited passenger capacity. Tickets in most cases must have a place number, also could have a class like premium, standard and budget class, for example. Also there are special baggage tickets, pet tickets and tickets for transporting of bycicle, car, oversized stuff. I don’t really want to complicate system with insurance and other travel supplies.

Graph

Let’s start from trip. What is it? Trip is entity, that connect into one aggregation root several things: train, tickets, cities and schedule information. From train we know all important physical information such as train number, limit of passengers capacity, class of all passenger sits, that is really depends on train abilities. Schedule data toegether with connected cities includes concrete date and time of train departure from and arrival to, time in trip. Tickets are entities that going to be created in future by users as facts of bookings and used to connect booking with concrete trip.

La Manche example will connect just 2 cities: Folkestone (England) and Calais (France). Also, let’s assume that we need to support just 4 possible trips per day: in both directions in moring and evening. That is not cool, if you spent £4.65 billions on six years of construction.

Trip no. 1 from Folkestone to Calais starts at 7:00AM and ends 7:35AM.
Trip no. 2 from Calais to Folkestone starts at 7:00AM and ends 7:35AM.
Trip no. 3 from Folkestone to Calais starts at 8:00PM and ends 8:35PM.
Trip no. 4 from Calais to Folkestone starts at 8:00PM and ends 8:35PM.
Also, just for fun, in weekends every train departure +2 hours later in morning and +1 hours later in evening, 9:00AM and 9:00PM accordingly.

Ok, let’s create timetable for this month:

Code come into the picture to resolve two things: have unique code for every trip in one month. And it helps to have relevant destination for future tickets, will we go from Folkestone to Calais or vice versa. For unique code it is better use string of UUID to make it unique for entire timeline of system, not just one month.

Here small query to get trips for today in all directions:

Now time for bookings. As you can understand bookings entities should been created, when user made a booking. I plan to simulate “made a booking” command with Cypher query.

Let’s made a bookig for a trip. Imagine that, me amd my wife settle down in France now and plan to go to England for few days to watch upcoming Premier League match on Saturday. It is my blog, so why not to dream about it here. So we plan departure tomorrow morning and back in the nearest Sunday evening. I have a user account in some not existed “la Manche Booking System” and booking paid by a credit card.

I need to know trips nodes first, and to be true in not existed “la Manche Booking System” it would be known by trip code, after search for trips for appropriate days, but here I just use small query to get them:

Trip code for tomorrow “3_7” and “2_12” for ride back.

And now let’s made a booking with all required information about it:

Sign up to discover human stories that deepen your understanding of the world.

--

--

Vlad Batushkov
Vlad Batushkov

Written by Vlad Batushkov

Engineering Manager @ Agoda. Neo4j Ninja. Articles brewed on modern tech, hops and indie rock’n’roll.

No responses yet

Write a response