Best logical way to track and manage marketing automation actions in a CRM

A marketing crm application I've build involves a feature where the application tracks and scheduled actions for a specific contact. The user schedule these up front and application execute them on time.

When they are created the scheduling instructions are provided as follows.

1. Execute this action on contact creation.
2. Execute this action after 2 days of creation.
3. Execute this action after 3 days of creation.

These actions contains, email sending, reminders to self etc.

The way I've placed it now is, in background the application loop through and execute these actions. Once an action is executed a record is saved in database to keep track. The next time it runs it can skip the already executed steps.

 The flow.

1. Get all the possible steps.
2. Query and loop through each contact.
3. On each contact, query the action tracking table and see which steps has been executed.
4. After excluding executed steps, loop through possible steps and check if any step can be executed (checking with creation date)
5. Execute the steps and record in tracking table.

This flow has allowed flexibility on action management. Because it query actions and it's related instructions on each execution user can go ahead and update the actions any time and have them reflected almost instantly.

However I have to run this flow on every 5 minutes, and right now my application has total of 100,000+ contacts and it's getting harder to maintain.

Is there a  better way to do this or should I just optimize the current flow?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
Your workflow seems quite ok for me. The change that I would propose is to avoid get all the contacts: Try to apply filters to get only the contacts that needs to be processed. I mean, for each step, select the contacts that are valid for the step, and call just for them. That would decrease your processing time.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.