News for hackers. Stuff that matters.
Re:Why Uber Engineering Switched from Postgres to MySQL by tormeh on Tuesday July 26, 2016 @02:52PM (#12167937)
So the correct version is, I guess:

Open db connection 1

get data for receipt

Close db connection 1

generate receipt

send email

open db connection 2

write success to database

close db connection 2

I guess you could speed this up a lot by doing it in bulk instead of opening and closing db connections twice for every email. Anyway, the version you wrote sounds reasonable for everything but really big operations to me, but then again I'm fairly junior.

Re:Why Uber Engineering Switched from Postgres to MySQL by jedberg on Tuesday July 26, 2016 @03:04PM (#12168029)
You can still do it in one transaction, sort of. Replace "send email" with "queue email" and then either eliminate the step that writes the success to the database (pushing that off to the queue processor, who will open or already have open a database connection) or instead write to the DB that you queued the item.

The advantage you gain is that the queue is a nice buffer if something gets held up with email sending, and also, the queue processor can work in bulk, say sending 100 emails, and then opening a connection to the DB and writing them all in one statement.

Re:Why Uber Engineering Switched from Postgres to MySQL by 1024core on Tuesday July 26, 2016 @11:07PM (#12170234)
So if the "send email" step fails (temporarily), the next worker to come along will grab the same receipt and send it again?

I think a better solution would be to use a centralized queue that actually does the mail sending, and retries in case of failure.

Re:Why Uber Engineering Switched from Postgres to MySQL by srd on Wednesday July 27, 2016 @02:03AM (#12170830)
That depends on the implementation of the queue. Most queues have a "failed/retry" concept, where they mark failed jobs to be retried in some set future point in time. So one failed job does not hold up your entire queue processing.
Re:Why Uber Engineering Switched from Postgres to MySQL by ChoHag on Wednesday July 27, 2016 @07:45AM (#12171995)
> a centralized queue that actually does the mail sending, and retries in case of failure.

A bit like SMTP?

Re:Why Uber Engineering Switched from Postgres to MySQL by lazyant on Wednesday July 27, 2016 @10:56AM (#12173305)
not necessarily, in the first write we can update receipt status as "queued" so queue system can update to "sent" or retry
Re:Why Uber Engineering Switched from Postgres to MySQL by viraptor on Tuesday July 26, 2016 @06:58PM (#12169347)
Depending on the number of email sending workers, it could be closer to:

    transaction 1:
        mark an unsent receipt as being processed
        get the marked receipt
    send email
    transaction 2:
        mark the receipt as sent
Re:Why Uber Engineering Switched from Postgres to MySQL by nawitus on Tuesday July 26, 2016 @03:02PM (#12168017)
You don't need to close the database connection to not have an open transaction, right?
Re:Why Uber Engineering Switched from Postgres to MySQL by jedberg on Tuesday July 26, 2016 @03:08PM (#12168051)
No you don't. But depending on the ORM, some of them batch writes unless you explicitly tell them not to, but will flush when you close the connection. So some programmers will just close the connection because they don't understand the difference or because they've learned that closing the connection guarantees that the data is written.
Re:Why Uber Engineering Switched from Postgres to MySQL by mschuster91 on Tuesday July 26, 2016 @03:35PM (#12168205)
> So some programmers will just close the connection because they don't understand the difference or because they've learned that closing the connection guarantees that the data is written.

To be fair, this problem isn't limited to databases. Filesystems, even HDDs/SSDs, have been known to readily ignore flush() calls in order to achieve better benchmark results.