Zend framework concurrency and ajax - zend-framework

I'm developing a web application with zend framework. It is an app for selling movie tickets. The app allows users to select a seat and then buy the ticket. (The problem is similar to the classical air ticket booking.)
I have two problems.
Problem 1: The page that shows all seats has to refresh with ajax (a seat is green if available and red if unavailable): each seat is a div in the page. For example if USER A is watching the page with the seats and another USER B buy a ticket, the corresponding seat has to become red for USER A without refresh the page. How could i refresh every div? i'd like to use jquery, i tried to do a json request for each div every second but it is too slow. Any idea?
Problem 2: How could i manage the concurrency? If USER A and USER B click simultaneously to buy the same ticket (a ticket for the same seat) what do i have to do? I'm using a InnoDB engine, but do i have to manage the transaction in the relative controller?
Thanks a lot!

I think that you could use Comet for problem 1 to push the data from your webserver to the browser.
Unfortunately, I don't know how to use this practically.

I would probably solve problem 1 and 2 differently (as I have seen on lots of ticketing sites). Once a user selects a seat, I would update the seat with a timestamp in the database. That would infer that the seat is taken. The user then has 1 minute (or some amount of time) to purchase the tickets. If they don't then the timestamp changes back to null.
The sql would be fairly easy. Basically the system would scan that table for person B and say return any available seats (rows in the database) where the timestamp is null or (now - timestamp > 1 minute).

Related

How to Implement "Your contact just joined app" feature

I am building a mobile app (in flutter firebase, but the answer does not have to be firebase specific). And I would like to implement a feature that notifies users whenever anyone from their contact list joins the app. This seems like a very EXPENSIVE feature.
At the top of my head, I see a lambda/cloud function that is triggered everytime a user joins and then searches a database of users and their respective contacts for the existence of the new user's phone number. To me, this solution does not scale well for two reasons: if the number of total users is in the millions and the number of users joining simultaneously is a lot.
My better solution is to get the user's contacts upon joining and then searching a database of current users contacts for any of the phone numbers of the newly joined user.
Is there a solution better than the second one? If so, what is it? If the second solution is standard, what kind of backend storage mechanism provides the best search and retrieval time for a database of users and their respective contacts?
In the case of large users ill not do first solution because that may slow the sign up process instead i will creat a cron job that runs at a specific time or periodically it will get the list of the latest users signed up that day or that hour whatever you prefer then that cron will check the new user if related to any user in the databases and send a notification right away, or a better solution create a temporary table in a database in another server insert the notification informations into the other server, creat another cron job in the second server it will run at a specific time to sendthe notification

Prevent race-condition in RESTful api in certain scenario

