Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm facing problem with generating character sequence for SMS tracking. there should be a easy to enter code send with all the outgoing messages. Reply SMS will map with that code. I can't generate next sequence of code from database because multiple clients may have read at ones and may end up with same code.

is there any workaround for this?

Database is Informix.

share|improve this question
Do you have some expiration time for reply with sms code? Like, in 15 minutes that code should be disregarded? – Yusubov Jun 29 '12 at 9:52
Yes, there is a expiration time. I have to wait for at least 30 days. – Damith Jun 29 '12 at 9:55
well, this sounds like very long period. how big is your customer base? – Yusubov Jun 29 '12 at 9:56
Around 5k, initially it will be less than 1k, but most of the time one client send more than 100 SMS as bulk. – Damith Jun 29 '12 at 9:59
you may consider to have "sms code + outgoing message id" to be identity for operation. That might do the work. – Yusubov Jun 29 '12 at 10:18
up vote 3 down vote accepted

Do generate the codes, from a shared database. If the codes are shared between customers, the code database should be as well.

Any decent database can deal with concurrent access. I'd just generate codes at random, and try to insert them. If the insert fails, just generate another.

Note that there are only 456 976 alphabetic codes of length 4; if your 5K users generate 100 codes you're quickly out of codes. Adding numbers pushes it up to 1 679 616 alphanumeric codes, but gets you into 0/0 confusion.

See also my answer to a similar question on StackOverflow.

share|improve this answer
you all so potentially lose code space if you need to make sure no code is a rude word, 4 letter codes code generate a lot of four letter words – jk. Jun 29 '12 at 13:29
I'd say that all 4 letter codes are 4 letter words ;) – MSalters Jun 29 '12 at 13:41
yes but not all four letter words are four letter words ;) – jk. Jun 29 '12 at 14:01
@MSalters Thanks for your reply, I need to find a way to generate code from informix table. I'm not familiar with informix. – Damith Jun 29 '12 at 14:51

Use a transactional database, and make reading a code + incrementing the code to the next free one an atomic transaction. When you use a SQL database, google for "SELECT FOR UPDATE".

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.