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 725725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:22:11+00:00 2026-05-14T06:22:11+00:00

I’m building a Sinatra based app for deployment on Heroku. You can imagine it

  • 0

I’m building a Sinatra based app for deployment on Heroku. You can imagine it like a standard URL shortener but where old shortcodes expire and become available for new URLs (I realise this is a silly concept but its easier to explain this way). I’m representing the shortcode in my database as an integer and redefining its reader to give a nice short and unique string from the integer.

As some rows will be deleted, I’ve written code that goes thru all the shortcode integers and picks the first free one to use just before_save. Unfortunately I can make my code create two rows with identical shortcode integers if I run two instances very quickly one after another, which is obviously no good! How should I implement a locking system so that I can quickly save my record with a unique shortcode integer?

Here’s what I have so far:

Chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
CharLength = Chars.length

class Shorts < ActiveRecord::Base
  before_save :gen_shortcode
  after_save  :done_shortcode

  def shortcode
    i = read_attribute(:shortcode).to_i

    return '0' if i == 0
    s = ''
    while i > 0
      s << Chars[i.modulo(CharLength)]
      i /= 62
    end
    s
  end

  private
  def gen_shortcode
    shortcode = 0
    self.class.find(:all,:order=>"shortcode ASC").each do |s|
      if s.read_attribute(:shortcode).to_i != shortcode
        # Begin locking?
        break
      end
      shortcode += 1
    end

    write_attribute(:shortcode,shortcode)
  end

  def done_shortcode
    # End Locking?
  end
end
  • 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-14T06:22:11+00:00Added an answer on May 14, 2026 at 6:22 am

    This line:

    self.class.find(:all,:order=>"shortcode ASC").each
    

    will do a sequential search over your entire record collection. You’d have to lock the entire table so that, when one of your processes is scanning for the next integer, the others will wait for the first one to finish. This will be a performance killer. My suggestion, if possible, is for the process to be as follows:

    1. Add a column that indicates when a record has expired (do you expire them by time of creation? last use?). Index this column.
    2. When you need to find the next lowest usable number, do something like

      Shorts.find(:conditions => {:expired => true},:order => ‘shortcode’)

    This will have the database doing the hard work of finding the lowest expired shortcode. Recall that, in the absence of the :all parameter, the find method will only return the first matching record.

    Now, in order to prevent race conditions between processes, you can wrap this in a transaction and lock while doing the search:

    Shorts.transaction do
        Shorts.find(:conditions => {:expired => true},:order => 'shortcode', :lock => true)
        #Do your thing here. Be quick about it, the row is locked while you work.
    end #on ending the transaction the lock is released
    

    Now when a second process starts looking for a free shortcode, it will not read the one that’s locked (so presumably it will find the next one). This is because the :lock => true parameter gets an exclusive lock (both read/write).

    Check this guide for more on locking with ActiveRecord.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.