I'm currently developing an online course system where students can choose any course and enroll to it. The course will be held one-to-one principle so student will choose a certain date for the course and on that date, there will be an online video meeting with the instructor. Only one instructor and one student for each course session.
Typical use-case flow is:
Student press "Enroll" button and proceeds to the next page.
On this page, student chooses the course date from the calendar (only from available dates) and proceeds to checkout page.
On checkout page, student enters his/her card details and certain amount charged from student.
A race condition may occur in this scenario (let's say there are only 2 users and 1 instructor):
User1 chooses date from the calendar and proceeds to the checkout page.
Meanwhile User2 also chooses exactly same date and proceeds to checkout page.
User2 enters card details faster that User1 and reserves that date.
User1 enters card details and system charges both students and BINGO (there are two students for the same date).
I don't want to check date availability before payment so I think It'll give a bad user experience so the User must go to the previous step again and choose another date. Even this could happen infinitely :)
Any ideas will be welcomed.
Also, I can change the current enrollment flow to protect security.
The reference you want to review is Pat Helland 2007: Memories, Guesses and Apologies
You've got a distributed system, and remote clients are looking at local copies of your data that may be out of date. So your protocol needs to recognize that you will be receiving messages about decisions based on stale data, and have explicit handling for the contingency that the desired outcome of the decision is not currently available.
The REST part is "just" providing the correct affordances for your protocol.
One possible change to your protocol that may help is to introduce the idea of a provisional hold; Alice has a provisional hold on the time slot, and therefore when Bob asks the slot is unavailable, but it might become available later if Alice declines to exercise the option.
(This doesn't eliminate the race condition, of course, it just moves it around).
A common protocol solution here is overbooking - you accept both claims on the time slot, and then clean up the mess later.
Commercial airlines do this sort of thing all the time; they want to maximize their profit per flight, which means selling more tickets than there are seats on the plane. They can do this, because enough travelers change their plans later that there is an effective surplus.
But sometimes, too many paying customers show up for the same flight, and then the contingency plans come out -- standby passengers are deferred, ticketed customers are offered compensation packages for changing their plans, and so on.
You probably need contingency protocols anyway (what happens if the instructor has to cancel the appointment, for example because of illness); the race condition during booking is just one more contingency protocol to add to the run book.
Having established what the contingency protocol should be, you then have a second question to explore: what parts of that protocol should be automated. If conflicts are rare, it may make sense to escalate the problem to a human being to solve, rather than doing so in code. Sometimes the right answer is for the machine to stay out of the way.
I want to implement double-check mechanism so:
When User1 proceeds to calendar page it will create a persistent connection with the server (SSE or WebSocket). And available dates will be shown on real-time. So when User1 selects any date and proceeds to checkout an event will be published and that date will be marked as BLOCKED until the payment done.
When User1 enters his card details and clicks Pay button the system will check again if that date is really reserved by User1.
If payment is successful this date will be updated from BLOCKED to RESERVED.
BUT taking into account that this is a REST API with React client, all of the endpoints will be visible to anyone. So an attacker could make a simple brute-force to BLOCK all available dates for the course.

Instagram style news feed logic

I am in the process of making a social application that involves users and a news feed. In order to display a user's news feed in chronological order, which of the following would make more sense?
Get all posts from each user the person is following and sort them into chronological order
Each time a user goes to post, save that id and the date into each
of their followers profile data (seems redundant)
A better solution that you may know
Generally social sites are built using Graph Database. I have worked on one of the social sites. Below was the technology stack and logic to display the news feeds.
Java, Spring MVC
OrientDb- A GraphDB containing Vertex connected with Edges
JQuery & Bootstrap for UI
Apache Solr- for text searches on User,Articles
Below was the logic for newsFeeds:
When any user does any activity, we used to create Activity vertex containing
the meta-data of activity like dateTime, type of activity and actor etc.
We had a batch program(running after every 3 mins) which will go thr all unprocessed the activities. The onus of the batch is to generate the newsFeeds from Activity and assign these newFeeds_To_Be_Displayed to relevant users(i.e connections of actor) newsFeeds to be displayed on screen.
When the user logs in, we used to query newFeeds_To_Be_Displayed of the given user and display them on screen.
We chose to calculate the newFeeds_To_Be_Displayed asynchronously, as thats a time consuming process and we dint want to end up in bad user experience.

Analysis class diagram - associating classes

New user so I can't post images. Image link provided below:
http://i.stack.imgur.com/EXf0G.jpg
This is for a walk-in booking system not an online reservation system.
Normal Booking scenario:
User/Member gives information to receptionist. Users can book up to a month in-advance.
Receptionist searches user/member info. Receptionist must be logged in to search user/member or make a booking.
if details are found the booking continues as normal, if not user details are added to the users file.
Booking time/date/type is then checked for availability. If available then a booking is made.
Extra:
There are two types of staff account 'normal-user' (Receptionist) and 'admin' (Manager).
Manager can reset staff account passwords and create new staff accounts.
Manager can edit session details on the timetable (time, date, type) etc. Do i need a timetable class here??
In order to answer that, we would need a much more developed specification.
I would suggest you develop with what you have, meeting only the minimum requirements for each iteration. Then, if you find your users need a timetable of some sort, then add it at that point.
In general, don't add more complexity than you need until you know you need it. The more moving parts a system has, the harder it is to maintain and use and to put together in the first place. Get the application up and functioning and in the users' hands. Until you get real feedback from them, you are just taking stabs in the dark. Let the users' tell you what they need and want.

How do I show information to users belonging to different groups (web) in modx

I have created a website using modx evolution v1.0.2.
The website that I have developed has 12 different types of users (categorized in groups). Each user will be shown a different price depending on the group to which he belongs.
Till now I have been able to fetch the group name of current logged in user (created a snippet for that), but how can I achieve the above mentioned functionality so that each user should be able to see only the price that I have coded according to his group.
For example:
If a user is associated with the 'ocassional' group then he should be shown the price as , say, 50 bucks
and if a user is associated with the 'regular' group then he should be shown the price as, say, 40 bucks
I can easily do this by coding a single snippet for every product's variant, but there are a lot of variants (more than 100 and growing).
I have created a resource(page) for every product and it's variant. Every variant has a price. It is this price that I want to be shown according to the logged in user group membership.
I hope I am able to explain my query clearly.
Please help me do this functionality.
Thanks
I'm not really convinced that modx (as much as i love it) is a great solution for e-commerce sites.
However, the best solution might be to use the database to store the prices of variants per user group and retrieve them yourself using a snippet.