Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7186247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:35:29+00:00 2026-05-28T18:35:29+00:00

I have an API that services a web-based plugin for processing email. The API

  • 0

I have an API that services a web-based plugin for processing email. The API is responsible for two things:

  1. Creating SessionIDs so the plugin can setup a dynamic link; and
  2. Once an email is sent, for receiving that SessionID, the email recipients and subject line, to store the information into a new session.

Imagine the scenario where the plugin sends a request to the API:

PUT http://server.com/api/email/update/<SessionID> -d "to=<address1,address2>&subject=<subject>"

In testing this works fine: the data is saved normally. However, the plugin can’t help but send that request several times a second, bombarding my server with identical requests. The result is that I get my EmailSession object saving multiple copies of the recipients.

In terms of my database schema, I have an EmailSession model, which has_many EmailRecipients.

Here’s the relevant part of the update method in my API’s controller:

@email_session = EmailSession.find_or_create_by_session_id(:session_id => params[:id], :user_id => @user.id)

if opts[:params][:cm_to].blank? == false

    self.email_recipients.destroy_all                   

    unless opts[:params][:cm_to].blank?
        opts[:params][:cm_to].strip.split(",").each do |t|
            self.email_recipients << EmailRecipient.create(:recipient_email => t)
        end
    end
end

Admittedly, the “find_or_create” dynamic method is new to me, and I wonder if there’s something about that screwing up the works.

The symptoms I’m seeing include:

  • ActiveRecord errors complaining about attempts to save a non-unique key into the database (I have an index on the SessionId)

  • Duplicate recipients ending up in the EmailRecipients collection

  • In the case of multiple users employing the plugin, I get recipients from other emails ending up in the wrong email session collections.

I’ve attempted to employ delayed_job to attempt to serialize these requests somehow. I haven’t had much luck with it thanks to various bugs in the current release. But I’m wondering if there’s a more fundamental problem with my approach to this solution? Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:35:31+00:00Added an answer on May 28, 2026 at 6:35 pm

    I’m still not sure I understand what you’re doing, but here’s my advice.

    First off I don’t think you are using find_or_create_by properly. This method has slightly confusing semantics (which is why 3.2 introduces some clearer alternatives) but as it stands it isn’t using the user_id to find the record (although it is setting user_id if a record is created). I don’t think this is what you wanted. Instead use find_or_create_by_session_id_and_user_id

    This can still raise a duplicate key error since in between find_or_create checking and it creating the record there is time for someone else to create the record. If you weren’t doing anything other than creating email session rows the  rescuing this duplicate key error and then retrying should take of that: on the retry you’ll find the row that blocked your insert.

    However when you then go on to add recipients you still have a potential issue because 2 things could be trying to remove recipients and add them to the same email session at the same time. This might be a good usecase for pessimistic locking. 

    begin
      EmailSession.transaction do
        session = EmailSession.lock(true).find_or_create_by_bla_bla(...)
        # use the session object here, add recipients etc.
      end
    rescue ActiveRecord::StatementInvalid => e
    end
    

    What is happening here is that when the email session is retrieved from the db, the row is locked (even if it doesn’t exist yet – effectively you can lock the gap where the record would go). This means that anyone else wanting to add recipients or do any other manipulation has to wait for the lock to be released. Locks last as long as the transaction in which they occur lasts  so all your work should happen in here (even if in the second part you are not actually changing the email session object any more).

    You may end up with deadlocks – I don’t know what else is going on in your app but you should be prepared for them if you are using transactions. That’s what the rescue block is for: if the error message looks like a deadlock then you should probably retry some limited number of times.

    Locks are (at least on MySQL) row level locks: as long as you have an index on session_id,user_id then just because one of your instance has one email session object locked doesn’t stop another instance from using another one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple web service that has an API third party developers are
I have an API that can be implemented by various vendors. I can send
So Google Analytics does not have an API that we can use to get
I have a web API that returns python dictionaries or lists as a response
We are creating an API that suits the benefits of a REST based architecture.
Please advise if you can. I am building an SMS web service API that
I'm working with some web services that have already been created and I need
We're in the process of building an internal, Java-based RESTful web services application that
I have two webapps: a web-service client and a server (both CXF-based, using the
I'm writing an API wrapper to a couple of different web services. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.